rustc_hir/
definitions.rs

1//! For each definition, we track the following data. A definition
2//! here is defined somewhat circularly as "something with a `DefId`",
3//! but it generally corresponds to things like structs, enums, etc.
4//! There are also some rather random cases (like const initializer
5//! expressions) that are mostly just leftovers.
6
7use std::fmt::{self, Write};
8use std::hash::Hash;
9
10use rustc_data_structures::stable_hasher::StableHasher;
11use rustc_data_structures::unord::UnordMap;
12use rustc_hashes::Hash64;
13use rustc_index::IndexVec;
14use rustc_macros::{BlobDecodable, Decodable, Encodable};
15use rustc_span::{Symbol, kw, sym};
16use tracing::{debug, instrument};
17
18pub use crate::def_id::DefPathHash;
19use crate::def_id::{CRATE_DEF_INDEX, CrateNum, DefIndex, LOCAL_CRATE, LocalDefId, StableCrateId};
20use crate::def_path_hash_map::DefPathHashMap;
21
22/// The `DefPathTable` maps `DefIndex`es to `DefKey`s and vice versa.
23/// Internally the `DefPathTable` holds a tree of `DefKey`s, where each `DefKey`
24/// stores the `DefIndex` of its parent.
25/// There is one `DefPathTable` for each crate.
26#[derive(#[automatically_derived]
impl ::core::fmt::Debug for DefPathTable {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field4_finish(f, "DefPathTable",
            "stable_crate_id", &self.stable_crate_id, "index_to_key",
            &self.index_to_key, "def_path_hashes", &self.def_path_hashes,
            "def_path_hash_to_index", &&self.def_path_hash_to_index)
    }
}Debug)]
27pub struct DefPathTable {
28    stable_crate_id: StableCrateId,
29    index_to_key: IndexVec<DefIndex, DefKey>,
30    // We do only store the local hash, as all the definitions are from the current crate.
31    def_path_hashes: IndexVec<DefIndex, Hash64>,
32    def_path_hash_to_index: DefPathHashMap,
33}
34
35impl DefPathTable {
36    fn new(stable_crate_id: StableCrateId) -> DefPathTable {
37        DefPathTable {
38            stable_crate_id,
39            index_to_key: Default::default(),
40            def_path_hashes: Default::default(),
41            def_path_hash_to_index: Default::default(),
42        }
43    }
44
45    fn allocate(&mut self, key: DefKey, def_path_hash: DefPathHash) -> DefIndex {
46        // Assert that all DefPathHashes correctly contain the local crate's StableCrateId.
47        if true {
    match (&self.stable_crate_id, &def_path_hash.stable_crate_id()) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_eq!(self.stable_crate_id, def_path_hash.stable_crate_id());
48        let local_hash = def_path_hash.local_hash();
49
50        let index = self.index_to_key.push(key);
51        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir/src/definitions.rs:51",
                        "rustc_hir::definitions", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir/src/definitions.rs"),
                        ::tracing_core::__macro_support::Option::Some(51u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir::definitions"),
                        ::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!("DefPathTable::insert() - {0:?} <-> {1:?}",
                                                    key, index) as &dyn Value))])
            });
    } else { ; }
};debug!("DefPathTable::insert() - {key:?} <-> {index:?}");
52
53        self.def_path_hashes.push(local_hash);
54        if true {
    if !(self.def_path_hashes.len() == self.index_to_key.len()) {
        ::core::panicking::panic("assertion failed: self.def_path_hashes.len() == self.index_to_key.len()")
    };
};debug_assert!(self.def_path_hashes.len() == self.index_to_key.len());
55
56        // Check for hash collisions of DefPathHashes. These should be
57        // exceedingly rare.
58        if let Some(existing) = self.def_path_hash_to_index.insert(&local_hash, &index) {
59            let def_path1 = DefPath::make(LOCAL_CRATE, existing, |idx| self.def_key(idx));
60            let def_path2 = DefPath::make(LOCAL_CRATE, index, |idx| self.def_key(idx));
61
62            // Continuing with colliding DefPathHashes can lead to correctness
63            // issues. We must abort compilation.
64            //
65            // The likelihood of such a collision is very small, so actually
66            // running into one could be indicative of a poor hash function
67            // being used.
68            //
69            // See the documentation for DefPathHash for more information.
70            {
    ::core::panicking::panic_fmt(format_args!("found DefPathHash collision between {0:#?} and {1:#?}. Compilation cannot continue.",
            def_path1, def_path2));
};panic!(
71                "found DefPathHash collision between {def_path1:#?} and {def_path2:#?}. \
72                    Compilation cannot continue."
73            );
74        }
75
76        index
77    }
78
79    #[inline(always)]
80    pub fn def_key(&self, index: DefIndex) -> DefKey {
81        self.index_to_key[index]
82    }
83
84    x;#[instrument(level = "trace", skip(self), ret)]
85    #[inline(always)]
86    pub fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
87        let hash = self.def_path_hashes[index];
88        DefPathHash::new(self.stable_crate_id, hash)
89    }
90
91    pub fn enumerated_keys_and_path_hashes(
92        &self,
93    ) -> impl Iterator<Item = (DefIndex, &DefKey, DefPathHash)> + ExactSizeIterator {
94        self.index_to_key
95            .iter_enumerated()
96            .map(move |(index, key)| (index, key, self.def_path_hash(index)))
97    }
98}
99
100#[derive(#[automatically_derived]
impl ::core::fmt::Debug for DisambiguatorState {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field1_finish(f,
            "DisambiguatorState", "next", &&self.next)
    }
}Debug)]
101pub struct DisambiguatorState {
102    next: UnordMap<(LocalDefId, DefPathData), u32>,
103}
104
105impl DisambiguatorState {
106    pub const fn new() -> Self {
107        Self { next: Default::default() }
108    }
109
110    /// Creates a `DisambiguatorState` where the next allocated `(LocalDefId, DefPathData)` pair
111    /// will have `index` as the disambiguator.
112    pub fn with(def_id: LocalDefId, data: DefPathData, index: u32) -> Self {
113        let mut this = Self::new();
114        this.next.insert((def_id, data), index);
115        this
116    }
117}
118
119/// The definition table containing node definitions.
120/// It holds the `DefPathTable` for `LocalDefId`s/`DefPath`s.
121/// It also stores mappings to convert `LocalDefId`s to/from `HirId`s.
122#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Definitions {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field1_finish(f, "Definitions",
            "table", &&self.table)
    }
}Debug)]
123pub struct Definitions {
124    table: DefPathTable,
125}
126
127/// A unique identifier that we can use to lookup a definition
128/// precisely. It combines the index of the definition's parent (if
129/// any) with a `DisambiguatedDefPathData`.
130#[derive(#[automatically_derived]
impl ::core::marker::Copy for DefKey { }Copy, #[automatically_derived]
impl ::core::clone::Clone for DefKey {
    #[inline]
    fn clone(&self) -> DefKey {
        let _: ::core::clone::AssertParamIsClone<Option<DefIndex>>;
        let _: ::core::clone::AssertParamIsClone<DisambiguatedDefPathData>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for DefKey {
    #[inline]
    fn eq(&self, other: &DefKey) -> bool {
        self.parent == other.parent &&
            self.disambiguated_data == other.disambiguated_data
    }
}PartialEq, #[automatically_derived]
impl ::core::fmt::Debug for DefKey {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "DefKey",
            "parent", &self.parent, "disambiguated_data",
            &&self.disambiguated_data)
    }
}Debug, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for DefKey {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    DefKey {
                        parent: ref __binding_0, disambiguated_data: ref __binding_1
                        } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                    }
                }
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::BlobDecoder> ::rustc_serialize::Decodable<__D>
            for DefKey {
            fn decode(__decoder: &mut __D) -> Self {
                DefKey {
                    parent: ::rustc_serialize::Decodable::decode(__decoder),
                    disambiguated_data: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };BlobDecodable)]
