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::TyCtxtFeed;
15use rustc_span::{Span, Symbol, sym};
16use tracing::{debug, instrument};
17
18use crate::macros::MacroRulesScopeRef;
19use crate::{ConstArgContext, ImplTraitContext, InvocationParent, ParentScope, Resolver};
20
21pub(crate) fn collect_definitions<'ra>(
22    resolver: &mut Resolver<'ra, '_>,
23    fragment: &AstFragment,
24    parent_scope: ParentScope<'ra>,
25) -> MacroRulesScopeRef<'ra> {
26    let invocation_parent = resolver.invocation_parents[&parent_scope.expansion];
27    {
    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:27",
                        "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(27u32),
                        ::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:?}");
28    let mut visitor = DefCollector { r: resolver, invocation_parent, parent_scope };
29    fragment.visit_with(&mut visitor);
30    visitor.parent_scope.macro_rules
31}
32
33/// Creates `DefId`s for nodes in the AST.
34pub(crate) struct DefCollector<'a, 'ra, 'tcx> {
35    pub(crate) r: &'a mut Resolver<'ra, 'tcx>,
36    invocation_parent: InvocationParent,
37    pub(crate) parent_scope: ParentScope<'ra>,
38}
39
40impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
41    pub(super) fn create_def(
42        &mut self,
43        node_id: NodeId,
44        name: Option<Symbol>,
45        def_kind: DefKind,
46        span: Span,
47    ) -> TyCtxtFeed<'tcx, LocalDefId> {
48        let parent_def = self.invocation_parent.parent_def;
49        {
    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:49",
                        "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(49u32),
                        ::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!(
50            "create_def(node_id={:?}, def_kind={:?}, parent_def={:?})",
51            node_id, def_kind, parent_def
52        );
53        self.r.create_def(
54            parent_def,
55            node_id,
56            name,
57            def_kind,
58            self.parent_scope.expansion.to_expn_id(),
59            span.with_parent(None),
60        )
61    }
62
63    fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: LocalDefId, f: F) {
64        let orig_parent_def = mem::replace(&mut self.invocation_parent.parent_def, parent_def);
65        f(self);
66        self.invocation_parent.parent_def = orig_parent_def;
67    }
68
69    fn with_impl_trait<F: FnOnce(&mut Self)>(
70        &mut self,
71        impl_trait_context: ImplTraitContext,
72        f: F,
73    ) {
74        let orig_itc =
75            mem::replace(&mut self.invocation_parent.impl_trait_context, impl_trait_context);
76        f(self);
77        self.invocation_parent.impl_trait_context = orig_itc;
78    }
79
80    fn with_const_arg<F: FnOnce(&mut Self)>(&mut self, ctxt: ConstArgContext, f: F) {
81        let orig = mem::replace(&mut self.invocation_parent.const_arg_context, ctxt);
82        f(self);
83        self.invocation_parent.const_arg_context = orig;
84    }
85
86    fn collect_field(&mut self, field: &'a FieldDef, index: Option<usize>) {
87        let index = |this: &Self| {
88            index.unwrap_or_else(|| {
89                let node_id = NodeId::placeholder_from_expn_id(this.parent_scope.expansion);
90                this.r.placeholder_field_indices[&node_id]
91            })
92        };
93
94        if field.is_placeholder {
95            let old_index = self.r.placeholder_field_indices.insert(field.id, index(self));
96            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");
97            self.visit_macro_invoc(field.id);
98            self.visit_invoc(field.id);
99        } else {
100            let name = field.ident.map_or_else(|| sym::integer(index(self)), |ident| ident.name);
101            let def = self.create_def(field.id, Some(name), DefKind::Field, field.span);
102            self.with_parent(def.def_id(), |this| this.brg_visit_field_def(field, def));
103        }
104    }
105
106    #[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(106u32),
                                    ::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:108",
                                    "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(108u32),
                                    ::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))]
