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