131pub struct DefKey {
132    /// The parent path.
133    pub parent: Option<DefIndex>,
134
135    /// The identifier of this node.
136    pub disambiguated_data: DisambiguatedDefPathData,
137}
138
139impl DefKey {
140    pub(crate) fn compute_stable_hash(&self, parent: DefPathHash) -> DefPathHash {
141        let mut hasher = StableHasher::new();
142
143        // The new path is in the same crate as `parent`, and will contain the stable_crate_id.
144        // Therefore, we only need to include information of the parent's local hash.
145        parent.local_hash().hash(&mut hasher);
146
147        let DisambiguatedDefPathData { ref data, disambiguator } = self.disambiguated_data;
148
149        std::mem::discriminant(data).hash(&mut hasher);
150        if let Some(name) = data.hashed_symbol() {
151            // Get a stable hash by considering the symbol chars rather than
152            // the symbol index.
153            name.as_str().hash(&mut hasher);
154        }
155
156        disambiguator.hash(&mut hasher);
157
158        let local_hash = hasher.finish();
159
160        // Construct the new DefPathHash, making sure that the `crate_id`
161        // portion of the hash is properly copied from the parent. This way the
162        // `crate_id` part will be recursively propagated from the root to all
163        // DefPathHashes in this DefPathTable.
164        DefPathHash::new(parent.stable_crate_id(), local_hash)
165    }
166
167    #[inline]
168    pub fn get_opt_name(&self) -> Option<Symbol> {
169        self.disambiguated_data.data.get_opt_name()
170    }
171}
172
173/// A pair of `DefPathData` and an integer disambiguator. The integer is
174/// normally `0`, but in the event that there are multiple defs with the
175/// same `parent` and `data`, we use this field to disambiguate
176/// between them. This introduces some artificial ordering dependency
177/// but means that if you have, e.g., two impls for the same type in
178/// the same module, they do get distinct `DefId`s.
179#[derive(#[automatically_derived]
impl ::core::marker::Copy for DisambiguatedDefPathData { }Copy, #[automatically_derived]
impl ::core::clone::Clone for DisambiguatedDefPathData {
    #[inline]
    fn clone(&self) -> DisambiguatedDefPathData {
        let _: ::core::clone::AssertParamIsClone<DefPathData>;
        let _: ::core::clone::AssertParamIsClone<u32>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for DisambiguatedDefPathData {
    #[inline]
    fn eq(&self, other: &DisambiguatedDefPathData) -> bool {
        self.disambiguator == other.disambiguator && self.data == other.data
    }
}PartialEq, #[automatically_derived]
impl ::core::fmt::Debug for DisambiguatedDefPathData {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "DisambiguatedDefPathData", "data", &self.data, "disambiguator",
            &&self.disambiguator)
    }
}Debug, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for DisambiguatedDefPathData {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    DisambiguatedDefPathData {
                        data: ref __binding_0, disambiguator: ref __binding_1 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                    }
                }
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::BlobDecoder> ::rustc_serialize::Decodable<__D>
            for DisambiguatedDefPathData {
            fn decode(__decoder: &mut __D) -> Self {
                DisambiguatedDefPathData {
                    data: ::rustc_serialize::Decodable::decode(__decoder),
                    disambiguator: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };BlobDecodable)]