107    fn visit_macro_invoc(&mut self, id: NodeId) {
108        debug!(?self.invocation_parent);
109
110        let id = id.placeholder_to_expn_id();
111        let old_parent = self.r.invocation_parents.insert(id, self.invocation_parent);
112        assert!(old_parent.is_none(), "parent `LocalDefId` is reset for an invocation");
113    }
114}
115
116impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
117    fn visit_item(&mut self, i: &'a Item) {
118        // Pick the def data. This need not be unique, but the more
119        // information we encapsulate into, the better
120        let mut opt_macro_data = None;
121        let def_kind = match &i.kind {
122            ItemKind::Impl(i) => DefKind::Impl { of_trait: i.of_trait.is_some() },
123            ItemKind::ForeignMod(..) => DefKind::ForeignMod,
124            ItemKind::Mod(..) => DefKind::Mod,
125            ItemKind::Trait(..) => DefKind::Trait,
126            ItemKind::TraitAlias(..) => DefKind::TraitAlias,
127            ItemKind::Enum(..) => DefKind::Enum,
128            ItemKind::Struct(..) => DefKind::Struct,
129            ItemKind::Union(..) => DefKind::Union,
130            ItemKind::ExternCrate(..) => DefKind::ExternCrate,
131            ItemKind::TyAlias(..) => DefKind::TyAlias,
132            ItemKind::Static(s) => DefKind::Static {
133                safety: hir::Safety::Safe,
134                mutability: s.mutability,
135                nested: false,
136            },
137            ItemKind::Const(citem) => {
138                let is_type_const = #[allow(non_exhaustive_omitted_patterns)] match citem.rhs_kind {
    ConstItemRhsKind::TypeConst { .. } => true,
    _ => false,
}matches!(citem.rhs_kind, ConstItemRhsKind::TypeConst { .. });
139                DefKind::Const { is_type_const }
140            }
141            ItemKind::ConstBlock(..) => DefKind::Const { is_type_const: false },
142            ItemKind::Fn(..) | ItemKind::Delegation(..) => DefKind::Fn,
143            ItemKind::MacroDef(ident, def) => {
144                let edition = i.span.edition();
145
146                // FIXME(jdonszelmann) make one of these in the resolver?
147                // FIXME(jdonszelmann) don't care about tools here maybe? Just parse what we can.
148                // Does that prevents errors from happening? maybe
149                let mut parser = AttributeParser::new(
150                    &self.r.tcx.sess,
151                    self.r.tcx.features(),
152                    self.r.tcx().registered_tools(()),
153                    ShouldEmit::Nothing,
154                );
155                let attrs = parser.parse_attribute_list(
156                    &i.attrs,
157                    i.span,
158                    Target::MacroDef,
159                    OmitDoc::Skip,
160                    std::convert::identity,
161                    |_lint_id, _span, _kind| {
162                        // FIXME(jdonszelmann): emit lints here properly
163                        // NOTE that before new attribute parsing, they didn't happen either
164                        // but it would be nice if we could change that.
165                    },
166                );
167
168                let macro_data = self.r.compile_macro(def, *ident, &attrs, i.span, i.id, edition);
169                let macro_kinds = macro_data.ext.macro_kinds();
170                opt_macro_data = Some(macro_data);
171                DefKind::Macro(macro_kinds)
172            }
173            ItemKind::GlobalAsm(..) => DefKind::GlobalAsm,
174            ItemKind::Use(_) => {
175                let feed = self.create_def(i.id, None, DefKind::Use, i.span);
176                self.brg_visit_item(i, feed);
177                return;
178            }
179            ItemKind::MacCall(..) => {
180                self.visit_macro_invoc(i.id);
181                self.brg_visit_mac_call_in_module(i.id);
182                return;
183            }
184            ItemKind::DelegationMac(..) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
185        };
186        let feed = self.create_def(i.id, i.kind.ident().map(|ident| ident.name), def_kind, i.span);
187
188        if let Some(macro_data) = opt_macro_data {
189            self.r.new_local_macro(feed.def_id(), macro_data);
190        }
191
192        self.with_parent(feed.def_id(), |this| {
193            this.with_impl_trait(ImplTraitContext::Existential, |this| this.brg_visit_item(i, feed))
194        });
195    }
196
197    fn visit_block(&mut self, block: &'a Block) {
198        self.brg_visit_block(block);
199    }
200
201    fn visit_fn(&mut self, fn_kind: FnKind<'a>, _: &AttrVec, span: Span, _: NodeId) {
202        match fn_kind {
203            FnKind::Fn(
204                ctxt,
205                _vis,
206                Fn {
207                    sig: FnSig { header, decl, span: _ }, ident, generics, contract, body, ..
208                },
209            ) if let Some(coroutine_kind) = header.coroutine_kind
210                // Foreign ones are denied, so don't create them here.
211                && ctxt != visit::FnCtxt::Foreign =>
212            {
213                self.visit_ident(ident);
214                self.visit_fn_header(header);
215                self.visit_generics(generics);
216                if let Some(contract) = contract {
217                    self.visit_contract(contract);
218                }
219
220                // For async functions, we need to create their inner defs inside of a
221                // closure to match their desugared representation. Besides that,
222                // we must mirror everything that `visit::walk_fn` below does.
223                let FnDecl { inputs, output } = &**decl;
224                for param in inputs {
225                    self.visit_param(param);
226                }
227
228                let (return_id, return_span) = coroutine_kind.return_id();
229                let return_def =
230                    self.create_def(return_id, None, DefKind::OpaqueTy, return_span).def_id();
231                self.with_parent(return_def, |this| this.visit_fn_ret_ty(output));
232
233                // If this async fn has no body (i.e. it's an async fn signature in a trait)
234                // then the closure_def will never be used, and we should avoid generating a
235                // def-id for it.
236                if let Some(body) = body {
237                    let closure_def = self
238                        .create_def(coroutine_kind.closure_id(), None, DefKind::Closure, span)
239                        .def_id();
240                    self.with_parent(closure_def, |this| this.visit_block(body));
241                }
242            }
243            FnKind::Closure(binder, Some(coroutine_kind), decl, body) => {
244                self.visit_closure_binder(binder);
245                visit::walk_fn_decl(self, decl);
246
247                // Async closures desugar to closures inside of closures, so
248                // we must create two defs.
249                let coroutine_def = self
250                    .create_def(coroutine_kind.closure_id(), None, DefKind::Closure, span)
251                    .def_id();
252                self.with_parent(coroutine_def, |this| this.visit_expr(body));
253            }
254            _ => visit::walk_fn(self, fn_kind),
255        }
256    }
257
258    fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
259        let (ident, def_kind) = match fi.kind {
260            ForeignItemKind::Static(box StaticItem {
261                ident,
262                ty: _,
263                mutability,
264                expr: _,
265                safety,
266                define_opaque: _,
267                eii_impls: _,
268            }) => {
269                let safety = match safety {
270                    ast::Safety::Unsafe(_) | ast::Safety::Default => hir::Safety::Unsafe,
271                    ast::Safety::Safe(_) => hir::Safety::Safe,
272                };
273
274                (ident, DefKind::Static { safety, mutability, nested: false })
275            }
276            ForeignItemKind::Fn(box Fn { ident, .. }) => (ident, DefKind::Fn),
277            ForeignItemKind::TyAlias(box TyAlias { ident, .. }) => (ident, DefKind::ForeignTy),
278            ForeignItemKind::MacCall(_) => {
279                self.visit_invoc_in_module(fi.id);
280                self.visit_macro_invoc(fi.id);
281                return;
282            }
283        };
284
285        let def = self.create_def(fi.id, Some(ident.name), def_kind, fi.span);
286
287        self.with_parent(def.def_id(), |this| {
288            this.build_reduced_graph_for_foreign_item(fi, ident, def);
289            visit::walk_item(this, fi)
290        });
291    }
292
293    fn visit_variant(&mut self, v: &'a Variant) {
294        if v.is_placeholder {
295            self.visit_macro_invoc(v.id);
296            self.visit_invoc_in_module(v.id);
297            return;
298        }
299        let feed = self.create_def(v.id, Some(v.ident.name), DefKind::Variant, v.span);
300        self.with_parent(feed.def_id(), |this| this.brg_visit_variant(v, feed));
301    }
302
303    fn visit_where_predicate(&mut self, pred: &'a WherePredicate) {
304        if pred.is_placeholder {
305            self.visit_macro_invoc(pred.id);
306            self.visit_invoc(pred.id);
307        } else {
308            visit::walk_where_predicate(self, pred)
309        }
310    }
311
312    fn visit_variant_data(&mut self, data: &'a VariantData) {
313        // The assumption here is that non-`cfg` macro expansion cannot change field indices.
314        // It currently holds because only inert attributes are accepted on fields,
315        // and every such attribute expands into a single field after it's resolved.
316        for (index, field) in data.fields().iter().enumerate() {
317            self.collect_field(field, Some(index));
318        }
319    }
320
321    fn visit_generic_param(&mut self, param: &'a GenericParam) {
322        if param.is_placeholder {
323            self.visit_macro_invoc(param.id);
324            self.visit_invoc(param.id);
325            return;
326        }
327        let def_kind = match param.kind {
328            GenericParamKind::Lifetime { .. } => DefKind::LifetimeParam,
329            GenericParamKind::Type { .. } => DefKind::TyParam,
330            GenericParamKind::Const { .. } => DefKind::ConstParam,
331        };
332        self.create_def(param.id, Some(param.ident.name), def_kind, param.ident.span);
333
334        // impl-Trait can happen inside generic parameters, like
335        // ```
336        // fn foo<U: Iterator<Item = impl Clone>>() {}
337        // ```
338        //
339        // In that case, the impl-trait is lowered as an additional generic parameter.
340        self.with_impl_trait(ImplTraitContext::Universal, |this| {
341            visit::walk_generic_param(this, param)
342        });
343    }
344
345    fn visit_assoc_item(&mut self, i: &'a AssocItem, ctxt: visit::AssocCtxt) {
346        let (ident, def_kind, ns) = match &i.kind {
347            AssocItemKind::Fn(box Fn { ident, .. })
348            | AssocItemKind::Delegation(box Delegation { ident, .. }) => {
349                (*ident, DefKind::AssocFn, ValueNS)
350            }
351            AssocItemKind::Const(box ConstItem { ident, rhs_kind, .. }) => (
352                *ident,
353                DefKind::AssocConst {
354                    is_type_const: #[allow(non_exhaustive_omitted_patterns)] match rhs_kind {
    ConstItemRhsKind::TypeConst { .. } => true,
    _ => false,
}matches!(rhs_kind, ConstItemRhsKind::TypeConst { .. }),
355                },
356                ValueNS,
357            ),
358            AssocItemKind::Type(box TyAlias { ident, .. }) => (*ident, DefKind::AssocTy, TypeNS),
359            AssocItemKind::MacCall(..) => {
360                self.visit_macro_invoc(i.id);
361                self.visit_assoc_item_mac_call(i, ctxt);
362                return;
363            }
364            AssocItemKind::DelegationMac(..) => {
365                ::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")
366            }
367        };
368
369        let feed = self.create_def(i.id, Some(ident.name), def_kind, i.span);
370        self.with_parent(feed.def_id(), |this| this.brg_visit_assoc_item(i, ctxt, ident, ns, feed));
371    }
372
373    fn visit_pat(&mut self, pat: &'a Pat) {
374        match pat.kind {
375            PatKind::MacCall(..) => {
376                self.visit_macro_invoc(pat.id);
377                self.visit_invoc(pat.id);
378            }
379            _ => visit::walk_pat(self, pat),
380        }
381    }
382
383    fn visit_anon_const(&mut self, constant: &'a AnonConst) {
384        // `MgcaDisambiguation::Direct` is set even when MGCA is disabled, so
385        // to avoid affecting stable we have to feature gate the not creating
386        // anon consts
387        if !self.r.tcx.features().min_generic_const_args() {
388            let parent = self
389                .create_def(constant.id, None, DefKind::AnonConst, constant.value.span)
390                .def_id();
391            return self.with_parent(parent, |this| visit::walk_anon_const(this, constant));
392        }
393
394        match constant.mgca_disambiguation {
395            MgcaDisambiguation::Direct => self.with_const_arg(ConstArgContext::Direct, |this| {
396                visit::walk_anon_const(this, constant);
397            }),
398            MgcaDisambiguation::AnonConst => {
399                self.with_const_arg(ConstArgContext::NonDirect, |this| {
400                    let parent = this
401                        .create_def(constant.id, None, DefKind::AnonConst, constant.value.span)
402                        .def_id();
403                    this.with_parent(parent, |this| visit::walk_anon_const(this, constant));
404                })
405            }
406        };
407    }
408
409    #[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(409u32),
                                    ::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:411",
                                    "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(411u32),
                                    ::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))]
