Skip to main content

rustc_resolve/
def_collector.rs

1use std::mem;
2
3use rustc_ast::visit::FnKind;
4use rustc_ast::*;
5use rustc_attr_parsing as attr;
6use rustc_attr_parsing::{AttributeParser, OmitDoc, ShouldEmit};
7use rustc_expand::expand::AstFragment;
8use rustc_hir as hir;
9use rustc_hir::Target;
10use rustc_hir::def::DefKind;
11use rustc_hir::def::Namespace::{TypeNS, ValueNS};
12use rustc_hir::def_id::LocalDefId;
13use rustc_middle::span_bug;
14use rustc_middle::ty::{PerOwnerResolverData, TyCtxtFeed};
15use rustc_span::{Span, Symbol, sym};
16use tracing::{debug, instrument};
17
18use crate::macros::MacroRulesScopeRef;
19use crate::{
20    ConstArgContext, ImplTraitContext, InvocationParent, ParentScope, Resolver, with_owner,
21    with_owner_tables,
22};
23
24pub(crate) fn collect_definitions<'ra>(
25    resolver: &mut Resolver<'ra, '_>,
26    fragment: &AstFragment,
27    parent_scope: ParentScope<'ra>,
28) -> MacroRulesScopeRef<'ra> {
29    let invocation_parent = resolver.invocation_parents[&parent_scope.expansion];
30    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/def_collector.rs:30",
                        "rustc_resolve::def_collector", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/def_collector.rs"),
                        ::tracing_core::__macro_support::Option::Some(30u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_resolve::def_collector"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("new fragment to visit with invocation_parent: {0:?}",
                                                    invocation_parent) as &dyn Value))])
            });
    } else { ; }
};debug!("new fragment to visit with invocation_parent: {invocation_parent:?}");
31    if true {
    match (&resolver.current_owner.id, &DUMMY_NODE_ID) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_eq!(resolver.current_owner.id, DUMMY_NODE_ID);
32    with_owner(resolver, invocation_parent.owner, |r| {
33        let mut visitor = DefCollector { r, invocation_parent, parent_scope };
34        fragment.visit_with(&mut visitor);
35        visitor.parent_scope.macro_rules
36    })
37}
38
39/// Creates `DefId`s for nodes in the AST.
40pub(crate) struct DefCollector<'a, 'ra, 'tcx> {
41    pub(crate) r: &'a mut Resolver<'ra, 'tcx>,
42    invocation_parent: InvocationParent,
43    pub(crate) parent_scope: ParentScope<'ra>,
44}
45
46impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
47    pub(super) fn create_def(
48        &mut self,
49        node_id: NodeId,
50        name: Option<Symbol>,
51        def_kind: DefKind,
52        span: Span,
53    ) -> TyCtxtFeed<'tcx, LocalDefId> {
54        let parent_def = self.invocation_parent.parent_def;
55        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/def_collector.rs:55",
                        "rustc_resolve::def_collector", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/def_collector.rs"),
                        ::tracing_core::__macro_support::Option::Some(55u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_resolve::def_collector"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("create_def(node_id={0:?}, def_kind={1:?}, parent_def={2:?})",
                                                    node_id, def_kind, parent_def) as &dyn Value))])
            });
    } else { ; }
};debug!(
56            "create_def(node_id={:?}, def_kind={:?}, parent_def={:?})",
57            node_id, def_kind, parent_def
58        );
59        self.r.create_def(
60            parent_def,
61            node_id,
62            name,
63            def_kind,
64            self.parent_scope.expansion.to_expn_id(),
65            span.with_parent(None),
66            false,
67        )
68    }
69
70    fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: LocalDefId, f: F) {
71        let orig_parent_def = mem::replace(&mut self.invocation_parent.parent_def, parent_def);
72        f(self);
73        self.invocation_parent.parent_def = orig_parent_def;
74    }
75
76    pub(super) fn with_owner<F: FnOnce(&mut Self, TyCtxtFeed<'tcx, LocalDefId>)>(
77        &mut self,
78        owner: NodeId,
79        name: Option<Symbol>,
80        def_kind: DefKind,
81        span: Span,
82        f: F,
83    ) {
84        if true {
    match (&owner, &DUMMY_NODE_ID) {
        (left_val, right_val) => {
            if *left_val == *right_val {
                let kind = ::core::panicking::AssertKind::Ne;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_ne!(owner, DUMMY_NODE_ID);
85        if true {
    match (&owner, &CRATE_NODE_ID) {
        (left_val, right_val) => {
            if *left_val == *right_val {
                let kind = ::core::panicking::AssertKind::Ne;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_ne!(owner, CRATE_NODE_ID);
86        // We only get here if the owner didn't exist yet. After the owner has been created,
87        // future invocations of `collect_definitions` will get the owner out of the `owners`
88        // table.
89        let parent_def = self.invocation_parent.parent_def;
90        let feed = self.r.create_def(
91            parent_def,
92            owner,
93            name,
94            def_kind,
95            self.parent_scope.expansion.to_expn_id(),
96            span.with_parent(None),
97            true,
98        );
99        let tables = PerOwnerResolverData::new(owner, feed.key());
100
101        let orig_invoc_owner = mem::replace(&mut self.invocation_parent.owner, owner);
102        with_owner_tables(self, owner, tables, |this| f(this, feed));
103        let old = mem::replace(&mut self.invocation_parent.owner, orig_invoc_owner);
104        match (&old, &owner) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(old, owner);
105    }
106
107    fn with_impl_trait<F: FnOnce(&mut Self)>(
108        &mut self,
109        impl_trait_context: ImplTraitContext,
110        f: F,
111    ) {
112        let orig_itc =
113            mem::replace(&mut self.invocation_parent.impl_trait_context, impl_trait_context);
114        f(self);
115        self.invocation_parent.impl_trait_context = orig_itc;
116    }
117
118    fn with_const_arg<F: FnOnce(&mut Self)>(&mut self, ctxt: ConstArgContext, f: F) {
119        let orig = mem::replace(&mut self.invocation_parent.const_arg_context, ctxt);
120        f(self);
121        self.invocation_parent.const_arg_context = orig;
122    }
123
124    fn collect_field(&mut self, field: &'a FieldDef, index: Option<usize>) {
125        let index = |this: &Self| {
126            index.unwrap_or_else(|| {
127                let node_id = NodeId::placeholder_from_expn_id(this.parent_scope.expansion);
128                this.r.placeholder_field_indices[&node_id]
129            })
130        };
131
132        if field.is_placeholder {
133            let old_index = self.r.placeholder_field_indices.insert(field.id, index(self));
134            if !old_index.is_none() {
    {
        ::core::panicking::panic_fmt(format_args!("placeholder field index is reset for a node ID"));
    }
};assert!(old_index.is_none(), "placeholder field index is reset for a node ID");
135            self.visit_macro_invoc(field.id);
136            self.visit_invoc(field.id);
137        } else {
138            let name = field.ident.map_or_else(|| sym::integer(index(self)), |ident| ident.name);
139            let def = self.create_def(field.id, Some(name), DefKind::Field, field.span);
140            self.with_parent(def.def_id(), |this| this.brg_visit_field_def(field, def));
141        }
142    }
143
144    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("visit_macro_invoc",
                                    "rustc_resolve::def_collector", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/def_collector.rs"),
                                    ::tracing_core::__macro_support::Option::Some(144u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_resolve::def_collector"),
                                    ::tracing_core::field::FieldSet::new(&["id"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&id)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/def_collector.rs:146",
                                    "rustc_resolve::def_collector", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/def_collector.rs"),
                                    ::tracing_core::__macro_support::Option::Some(146u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_resolve::def_collector"),
                                    ::tracing_core::field::FieldSet::new(&["self.invocation_parent"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&self.invocation_parent)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            let id = id.placeholder_to_expn_id();
            let old_parent =
                self.r.invocation_parents.insert(id, self.invocation_parent);
            if !old_parent.is_none() {
                {
                    ::core::panicking::panic_fmt(format_args!("parent `LocalDefId` is reset for an invocation"));
                }
            };
        }
    }
}#[instrument(level = "debug", skip(self))]
145    fn visit_macro_invoc(&mut self, id: NodeId) {
146        debug!(?self.invocation_parent);
147
148        let id = id.placeholder_to_expn_id();
149        let old_parent = self.r.invocation_parents.insert(id, self.invocation_parent);
150        assert!(old_parent.is_none(), "parent `LocalDefId` is reset for an invocation");
151    }
152}
153
154impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
155    fn visit_item(&mut self, i: &'a Item) {
156        // Pick the def data. This need not be unique, but the more
157        // information we encapsulate into, the better
158        let mut opt_syn_ext = None;
159        let def_kind = match &i.kind {
160            ItemKind::Impl(i) => DefKind::Impl { of_trait: i.of_trait.is_some() },
161            ItemKind::ForeignMod(..) => DefKind::ForeignMod,
162            ItemKind::Mod(..) => DefKind::Mod,
163            ItemKind::Trait(..) => DefKind::Trait,
164            ItemKind::TraitAlias(..) => DefKind::TraitAlias,
165            ItemKind::Enum(..) => DefKind::Enum,
166            ItemKind::Struct(..) => DefKind::Struct,
167            ItemKind::Union(..) => DefKind::Union,
168            ItemKind::ExternCrate(..) => DefKind::ExternCrate,
169            ItemKind::TyAlias(..) => DefKind::TyAlias,
170            ItemKind::Static(s) => DefKind::Static {
171                safety: hir::Safety::Safe,
172                mutability: s.mutability,
173                nested: false,
174            },
175            ItemKind::Const(citem) => {
176                let is_type_const = #[allow(non_exhaustive_omitted_patterns)] match citem.rhs_kind {
    ConstItemRhsKind::TypeConst { .. } => true,
    _ => false,
}matches!(citem.rhs_kind, ConstItemRhsKind::TypeConst { .. });
177                DefKind::Const { is_type_const }
178            }
179            ItemKind::ConstBlock(..) => DefKind::Const { is_type_const: false },
180            ItemKind::Fn(..) | ItemKind::Delegation(..) => DefKind::Fn,
181            ItemKind::MacroDef(ident, def) => {
182                let edition = i.span.edition();
183
184                // FIXME(jdonszelmann) make one of these in the resolver?
185                // FIXME(jdonszelmann) don't care about tools here maybe? Just parse what we can.
186                // Does that prevents errors from happening? maybe
187                let mut parser = AttributeParser::new(
188                    &self.r.tcx.sess,
189                    self.r.tcx.features(),
190                    self.r.tcx().registered_tools(()),
191                    ShouldEmit::Nothing,
192                );
193                let attrs = parser.parse_attribute_list(
194                    &i.attrs,
195                    i.span,
196                    Target::MacroDef,
197                    OmitDoc::Skip,
198                    std::convert::identity,
199                    |_lint_id, _span, _kind| {
200                        // FIXME(jdonszelmann): emit lints here properly
201                        // NOTE that before new attribute parsing, they didn't happen either
202                        // but it would be nice if we could change that.
203                    },
204                );
205
206                let ext = self.r.compile_macro(def, *ident, &attrs, i.span, i.id, edition);
207                let macro_kinds = ext.macro_kinds();
208                opt_syn_ext = Some(ext);
209                DefKind::Macro(macro_kinds)
210            }
211            ItemKind::GlobalAsm(..) => DefKind::GlobalAsm,
212            ItemKind::Use(_) => {
213                return self.with_owner(i.id, None, DefKind::Use, i.span, |this, feed| {
214                    this.brg_visit_item(i, feed);
215                });
216            }
217            ItemKind::MacCall(..) => {
218                self.visit_macro_invoc(i.id);
219                self.brg_visit_mac_call_in_module(i.id);
220                return;
221            }
222            ItemKind::DelegationMac(..) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
223        };
224        self.with_owner(
225            i.id,
226            i.kind.ident().map(|ident| ident.name),
227            def_kind,
228            i.span,
229            |this, feed| {
230                if let Some(ext) = opt_syn_ext {
231                    this.r.local_macro_map.insert(feed.def_id(), self.r.arenas.alloc_macro(ext));
232                }
233
234                this.with_parent(feed.def_id(), |this| {
235                    this.with_impl_trait(ImplTraitContext::Existential, |this| {
236                        this.brg_visit_item(i, feed)
237                    })
238                });
239            },
240        );
241    }
242
243    fn visit_block(&mut self, block: &'a Block) {
244        self.brg_visit_block(block);
245    }
246
247    fn visit_fn(&mut self, fn_kind: FnKind<'a>, _: &AttrVec, span: Span, _: NodeId) {
248        match fn_kind {
249            FnKind::Fn(
250                ctxt,
251                _vis,
252                Fn {
253                    sig: FnSig { header, decl, span: _ }, ident, generics, contract, body, ..
254                },
255            ) if let Some(coroutine_kind) = header.coroutine_kind
256                // Foreign ones are denied, so don't create them here.
257                && ctxt != visit::FnCtxt::Foreign =>
258            {
259                self.visit_ident(ident);
260                self.visit_fn_header(header);
261                self.visit_generics(generics);
262                if let Some(contract) = contract {
263                    self.visit_contract(contract);
264                }
265
266                // For async functions, we need to create their inner defs inside of a
267                // closure to match their desugared representation. Besides that,
268                // we must mirror everything that `visit::walk_fn` below does.
269                let FnDecl { inputs, output } = &**decl;
270                for param in inputs {
271                    self.visit_param(param);
272                }
273
274                let (return_id, return_span) = coroutine_kind.return_id();
275                let return_def =
276                    self.create_def(return_id, None, DefKind::OpaqueTy, return_span).def_id();
277                self.with_parent(return_def, |this| this.visit_fn_ret_ty(output));
278
279                // If this async fn has no body (i.e. it's an async fn signature in a trait)
280                // then the closure_def will never be used, and we should avoid generating a
281                // def-id for it.
282                if let Some(body) = body {
283                    let closure_def = self
284                        .create_def(coroutine_kind.closure_id(), None, DefKind::Closure, span)
285                        .def_id();
286                    self.with_parent(closure_def, |this| this.visit_block(body));
287                }
288            }
289            FnKind::Closure(binder, Some(coroutine_kind), decl, body) => {
290                self.visit_closure_binder(binder);
291                visit::walk_fn_decl(self, decl);
292
293                // Async closures desugar to closures inside of closures, so
294                // we must create two defs.
295                let coroutine_def = self
296                    .create_def(coroutine_kind.closure_id(), None, DefKind::Closure, span)
297                    .def_id();
298                self.with_parent(coroutine_def, |this| this.visit_expr(body));
299            }
300            _ => visit::walk_fn(self, fn_kind),
301        }
302    }
303
304    fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
305        let (ident, def_kind) = match fi.kind {
306            ForeignItemKind::Static(StaticItem {
307                ident,
308                ty: _,
309                mutability,
310                expr: _,
311                safety,
312                define_opaque: _,
313                eii_impls: _,
314            }) => {
315                let safety = match safety {
316                    ast::Safety::Unsafe(_) | ast::Safety::Default => hir::Safety::Unsafe,
317                    ast::Safety::Safe(_) => hir::Safety::Safe,
318                };
319
320                (ident, DefKind::Static { safety, mutability, nested: false })
321            }
322            ForeignItemKind::Fn(Fn { ident, .. }) => (ident, DefKind::Fn),
323            ForeignItemKind::TyAlias(TyAlias { ident, .. }) => (ident, DefKind::ForeignTy),
324            ForeignItemKind::MacCall(_) => {
325                self.visit_invoc_in_module(fi.id);
326                self.visit_macro_invoc(fi.id);
327                return;
328            }
329        };
330
331        self.with_owner(fi.id, Some(ident.name), def_kind, fi.span, |this, def| {
332            this.with_parent(def.def_id(), |this| {
333                this.build_reduced_graph_for_foreign_item(fi, ident, def);
334                visit::walk_item(this, fi)
335            });
336        })
337    }
338
339    fn visit_variant(&mut self, v: &'a Variant) {
340        if v.is_placeholder {
341            self.visit_macro_invoc(v.id);
342            self.visit_invoc_in_module(v.id);
343            return;
344        }
345        let feed = self.create_def(v.id, Some(v.ident.name), DefKind::Variant, v.span);
346        self.with_parent(feed.def_id(), |this| this.brg_visit_variant(v, feed));
347    }
348
349    fn visit_where_predicate(&mut self, pred: &'a WherePredicate) {
350        if pred.is_placeholder {
351            self.visit_macro_invoc(pred.id);
352            self.visit_invoc(pred.id);
353        } else {
354            visit::walk_where_predicate(self, pred)
355        }
356    }
357
358    fn visit_variant_data(&mut self, data: &'a VariantData) {
359        // The assumption here is that non-`cfg` macro expansion cannot change field indices.
360        // It currently holds because only inert attributes are accepted on fields,
361        // and every such attribute expands into a single field after it's resolved.
362        for (index, field) in data.fields().iter().enumerate() {
363            self.collect_field(field, Some(index));
364        }
365    }
366
367    fn visit_generic_param(&mut self, param: &'a GenericParam) {
368        if param.is_placeholder {
369            self.visit_macro_invoc(param.id);
370            self.visit_invoc(param.id);
371            return;
372        }
373        let def_kind = match param.kind {
374            GenericParamKind::Lifetime { .. } => DefKind::LifetimeParam,
375            GenericParamKind::Type { .. } => DefKind::TyParam,
376            GenericParamKind::Const { .. } => DefKind::ConstParam,
377        };
378        self.create_def(param.id, Some(param.ident.name), def_kind, param.ident.span);
379
380        // impl-Trait can happen inside generic parameters, like
381        // ```
382        // fn foo<U: Iterator<Item = impl Clone>>() {}
383        // ```
384        //
385        // In that case, the impl-trait is lowered as an additional generic parameter.
386        self.with_impl_trait(ImplTraitContext::Universal, |this| {
387            visit::walk_generic_param(this, param)
388        });
389    }
390
391    fn visit_assoc_item(&mut self, i: &'a AssocItem, ctxt: visit::AssocCtxt) {
392        let (ident, def_kind, ns) = match &i.kind {
393            AssocItemKind::Fn(Fn { ident, .. })
394            | AssocItemKind::Delegation(Delegation { ident, .. }) => {
395                (*ident, DefKind::AssocFn, ValueNS)
396            }
397            AssocItemKind::Const(ConstItem { ident, rhs_kind, .. }) => (
398                *ident,
399                DefKind::AssocConst {
400                    is_type_const: #[allow(non_exhaustive_omitted_patterns)] match rhs_kind {
    ConstItemRhsKind::TypeConst { .. } => true,
    _ => false,
}matches!(rhs_kind, ConstItemRhsKind::TypeConst { .. }),
401                },
402                ValueNS,
403            ),
404            AssocItemKind::Type(TyAlias { ident, .. }) => (*ident, DefKind::AssocTy, TypeNS),
405            AssocItemKind::MacCall(..) => {
406                self.visit_macro_invoc(i.id);
407                self.visit_assoc_item_mac_call(i, ctxt);
408                return;
409            }
410            AssocItemKind::DelegationMac(..) => {
411                ::rustc_middle::util::bug::span_bug_fmt(i.span,
    format_args!("degation mac invoc should have already been handled"))span_bug!(i.span, "degation mac invoc should have already been handled")
412            }
413        };
414
415        self.with_owner(i.id, Some(ident.name), def_kind, i.span, |this, feed| {
416            this.with_parent(feed.def_id(), |this| {
417                this.brg_visit_assoc_item(i, ctxt, ident, ns, feed)
418            });
419        })
420    }
421
422    fn visit_pat(&mut self, pat: &'a Pat) {
423        match pat.kind {
424            PatKind::MacCall(..) => {
425                self.visit_macro_invoc(pat.id);
426                self.visit_invoc(pat.id);
427            }
428            _ => visit::walk_pat(self, pat),
429        }
430    }
431
432    fn visit_anon_const(&mut self, constant: &'a AnonConst) {
433        // `MgcaDisambiguation::Direct` is set even when MGCA is disabled, so
434        // to avoid affecting stable we have to feature gate the not creating
435        // anon consts
436        if !self.r.tcx.features().min_generic_const_args() {
437            let parent = self
438                .create_def(constant.id, None, DefKind::AnonConst, constant.value.span)
439                .def_id();
440            return self.with_parent(parent, |this| visit::walk_anon_const(this, constant));
441        }
442
443        match constant.mgca_disambiguation {
444            MgcaDisambiguation::Direct => self.with_const_arg(ConstArgContext::Direct, |this| {
445                visit::walk_anon_const(this, constant);
446            }),
447            MgcaDisambiguation::AnonConst => {
448                self.with_const_arg(ConstArgContext::NonDirect, |this| {
449                    let parent = this
450                        .create_def(constant.id, None, DefKind::AnonConst, constant.value.span)
451                        .def_id();
452                    this.with_parent(parent, |this| visit::walk_anon_const(this, constant));
453                })
454            }
455        };
456    }
457
458    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("visit_expr",
                                    "rustc_resolve::def_collector", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/def_collector.rs"),
                                    ::tracing_core::__macro_support::Option::Some(458u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_resolve::def_collector"),
                                    ::tracing_core::field::FieldSet::new(&["expr"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expr)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/def_collector.rs:460",
                                    "rustc_resolve::def_collector", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/def_collector.rs"),
                                    ::tracing_core::__macro_support::Option::Some(460u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_resolve::def_collector"),
                                    ::tracing_core::field::FieldSet::new(&["self.invocation_parent"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&self.invocation_parent)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            let parent_def =
                match &expr.kind {
                    ExprKind::MacCall(..) => {
                        self.visit_macro_invoc(expr.id);
                        self.visit_invoc(expr.id);
                        return;
                    }
                    ExprKind::Closure(..) | ExprKind::Gen(..) => {
                        self.create_def(expr.id, None, DefKind::Closure,
                                expr.span).def_id()
                    }
                    ExprKind::ConstBlock(constant) => {
                        let def_kind =
                            match self.invocation_parent.const_arg_context {
                                ConstArgContext::Direct => DefKind::AnonConst,
                                ConstArgContext::NonDirect => DefKind::InlineConst,
                            };
                        return self.with_const_arg(ConstArgContext::NonDirect,
                                |this|
                                    {
                                        for attr in &expr.attrs {
                                            visit::walk_attribute(this, attr);
                                        }
                                        let def =
                                            this.create_def(constant.id, None, def_kind,
                                                    constant.value.span).def_id();
                                        this.with_parent(def,
                                            |this| visit::walk_anon_const(this, constant));
                                    });
                    }
                    ExprKind::Struct(_) | ExprKind::Call(..) | ExprKind::Tup(..)
                        | ExprKind::Array(..) => {
                        return visit::walk_expr(self, expr);
                    }
                    ExprKind::Block(block, _) if
                        let [stmt] = block.stmts.as_slice() =>
                        match stmt.kind {
                            StmtKind::Expr(..) | StmtKind::MacCall(..) =>
                                return visit::walk_expr(self, expr),
                            StmtKind::Let(..) | StmtKind::Item(..) | StmtKind::Semi(..)
                                | StmtKind::Empty => {
                                self.invocation_parent.parent_def
                            }
                        },
                    _ => self.invocation_parent.parent_def,
                };
            self.with_const_arg(ConstArgContext::NonDirect,
                |this|
                    {
                        this.with_parent(parent_def,
                            |this| visit::walk_expr(this, expr))
                    })
        }
    }
}#[instrument(level = "debug", skip(self))]
459    fn visit_expr(&mut self, expr: &'a Expr) {
460        debug!(?self.invocation_parent);
461
462        let parent_def = match &expr.kind {
463            ExprKind::MacCall(..) => {
464                self.visit_macro_invoc(expr.id);
465                self.visit_invoc(expr.id);
466                return;
467            }
468            ExprKind::Closure(..) | ExprKind::Gen(..) => {
469                self.create_def(expr.id, None, DefKind::Closure, expr.span).def_id()
470            }
471            ExprKind::ConstBlock(constant) => {
472                // Under `min_generic_const_args` a `const { }` block sometimes
473                // corresponds to an anon const rather than an inline const.
474                let def_kind = match self.invocation_parent.const_arg_context {
475                    ConstArgContext::Direct => DefKind::AnonConst,
476                    ConstArgContext::NonDirect => DefKind::InlineConst,
477                };
478
479                return self.with_const_arg(ConstArgContext::NonDirect, |this| {
480                    for attr in &expr.attrs {
481                        visit::walk_attribute(this, attr);
482                    }
483
484                    let def =
485                        this.create_def(constant.id, None, def_kind, constant.value.span).def_id();
486                    this.with_parent(def, |this| visit::walk_anon_const(this, constant));
487                });
488            }
489
490            // Avoid overwriting `const_arg_context` as we may want to treat const blocks
491            // as being anon consts if we are inside a const argument.
492            ExprKind::Struct(_) | ExprKind::Call(..) | ExprKind::Tup(..) | ExprKind::Array(..) => {
493                return visit::walk_expr(self, expr);
494            }
495            // FIXME(mgca): we may want to handle block labels in some manner
496            ExprKind::Block(block, _) if let [stmt] = block.stmts.as_slice() => match stmt.kind {
497                // FIXME(mgca): this probably means that mac calls that expand
498                // to semi'd const blocks are handled differently to just writing
499                // out a semi'd const block.
500                StmtKind::Expr(..) | StmtKind::MacCall(..) => return visit::walk_expr(self, expr),
501
502                // Fallback to normal behaviour
503                StmtKind::Let(..) | StmtKind::Item(..) | StmtKind::Semi(..) | StmtKind::Empty => {
504                    self.invocation_parent.parent_def
505                }
506            },
507
508            _ => self.invocation_parent.parent_def,
509        };
510
511        self.with_const_arg(ConstArgContext::NonDirect, |this| {
512            // Note in some cases the `parent_def` here may be the existing parent
513            // and this is actually a no-op `with_parent` call.
514            this.with_parent(parent_def, |this| visit::walk_expr(this, expr))
515        })
516    }
517
518    fn visit_ty(&mut self, ty: &'a Ty) {
519        match ty.kind {
520            TyKind::MacCall(..) => {
521                self.visit_macro_invoc(ty.id);
522                self.visit_invoc(ty.id);
523            }
524            TyKind::ImplTrait(opaque_id, _) => {
525                let name = *self
526                    .r
527                    .impl_trait_names
528                    .get(&ty.id)
529                    .unwrap_or_else(|| ::rustc_middle::util::bug::span_bug_fmt(ty.span,
    format_args!("expected this opaque to be named"))span_bug!(ty.span, "expected this opaque to be named"));
530                let kind = match self.invocation_parent.impl_trait_context {
531                    ImplTraitContext::Universal => DefKind::TyParam,
532                    ImplTraitContext::Existential => DefKind::OpaqueTy,
533                    ImplTraitContext::InBinding => return visit::walk_ty(self, ty),
534                };
535                let id = self.create_def(opaque_id, Some(name), kind, ty.span).def_id();
536                match self.invocation_parent.impl_trait_context {
537                    // Do not nest APIT, as we desugar them as `impl_trait: bounds`,
538                    // so the `impl_trait` node is not a parent to `bounds`.
539                    ImplTraitContext::Universal => visit::walk_ty(self, ty),
540                    ImplTraitContext::Existential => {
541                        self.with_parent(id, |this| visit::walk_ty(this, ty))
542                    }
543                    ImplTraitContext::InBinding => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
544                };
545            }
546            _ => visit::walk_ty(self, ty),
547        }
548    }
549
550    fn visit_stmt(&mut self, stmt: &'a Stmt) {
551        match stmt.kind {
552            StmtKind::MacCall(..) => {
553                self.brg_visit_mac_call_in_module(stmt.id);
554                self.visit_macro_invoc(stmt.id)
555            }
556            // FIXME(impl_trait_in_bindings): We don't really have a good way of
557            // introducing the right `ImplTraitContext` here for all the cases we
558            // care about, in case we want to introduce ITIB to other positions
559            // such as turbofishes (e.g. `foo::<impl Fn()>(|| {})`).
560            StmtKind::Let(ref local) => self.with_impl_trait(ImplTraitContext::InBinding, |this| {
561                visit::walk_local(this, local)
562            }),
563            _ => visit::walk_stmt(self, stmt),
564        }
565    }
566
567    fn visit_arm(&mut self, arm: &'a Arm) {
568        if arm.is_placeholder {
569            self.visit_macro_invoc(arm.id);
570            self.visit_invoc(arm.id);
571        } else {
572            visit::walk_arm(self, arm)
573        }
574    }
575
576    fn visit_expr_field(&mut self, f: &'a ExprField) {
577        if f.is_placeholder {
578            self.visit_macro_invoc(f.id);
579            self.visit_invoc(f.id);
580        } else {
581            visit::walk_expr_field(self, f)
582        }
583    }
584
585    fn visit_pat_field(&mut self, fp: &'a PatField) {
586        if fp.is_placeholder {
587            self.visit_macro_invoc(fp.id);
588            self.visit_invoc(fp.id);
589        } else {
590            visit::walk_pat_field(self, fp)
591        }
592    }
593
594    fn visit_param(&mut self, p: &'a Param) {
595        if p.is_placeholder {
596            self.visit_macro_invoc(p.id);
597            self.visit_invoc(p.id);
598        } else {
599            self.with_impl_trait(ImplTraitContext::Universal, |this| visit::walk_param(this, p))
600        }
601    }
602
603    // This method is called only when we are visiting an individual field
604    // after expanding an attribute on it.
605    fn visit_field_def(&mut self, field: &'a FieldDef) {
606        self.collect_field(field, None);
607    }
608
609    fn visit_crate(&mut self, krate: &'a Crate) {
610        if krate.is_placeholder {
611            self.visit_macro_invoc(krate.id);
612            self.visit_invoc_in_module(krate.id);
613        } else {
614            // Visit attributes after items for backward compatibility.
615            // This way they can use `macro_rules` defined later.
616
617            // We always have an invocation parent (set in `collect_definitions`)
618            // of at least the crate root, even for visiting the crate root,
619            // which would then remove the crate root from the tables
620            // list twice and try to insert it twice afterwards.
621            if true {
    match (&self.r.current_owner.id, &CRATE_NODE_ID) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_eq!(self.r.current_owner.id, CRATE_NODE_ID);
622
623            for elem in &krate.items {
    match ::rustc_ast_ir::visit::VisitorResult::branch(self.visit_item(elem))
        {
        core::ops::ControlFlow::Continue(()) =>
            (),
            #[allow(unreachable_code)]
            core::ops::ControlFlow::Break(r) => {
            return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
        }
    };
};visit::walk_list!(self, visit_item, &krate.items);
624            for elem in &krate.attrs {
    match ::rustc_ast_ir::visit::VisitorResult::branch(self.visit_attribute(elem))
        {
        core::ops::ControlFlow::Continue(()) =>
            (),
            #[allow(unreachable_code)]
            core::ops::ControlFlow::Break(r) => {
            return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
        }
    };
};visit::walk_list!(self, visit_attribute, &krate.attrs);
625            self.contains_macro_use(&krate.attrs);
626        }
627    }
628
629    fn visit_attribute(&mut self, attr: &'a Attribute) {
630        let orig_in_attr = mem::replace(&mut self.invocation_parent.in_attr, true);
631        if !attr.is_doc_comment() && attr::is_builtin_attr(attr) {
632            self.r
633                .builtin_attrs
634                .push((attr.get_normal_item().path.segments[0].ident, self.parent_scope));
635        }
636        visit::walk_attribute(self, attr);
637        self.invocation_parent.in_attr = orig_in_attr;
638    }
639
640    fn visit_inline_asm(&mut self, asm: &'a InlineAsm) {
641        let InlineAsm {
642            asm_macro: _,
643            template: _,
644            template_strs: _,
645            operands,
646            clobber_abis: _,
647            options: _,
648            line_spans: _,
649        } = asm;
650        for (op, _span) in operands {
651            match op {
652                InlineAsmOperand::In { expr, reg: _ }
653                | InlineAsmOperand::Out { expr: Some(expr), reg: _, late: _ }
654                | InlineAsmOperand::InOut { expr, reg: _, late: _ } => {
655                    self.visit_expr(expr);
656                }
657                InlineAsmOperand::Out { expr: None, reg: _, late: _ } => {}
658                InlineAsmOperand::SplitInOut { in_expr, out_expr, reg: _, late: _ } => {
659                    self.visit_expr(in_expr);
660                    if let Some(expr) = out_expr {
661                        self.visit_expr(expr);
662                    }
663                }
664                InlineAsmOperand::Const { anon_const } => {
665                    let def = self
666                        .create_def(
667                            anon_const.id,
668                            None,
669                            DefKind::InlineConst,
670                            anon_const.value.span,
671                        )
672                        .def_id();
673                    self.with_parent(def, |this| visit::walk_anon_const(this, anon_const));
674                }
675                InlineAsmOperand::Sym { sym } => self.visit_inline_asm_sym(sym),
676                InlineAsmOperand::Label { block } => self.visit_block(block),
677            }
678        }
679    }
680}