180pub struct DisambiguatedDefPathData {
181    pub data: DefPathData,
182    pub disambiguator: u32,
183}
184
185impl DisambiguatedDefPathData {
186    pub fn as_sym(&self, verbose: bool) -> Symbol {
187        match self.data.name() {
188            DefPathDataName::Named(name) => {
189                if verbose && self.disambiguator != 0 {
190                    Symbol::intern(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}#{1}", name,
                self.disambiguator))
    })format!("{}#{}", name, self.disambiguator))
191                } else {
192                    name
193                }
194            }
195            DefPathDataName::Anon { namespace } => {
196                if let DefPathData::AnonAssocTy(method) = self.data {
197                    Symbol::intern(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}::{{{1}#{2}}}", method,
                namespace, self.disambiguator))
    })format!("{}::{{{}#{}}}", method, namespace, self.disambiguator))
198                } else {
199                    Symbol::intern(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{{{0}#{1}}}", namespace,
                self.disambiguator))
    })format!("{{{}#{}}}", namespace, self.disambiguator))
200                }
201            }
202        }
203    }
204}
205
206#[derive(#[automatically_derived]
impl ::core::clone::Clone for DefPath {
    #[inline]
    fn clone(&self) -> DefPath {
        DefPath {
            data: ::core::clone::Clone::clone(&self.data),
            krate: ::core::clone::Clone::clone(&self.krate),
        }
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for DefPath {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "DefPath",
            "data", &self.data, "krate", &&self.krate)
    }
}Debug, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for DefPath {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    DefPath { data: ref __binding_0, krate: ref __binding_1 } =>
                        {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                    }
                }
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
            for DefPath {
            fn decode(__decoder: &mut __D) -> Self {
                DefPath {
                    data: ::rustc_serialize::Decodable::decode(__decoder),
                    krate: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };Decodable)]
