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