410    fn visit_expr(&mut self, expr: &'a Expr) {
411        debug!(?self.invocation_parent);
412
413        let parent_def = match &expr.kind {
414            ExprKind::MacCall(..) => {
415                self.visit_macro_invoc(expr.id);
416                self.visit_invoc(expr.id);
417                return;
418            }
419            ExprKind::Closure(..) | ExprKind::Gen(..) => {
420                self.create_def(expr.id, None, DefKind::Closure, expr.span).def_id()
421            }
422            ExprKind::ConstBlock(constant) => {
423                // Under `min_generic_const_args` a `const { }` block sometimes
424                // corresponds to an anon const rather than an inline const.
425                let def_kind = match self.invocation_parent.const_arg_context {
426                    ConstArgContext::Direct => DefKind::AnonConst,
427                    ConstArgContext::NonDirect => DefKind::InlineConst,
428                };
429
430                return self.with_const_arg(ConstArgContext::NonDirect, |this| {
431                    for attr in &expr.attrs {
432                        visit::walk_attribute(this, attr);
433                    }
434
435                    let def =
436                        this.create_def(constant.id, None, def_kind, constant.value.span).def_id();
437                    this.with_parent(def, |this| visit::walk_anon_const(this, constant));
438                });
439            }
440
441            // Avoid overwriting `const_arg_context` as we may want to treat const blocks
442            // as being anon consts if we are inside a const argument.
443            ExprKind::Struct(_) | ExprKind::Call(..) | ExprKind::Tup(..) | ExprKind::Array(..) => {
444                return visit::walk_expr(self, expr);
445            }
446            // FIXME(mgca): we may want to handle block labels in some manner
447            ExprKind::Block(block, _) if let [stmt] = block.stmts.as_slice() => match stmt.kind {
448                // FIXME(mgca): this probably means that mac calls that expand
449                // to semi'd const blocks are handled differently to just writing
450                // out a semi'd const block.
451                StmtKind::Expr(..) | StmtKind::MacCall(..) => return visit::walk_expr(self, expr),
452
453                // Fallback to normal behaviour
454                StmtKind::Let(..) | StmtKind::Item(..) | StmtKind::Semi(..) | StmtKind::Empty => {
455                    self.invocation_parent.parent_def
456                }
457            },
458
459            _ => self.invocation_parent.parent_def,
460        };
461
462        self.with_const_arg(ConstArgContext::NonDirect, |this| {
463            // Note in some cases the `parent_def` here may be the existing parent
464            // and this is actually a no-op `with_parent` call.
465            this.with_parent(parent_def, |this| visit::walk_expr(this, expr))
466        })
467    }
468
469    fn visit_ty(&mut self, ty: &'a Ty) {
470        match ty.kind {
471            TyKind::MacCall(..) => {
472                self.visit_macro_invoc(ty.id);
473                self.visit_invoc(ty.id);
474            }
475            TyKind::ImplTrait(opaque_id, _) => {
476                let name = *self
477                    .r
478                    .impl_trait_names
479                    .get(&ty.id)
480                    .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"));
481                let kind = match self.invocation_parent.impl_trait_context {
482                    ImplTraitContext::Universal => DefKind::TyParam,
483                    ImplTraitContext::Existential => DefKind::OpaqueTy,
484                    ImplTraitContext::InBinding => return visit::walk_ty(self, ty),
485                };
486                let id = self.create_def(opaque_id, Some(name), kind, ty.span).def_id();
487                match self.invocation_parent.impl_trait_context {
488                    // Do not nest APIT, as we desugar them as `impl_trait: bounds`,
489                    // so the `impl_trait` node is not a parent to `bounds`.
490                    ImplTraitContext::Universal => visit::walk_ty(self, ty),
491                    ImplTraitContext::Existential => {
492                        self.with_parent(id, |this| visit::walk_ty(this, ty))
493                    }
494                    ImplTraitContext::InBinding => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
495                };
496            }
497            _ => visit::walk_ty(self, ty),
498        }
499    }
500
501    fn visit_stmt(&mut self, stmt: &'a Stmt) {
502        match stmt.kind {
503            StmtKind::MacCall(..) => {
504                self.brg_visit_mac_call_in_module(stmt.id);
505                self.visit_macro_invoc(stmt.id)
506            }
507            // FIXME(impl_trait_in_bindings): We don't really have a good way of
508            // introducing the right `ImplTraitContext` here for all the cases we
509            // care about, in case we want to introduce ITIB to other positions
510            // such as turbofishes (e.g. `foo::<impl Fn()>(|| {})`).
511            StmtKind::Let(ref local) => self.with_impl_trait(ImplTraitContext::InBinding, |this| {
512                visit::walk_local(this, local)
513            }),
514            _ => visit::walk_stmt(self, stmt),
515        }
516    }
517
518    fn visit_arm(&mut self, arm: &'a Arm) {
519        if arm.is_placeholder {
520            self.visit_macro_invoc(arm.id);
521            self.visit_invoc(arm.id);
522        } else {
523            visit::walk_arm(self, arm)
524        }
525    }
526
527    fn visit_expr_field(&mut self, f: &'a ExprField) {
528        if f.is_placeholder {
529            self.visit_macro_invoc(f.id);
530            self.visit_invoc(f.id);
531        } else {
532            visit::walk_expr_field(self, f)
533        }
534    }
535
536    fn visit_pat_field(&mut self, fp: &'a PatField) {
537        if fp.is_placeholder {
538            self.visit_macro_invoc(fp.id);
539            self.visit_invoc(fp.id);
540        } else {
541            visit::walk_pat_field(self, fp)
542        }
543    }
544
545    fn visit_param(&mut self, p: &'a Param) {
546        if p.is_placeholder {
547            self.visit_macro_invoc(p.id);
548            self.visit_invoc(p.id);
549        } else {
550            self.with_impl_trait(ImplTraitContext::Universal, |this| visit::walk_param(this, p))
551        }
552    }
553
554    // This method is called only when we are visiting an individual field
555    // after expanding an attribute on it.
556    fn visit_field_def(&mut self, field: &'a FieldDef) {
557        self.collect_field(field, None);
558    }
559
560    fn visit_crate(&mut self, krate: &'a Crate) {
561        if krate.is_placeholder {
562            self.visit_macro_invoc(krate.id);
563            self.visit_invoc_in_module(krate.id);
564        } else {
565            // Visit attributes after items for backward compatibility.
566            // This way they can use `macro_rules` defined later.
567            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);
568            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);
569            self.contains_macro_use(&krate.attrs);
570        }
571    }
572
573    fn visit_attribute(&mut self, attr: &'a Attribute) {
574        let orig_in_attr = mem::replace(&mut self.invocation_parent.in_attr, true);
575        if !attr.is_doc_comment() && attr::is_builtin_attr(attr) {
576            self.r
577                .builtin_attrs
578                .push((attr.get_normal_item().path.segments[0].ident, self.parent_scope));
579        }
580        visit::walk_attribute(self, attr);
581        self.invocation_parent.in_attr = orig_in_attr;
582    }
583
584    fn visit_inline_asm(&mut self, asm: &'a InlineAsm) {
585        let InlineAsm {
586            asm_macro: _,
587            template: _,
588            template_strs: _,
589            operands,
590            clobber_abis: _,
591            options: _,
592            line_spans: _,
593        } = asm;
594        for (op, _span) in operands {
595            match op {
596                InlineAsmOperand::In { expr, reg: _ }
597                | InlineAsmOperand::Out { expr: Some(expr), reg: _, late: _ }
598                | InlineAsmOperand::InOut { expr, reg: _, late: _ } => {
599                    self.visit_expr(expr);
600                }
601                InlineAsmOperand::Out { expr: None, reg: _, late: _ } => {}
602                InlineAsmOperand::SplitInOut { in_expr, out_expr, reg: _, late: _ } => {
603                    self.visit_expr(in_expr);
604                    if let Some(expr) = out_expr {
605                        self.visit_expr(expr);
606                    }
607                }
608                InlineAsmOperand::Const { anon_const } => {
609                    let def = self
610                        .create_def(
611                            anon_const.id,
612                            None,
613                            DefKind::InlineConst,
614                            anon_const.value.span,
615                        )
616                        .def_id();
617                    self.with_parent(def, |this| visit::walk_anon_const(this, anon_const));
618                }
619                InlineAsmOperand::Sym { sym } => self.visit_inline_asm_sym(sym),
620                InlineAsmOperand::Label { block } => self.visit_block(block),
621            }
622        }
623    }
624}