207pub struct DefPath {
208    /// The path leading from the crate root to the item.
209    pub data: Vec<DisambiguatedDefPathData>,
210
211    /// The crate root this path is relative to.
212    pub krate: CrateNum,
213}
214
215impl DefPath {
216    pub fn make<FN>(krate: CrateNum, start_index: DefIndex, mut get_key: FN) -> DefPath
217    where
218        FN: FnMut(DefIndex) -> DefKey,
219    {
220        let mut data = ::alloc::vec::Vec::new()vec![];
221        let mut index = Some(start_index);
222        loop {
223            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir/src/definitions.rs:223",
                        "rustc_hir::definitions", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir/src/definitions.rs"),
                        ::tracing_core::__macro_support::Option::Some(223u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir::definitions"),
                        ::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!("DefPath::make: krate={0:?} index={1:?}",
                                                    krate, index) as &dyn Value))])
            });
    } else { ; }
};debug!("DefPath::make: krate={:?} index={:?}", krate, index);
224            let p = index.unwrap();
225            let key = get_key(p);
226            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir/src/definitions.rs:226",
                        "rustc_hir::definitions", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir/src/definitions.rs"),
                        ::tracing_core::__macro_support::Option::Some(226u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir::definitions"),
                        ::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!("DefPath::make: key={0:?}",
                                                    key) as &dyn Value))])
            });
    } else { ; }
};debug!("DefPath::make: key={:?}", key);
227            match key.disambiguated_data.data {
228                DefPathData::CrateRoot => {
229                    if !key.parent.is_none() {
    ::core::panicking::panic("assertion failed: key.parent.is_none()")
};assert!(key.parent.is_none());
230                    break;
231                }
232                _ => {
233                    data.push(key.disambiguated_data);
234                    index = key.parent;
235                }
236            }
237        }
238        data.reverse();
239        DefPath { data, krate }
240    }
241
242    /// Returns a string representation of the `DefPath` without
243    /// the crate-prefix. This method is useful if you don't have
244    /// a `TyCtxt` available.
245    pub fn to_string_no_crate_verbose(&self) -> String {
246        let mut s = String::with_capacity(self.data.len() * 16);
247
248        for component in &self.data {
249            s.write_fmt(format_args!("::{0}", component.as_sym(true)))write!(s, "::{}", component.as_sym(true)).unwrap();
250        }
251
252        s
253    }
254
255    /// Returns a filename-friendly string of the `DefPath`, without
256    /// the crate-prefix. This method is useful if you don't have
257    /// a `TyCtxt` available.
258    pub fn to_filename_friendly_no_crate(&self) -> String {
259        let mut s = String::with_capacity(self.data.len() * 16);
260
261        let mut opt_delimiter = None;
262        for component in &self.data {
263            s.extend(opt_delimiter);
264            opt_delimiter = Some('-');
265            s.write_fmt(format_args!("{0}", component.as_sym(true)))write!(s, "{}", component.as_sym(true)).unwrap();
266        }
267
268        s
269    }
270}
271
272/// New variants should only be added in synchronization with `enum DefKind`.
273#[derive(#[automatically_derived]
impl ::core::marker::Copy for DefPathData { }Copy, #[automatically_derived]
impl ::core::clone::Clone for DefPathData {
    #[inline]
    fn clone(&self) -> DefPathData {
        let _: ::core::clone::AssertParamIsClone<Symbol>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for DefPathData {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            DefPathData::CrateRoot =>
                ::core::fmt::Formatter::write_str(f, "CrateRoot"),
            DefPathData::Impl => ::core::fmt::Formatter::write_str(f, "Impl"),
            DefPathData::ForeignMod =>
                ::core::fmt::Formatter::write_str(f, "ForeignMod"),
            DefPathData::Use => ::core::fmt::Formatter::write_str(f, "Use"),
            DefPathData::GlobalAsm =>
                ::core::fmt::Formatter::write_str(f, "GlobalAsm"),
            DefPathData::TypeNs(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "TypeNs",
                    &__self_0),
            DefPathData::ValueNs(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "ValueNs", &__self_0),
            DefPathData::MacroNs(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "MacroNs", &__self_0),
            DefPathData::LifetimeNs(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "LifetimeNs", &__self_0),
            DefPathData::Closure =>
                ::core::fmt::Formatter::write_str(f, "Closure"),
            DefPathData::Ctor => ::core::fmt::Formatter::write_str(f, "Ctor"),
            DefPathData::AnonConst =>
                ::core::fmt::Formatter::write_str(f, "AnonConst"),
            DefPathData::LateAnonConst =>
                ::core::fmt::Formatter::write_str(f, "LateAnonConst"),
            DefPathData::DesugaredAnonymousLifetime =>
                ::core::fmt::Formatter::write_str(f,
                    "DesugaredAnonymousLifetime"),
            DefPathData::OpaqueTy =>
                ::core::fmt::Formatter::write_str(f, "OpaqueTy"),
            DefPathData::OpaqueLifetime(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "OpaqueLifetime", &__self_0),
            DefPathData::AnonAssocTy(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "AnonAssocTy", &__self_0),
            DefPathData::SyntheticCoroutineBody =>
                ::core::fmt::Formatter::write_str(f,
                    "SyntheticCoroutineBody"),
            DefPathData::NestedStatic =>
                ::core::fmt::Formatter::write_str(f, "NestedStatic"),
        }
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for DefPathData {
    #[inline]
    fn eq(&self, other: &DefPathData) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (DefPathData::TypeNs(__self_0), DefPathData::TypeNs(__arg1_0))
                    => __self_0 == __arg1_0,
                (DefPathData::ValueNs(__self_0),
                    DefPathData::ValueNs(__arg1_0)) => __self_0 == __arg1_0,
                (DefPathData::MacroNs(__self_0),
                    DefPathData::MacroNs(__arg1_0)) => __self_0 == __arg1_0,
                (DefPathData::LifetimeNs(__self_0),
                    DefPathData::LifetimeNs(__arg1_0)) => __self_0 == __arg1_0,
                (DefPathData::OpaqueLifetime(__self_0),
                    DefPathData::OpaqueLifetime(__arg1_0)) =>
                    __self_0 == __arg1_0,
                (DefPathData::AnonAssocTy(__self_0),
                    DefPathData::AnonAssocTy(__arg1_0)) => __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for DefPathData {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) -> () {
        let _: ::core::cmp::AssertParamIsEq<Symbol>;
    }
}Eq, #[automatically_derived]
impl ::core::hash::Hash for DefPathData {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        ::core::hash::Hash::hash(&__self_discr, state);
        match self {
            DefPathData::TypeNs(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
            DefPathData::ValueNs(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
            DefPathData::MacroNs(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
            DefPathData::LifetimeNs(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
            DefPathData::OpaqueLifetime(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
            DefPathData::AnonAssocTy(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
            _ => {}
        }
    }
}Hash, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for DefPathData {
            fn encode(&self, __encoder: &mut __E) {
                let disc =
                    match *self {
                        DefPathData::CrateRoot => { 0usize }
                        DefPathData::Impl => { 1usize }
                        DefPathData::ForeignMod => { 2usize }
                        DefPathData::Use => { 3usize }
                        DefPathData::GlobalAsm => { 4usize }
                        DefPathData::TypeNs(ref __binding_0) => { 5usize }
                        DefPathData::ValueNs(ref __binding_0) => { 6usize }
                        DefPathData::MacroNs(ref __binding_0) => { 7usize }
                        DefPathData::LifetimeNs(ref __binding_0) => { 8usize }
                        DefPathData::Closure => { 9usize }
                        DefPathData::Ctor => { 10usize }
                        DefPathData::AnonConst => { 11usize }
                        DefPathData::LateAnonConst => { 12usize }
                        DefPathData::DesugaredAnonymousLifetime => { 13usize }
                        DefPathData::OpaqueTy => { 14usize }
                        DefPathData::OpaqueLifetime(ref __binding_0) => { 15usize }
                        DefPathData::AnonAssocTy(ref __binding_0) => { 16usize }
                        DefPathData::SyntheticCoroutineBody => { 17usize }
                        DefPathData::NestedStatic => { 18usize }
                    };
                ::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
                match *self {
                    DefPathData::CrateRoot => {}
                    DefPathData::Impl => {}
                    DefPathData::ForeignMod => {}
                    DefPathData::Use => {}
                    DefPathData::GlobalAsm => {}
                    DefPathData::TypeNs(ref __binding_0) => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                    DefPathData::ValueNs(ref __binding_0) => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                    DefPathData::MacroNs(ref __binding_0) => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                    DefPathData::LifetimeNs(ref __binding_0) => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                    DefPathData::Closure => {}
                    DefPathData::Ctor => {}
                    DefPathData::AnonConst => {}
                    DefPathData::LateAnonConst => {}
                    DefPathData::DesugaredAnonymousLifetime => {}
                    DefPathData::OpaqueTy => {}
                    DefPathData::OpaqueLifetime(ref __binding_0) => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                    DefPathData::AnonAssocTy(ref __binding_0) => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                    DefPathData::SyntheticCoroutineBody => {}
                    DefPathData::NestedStatic => {}
                }
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::BlobDecoder> ::rustc_serialize::Decodable<__D>
            for DefPathData {
            fn decode(__decoder: &mut __D) -> Self {
                match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
                    {
                    0usize => { DefPathData::CrateRoot }
                    1usize => { DefPathData::Impl }
                    2usize => { DefPathData::ForeignMod }
                    3usize => { DefPathData::Use }
                    4usize => { DefPathData::GlobalAsm }
                    5usize => {
                        DefPathData::TypeNs(::rustc_serialize::Decodable::decode(__decoder))
                    }
                    6usize => {
                        DefPathData::ValueNs(::rustc_serialize::Decodable::decode(__decoder))
                    }
                    7usize => {
                        DefPathData::MacroNs(::rustc_serialize::Decodable::decode(__decoder))
                    }
                    8usize => {
                        DefPathData::LifetimeNs(::rustc_serialize::Decodable::decode(__decoder))
                    }
                    9usize => { DefPathData::Closure }
                    10usize => { DefPathData::Ctor }
                    11usize => { DefPathData::AnonConst }
                    12usize => { DefPathData::LateAnonConst }
                    13usize => { DefPathData::DesugaredAnonymousLifetime }
                    14usize => { DefPathData::OpaqueTy }
                    15usize => {
                        DefPathData::OpaqueLifetime(::rustc_serialize::Decodable::decode(__decoder))
                    }
                    16usize => {
                        DefPathData::AnonAssocTy(::rustc_serialize::Decodable::decode(__decoder))
                    }
                    17usize => { DefPathData::SyntheticCoroutineBody }
                    18usize => { DefPathData::NestedStatic }
                    n => {
                        ::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `DefPathData`, expected 0..19, actual {0}",
                                n));
                    }
                }
            }
        }
    };BlobDecodable)]
274pub enum DefPathData {
275    // Root: these should only be used for the root nodes, because
276    // they are treated specially by the `def_path` function.
277    /// The crate root (marker).
278    CrateRoot,
279
280    // Different kinds of items and item-like things:
281    /// An impl.
282    Impl,
283    /// An `extern` block.
284    ForeignMod,
285    /// A `use` item.
286    Use,
287    /// A global asm item.
288    GlobalAsm,
289    /// Something in the type namespace.
290    TypeNs(Symbol),
291    /// Something in the value namespace.
292    ValueNs(Symbol),
293    /// Something in the macro namespace.
294    MacroNs(Symbol),
295    /// Something in the lifetime namespace.
296    LifetimeNs(Symbol),
297    /// A closure expression.
298    Closure,
299
300    // Subportions of items:
301    /// Implicit constructor for a unit or tuple-like struct or enum variant.
302    Ctor,
303    /// A constant expression (see `{ast,hir}::AnonConst`).
304    AnonConst,
305    /// A constant expression created during AST->HIR lowering..
306    LateAnonConst,
307    /// A fresh anonymous lifetime created by desugaring elided lifetimes.
308    DesugaredAnonymousLifetime,
309    /// An existential `impl Trait` type node.
310    /// Argument position `impl Trait` have a `TypeNs` with their pretty-printed name.
311    OpaqueTy,
312    /// Used for remapped captured lifetimes in an existential `impl Trait` type node.
313    OpaqueLifetime(Symbol),
314    /// An anonymous associated type from an RPITIT. The symbol refers to the name of the method
315    /// that defined the type.
316    AnonAssocTy(Symbol),
317    /// A synthetic body for a coroutine's by-move body.
318    SyntheticCoroutineBody,
319    /// Additional static data referred to by a static.
320    NestedStatic,
321}
322
323impl Definitions {
324    pub fn def_path_table(&self) -> &DefPathTable {
325        &self.table
326    }
327
328    /// Gets the number of definitions.
329    pub fn def_index_count(&self) -> usize {
330        self.table.index_to_key.len()
331    }
332
333    #[inline]
334    pub fn def_key(&self, id: LocalDefId) -> DefKey {
335        self.table.def_key(id.local_def_index)
336    }
337
338    #[inline(always)]
339    pub fn def_path_hash(&self, id: LocalDefId) -> DefPathHash {
340        self.table.def_path_hash(id.local_def_index)
341    }
342
343    /// Returns the path from the crate root to `index`. The root
344    /// nodes are not included in the path (i.e., this will be an
345    /// empty vector for the crate root). For an inlined item, this
346    /// will be the path of the item in the external crate (but the
347    /// path will begin with the path to the external crate).
348    pub fn def_path(&self, id: LocalDefId) -> DefPath {
349        DefPath::make(LOCAL_CRATE, id.local_def_index, |index| {
350            self.def_key(LocalDefId { local_def_index: index })
351        })
352    }
353
354    /// Adds a root definition (no parent) and a few other reserved definitions.
355    pub fn new(stable_crate_id: StableCrateId) -> Definitions {
356        let key = DefKey {
357            parent: None,
358            disambiguated_data: DisambiguatedDefPathData {
359                data: DefPathData::CrateRoot,
360                disambiguator: 0,
361            },
362        };
363
364        // We want *both* halves of a DefPathHash to depend on the crate-id of the defining crate.
365        // The crate-id can be more easily changed than the DefPath of an item, so, in the case of
366        // a crate-local DefPathHash collision, the user can simply "roll the dice again" for all
367        // DefPathHashes in the crate by changing the crate disambiguator (e.g. via bumping the
368        // crate's version number).
369        //
370        // Children paths will only hash the local portion, and still inherit the change to the
371        // root hash.
372        let def_path_hash =
373            DefPathHash::new(stable_crate_id, Hash64::new(stable_crate_id.as_u64()));
374
375        // Create the root definition.
376        let mut table = DefPathTable::new(stable_crate_id);
377        let root = LocalDefId { local_def_index: table.allocate(key, def_path_hash) };
378        match (&root.local_def_index, &CRATE_DEF_INDEX) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(root.local_def_index, CRATE_DEF_INDEX);
379
380        Definitions { table }
381    }
382
383    /// Creates a definition with a parent definition.
384    /// If there are multiple definitions with the same DefPathData and the same parent, use
385    /// `disambiguator` to differentiate them. Distinct `DisambiguatorState` instances are not
386    /// guaranteed to generate unique disambiguators and should instead ensure that the `parent`
387    /// and `data` pair is distinct from other instances.
388    pub fn create_def(
389        &mut self,
390        parent: LocalDefId,
391        data: DefPathData,
392        disambiguator: &mut DisambiguatorState,
393    ) -> LocalDefId {
394        // We can't use `Debug` implementation for `LocalDefId` here, since it tries to acquire a
395        // reference to `Definitions` and we're already holding a mutable reference.
396        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir/src/definitions.rs:396",
                        "rustc_hir::definitions", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir/src/definitions.rs"),
                        ::tracing_core::__macro_support::Option::Some(396u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir::definitions"),
                        ::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(parent={0}, data={1:?})",
                                                    self.def_path(parent).to_string_no_crate_verbose(), data) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(
397            "create_def(parent={}, data={data:?})",
398            self.def_path(parent).to_string_no_crate_verbose(),
399        );
400
401        // The root node must be created in `new()`.
402        if !(data != DefPathData::CrateRoot) {
    ::core::panicking::panic("assertion failed: data != DefPathData::CrateRoot")
};assert!(data != DefPathData::CrateRoot);
403
404        // Find the next free disambiguator for this key.
405        let disambiguator = {
406            let next_disamb = disambiguator.next.entry((parent, data)).or_insert(0);
407            let disambiguator = *next_disamb;
408            *next_disamb = next_disamb.checked_add(1).expect("disambiguator overflow");
409            disambiguator
410        };
411        let key = DefKey {
412            parent: Some(parent.local_def_index),
413            disambiguated_data: DisambiguatedDefPathData { data, disambiguator },
414        };
415
416        let parent_hash = self.table.def_path_hash(parent.local_def_index);
417        let def_path_hash = key.compute_stable_hash(parent_hash);
418
419        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir/src/definitions.rs:419",
                        "rustc_hir::definitions", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir/src/definitions.rs"),
                        ::tracing_core::__macro_support::Option::Some(419u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir::definitions"),
                        ::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: after disambiguation, key = {0:?}",
                                                    key) as &dyn Value))])
            });
    } else { ; }
};debug!("create_def: after disambiguation, key = {:?}", key);
420
421        // Create the definition.
422        LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) }
423    }
424
425    #[inline(always)]
426    /// Returns `None` if the `DefPathHash` does not correspond to a `LocalDefId`
427    /// in the current compilation session. This can legitimately happen if the
428    /// `DefPathHash` is from a `DefId` in an upstream crate or, during incr. comp.,
429    /// if the `DefPathHash` is from a previous compilation session and
430    /// the def-path does not exist anymore.
431    pub fn local_def_path_hash_to_def_id(&self, hash: DefPathHash) -> Option<LocalDefId> {
432        if true {
    if !(hash.stable_crate_id() == self.table.stable_crate_id) {
        ::core::panicking::panic("assertion failed: hash.stable_crate_id() == self.table.stable_crate_id")
    };
};debug_assert!(hash.stable_crate_id() == self.table.stable_crate_id);
433        self.table
434            .def_path_hash_to_index
435            .get(&hash.local_hash())
436            .map(|local_def_index| LocalDefId { local_def_index })
437    }
438
439    pub fn def_path_hash_to_def_index_map(&self) -> &DefPathHashMap {
440        &self.table.def_path_hash_to_index
441    }
442
443    pub fn num_definitions(&self) -> usize {
444        self.table.def_path_hashes.len()
445    }
446}
447
448#[derive(#[automatically_derived]
impl ::core::marker::Copy for DefPathDataName { }Copy, #[automatically_derived]
impl ::core::clone::Clone for DefPathDataName {
    #[inline]
    fn clone(&self) -> DefPathDataName {
        let _: ::core::clone::AssertParamIsClone<Symbol>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for DefPathDataName {
    #[inline]
    fn eq(&self, other: &DefPathDataName) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (DefPathDataName::Named(__self_0),
                    DefPathDataName::Named(__arg1_0)) => __self_0 == __arg1_0,
                (DefPathDataName::Anon { namespace: __self_0 },
                    DefPathDataName::Anon { namespace: __arg1_0 }) =>
                    __self_0 == __arg1_0,
                _ => unsafe { ::core::intrinsics::unreachable() }
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::fmt::Debug for DefPathDataName {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            DefPathDataName::Named(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Named",
                    &__self_0),
            DefPathDataName::Anon { namespace: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f, "Anon",
                    "namespace", &__self_0),
        }
    }
}Debug)]
449pub enum DefPathDataName {
450    Named(Symbol),
451    Anon { namespace: Symbol },
452}
453
454impl DefPathData {
455    pub fn get_opt_name(&self) -> Option<Symbol> {
456        use self::DefPathData::*;
457        match *self {
458            TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name)
459            | OpaqueLifetime(name) => Some(name),
460
461            DesugaredAnonymousLifetime => Some(kw::UnderscoreLifetime),
462
463            Impl
464            | ForeignMod
465            | CrateRoot
466            | Use
467            | GlobalAsm
468            | Closure
469            | Ctor
470            | AnonConst
471            | LateAnonConst
472            | OpaqueTy
473            | AnonAssocTy(..)
474            | SyntheticCoroutineBody
475            | NestedStatic => None,
476        }
477    }
478
479    fn hashed_symbol(&self) -> Option<Symbol> {
480        use self::DefPathData::*;
481        match *self {
482            TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) | AnonAssocTy(name)
483            | OpaqueLifetime(name) => Some(name),
484
485            DesugaredAnonymousLifetime => Some(kw::UnderscoreLifetime),
486
487            Impl
488            | ForeignMod
489            | CrateRoot
490            | Use
491            | GlobalAsm
492            | Closure
493            | Ctor
494            | AnonConst
495            | LateAnonConst
496            | OpaqueTy
497            | SyntheticCoroutineBody
498            | NestedStatic => None,
499        }
500    }
501
502    pub fn name(&self) -> DefPathDataName {
503        use self::DefPathData::*;
504        match *self {
505            TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name)
506            | OpaqueLifetime(name) => DefPathDataName::Named(name),
507            // Note that this does not show up in user print-outs.
508            CrateRoot => DefPathDataName::Anon { namespace: kw::Crate },
509            Impl => DefPathDataName::Anon { namespace: kw::Impl },
510            ForeignMod => DefPathDataName::Anon { namespace: kw::Extern },
511            Use => DefPathDataName::Anon { namespace: kw::Use },
512            GlobalAsm => DefPathDataName::Anon { namespace: sym::global_asm },
513            Closure => DefPathDataName::Anon { namespace: sym::closure },
514            Ctor => DefPathDataName::Anon { namespace: sym::constructor },
515            AnonConst | LateAnonConst => DefPathDataName::Anon { namespace: sym::constant },
516            DesugaredAnonymousLifetime => DefPathDataName::Named(kw::UnderscoreLifetime),
517            OpaqueTy => DefPathDataName::Anon { namespace: sym::opaque },
518            AnonAssocTy(..) => DefPathDataName::Anon { namespace: sym::anon_assoc },
519            SyntheticCoroutineBody => DefPathDataName::Anon { namespace: sym::synthetic },
520            NestedStatic => DefPathDataName::Anon { namespace: sym::nested },
521        }
522    }
523}
524
525impl fmt::Display for DefPathData {
526    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
527        match self.name() {
528            DefPathDataName::Named(name) => f.write_str(name.as_str()),
529            // FIXME(#70334): this will generate legacy {{closure}}, {{impl}}, etc
530            DefPathDataName::Anon { namespace } => f.write_fmt(format_args!("{{{{{0}}}}}", namespace))write!(f, "{{{{{namespace}}}}}"),
531        }
532    }
533}