Skip to main content

rustc_span/
hygiene.rs

1//! Machinery for hygienic macros.
2//!
3//! Inspired by Matthew Flatt et al., “Macros That Work Together: Compile-Time Bindings, Partial
4//! Expansion, and Definition Contexts,” *Journal of Functional Programming* 22, no. 2
5//! (March 1, 2012): 181–216, <https://doi.org/10.1017/S0956796812000093>.
6
7// Hygiene data is stored in a global variable and accessed via TLS, which
8// means that accesses are somewhat expensive. (`HygieneData::with`
9// encapsulates a single access.) Therefore, on hot code paths it is worth
10// ensuring that multiple HygieneData accesses are combined into a single
11// `HygieneData::with`.
12//
13// This explains why `HygieneData`, `SyntaxContext` and `ExpnId` have interfaces
14// with a certain amount of redundancy in them. For example,
15// `SyntaxContext::outer_expn_data` combines `SyntaxContext::outer` and
16// `ExpnId::expn_data` so that two `HygieneData` accesses can be performed within
17// a single `HygieneData::with` call.
18//
19// It also explains why many functions appear in `HygieneData` and again in
20// `SyntaxContext` or `ExpnId`. For example, `HygieneData::outer` and
21// `SyntaxContext::outer` do the same thing, but the former is for use within a
22// `HygieneData::with` call while the latter is for use outside such a call.
23// When modifying this file it is important to understand this distinction,
24// because getting it wrong can lead to nested `HygieneData::with` calls that
25// trigger runtime aborts. (Fortunately these are obvious and easy to fix.)
26
27use std::hash::Hash;
28use std::sync::Arc;
29use std::{fmt, iter, mem};
30
31use rustc_data_structures::fingerprint::Fingerprint;
32use rustc_data_structures::fx::{FxHashMap, FxHashSet};
33use rustc_data_structures::stable_hasher::{
34    HashStable, HashStableContext, StableHasher, ToStableHashKey,
35};
36use rustc_data_structures::sync::Lock;
37use rustc_data_structures::unhash::UnhashMap;
38use rustc_hashes::Hash64;
39use rustc_index::IndexVec;
40use rustc_macros::{Decodable, Encodable, HashStable};
41use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
42use tracing::{debug, trace};
43
44use crate::def_id::{CRATE_DEF_ID, CrateNum, DefId, LOCAL_CRATE, StableCrateId};
45use crate::edition::Edition;
46use crate::source_map::SourceMap;
47use crate::symbol::{Symbol, kw, sym};
48use crate::{DUMMY_SP, Span, SpanDecoder, SpanEncoder, with_session_globals};
49
50/// A `SyntaxContext` represents a chain of pairs `(ExpnId, Transparency)` named "marks".
51///
52/// See <https://rustc-dev-guide.rust-lang.org/macro-expansion.html> for more explanation.
53#[derive(#[automatically_derived]
impl ::core::clone::Clone for SyntaxContext {
    #[inline]
    fn clone(&self) -> SyntaxContext {
        let _: ::core::clone::AssertParamIsClone<u32>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for SyntaxContext { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for SyntaxContext {
    #[inline]
    fn eq(&self, other: &SyntaxContext) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for SyntaxContext {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<u32>;
    }
}Eq, #[automatically_derived]
impl ::core::hash::Hash for SyntaxContext {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.0, state)
    }
}Hash)]
54pub struct SyntaxContext(u32);
55
56// To ensure correctness of incremental compilation,
57// `SyntaxContext` must not implement `Ord` or `PartialOrd`.
58// See https://github.com/rust-lang/rust/issues/90317.
59impl !Ord for SyntaxContext {}
60impl !PartialOrd for SyntaxContext {}
61
62/// If this part of two syntax contexts is equal, then the whole syntax contexts should be equal.
63/// The other fields are only for caching.
64pub type SyntaxContextKey = (SyntaxContext, ExpnId, Transparency);
65
66#[derive(#[automatically_derived]
impl ::core::clone::Clone for SyntaxContextData {
    #[inline]
    fn clone(&self) -> SyntaxContextData {
        let _: ::core::clone::AssertParamIsClone<ExpnId>;
        let _: ::core::clone::AssertParamIsClone<Transparency>;
        let _: ::core::clone::AssertParamIsClone<SyntaxContext>;
        let _: ::core::clone::AssertParamIsClone<Symbol>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for SyntaxContextData { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for SyntaxContextData {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        let names: &'static _ =
            &["outer_expn", "outer_transparency", "parent", "opaque",
                        "opaque_and_semiopaque", "dollar_crate_name"];
        let values: &[&dyn ::core::fmt::Debug] =
            &[&self.outer_expn, &self.outer_transparency, &self.parent,
                        &self.opaque, &self.opaque_and_semiopaque,
                        &&self.dollar_crate_name];
        ::core::fmt::Formatter::debug_struct_fields_finish(f,
            "SyntaxContextData", names, values)
    }
}Debug)]
67struct SyntaxContextData {
68    /// The last macro expansion in the chain.
69    /// (Here we say the most deeply nested macro expansion is the "outermost" expansion.)
70    outer_expn: ExpnId,
71    /// Transparency of the last macro expansion
72    outer_transparency: Transparency,
73    parent: SyntaxContext,
74    /// This context, but with all transparent and semi-opaque expansions filtered away.
75    opaque: SyntaxContext,
76    /// This context, but with all transparent expansions filtered away.
77    opaque_and_semiopaque: SyntaxContext,
78    /// Name of the crate to which `$crate` with this context would resolve.
79    dollar_crate_name: Symbol,
80}
81
82impl SyntaxContextData {
83    fn root() -> SyntaxContextData {
84        SyntaxContextData {
85            outer_expn: ExpnId::root(),
86            outer_transparency: Transparency::Opaque,
87            parent: SyntaxContext::root(),
88            opaque: SyntaxContext::root(),
89            opaque_and_semiopaque: SyntaxContext::root(),
90            dollar_crate_name: kw::DollarCrate,
91        }
92    }
93
94    fn key(&self) -> SyntaxContextKey {
95        (self.parent, self.outer_expn, self.outer_transparency)
96    }
97}
98
99impl ::std::fmt::Debug for ExpnIndex {
    fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
        fmt.write_fmt(format_args!("{0}", self.as_u32()))
    }
}rustc_index::newtype_index! {
100    /// A unique ID associated with a macro invocation and expansion.
101    #[orderable]
102    pub struct ExpnIndex {}
103}
104
105/// A unique ID associated with a macro invocation and expansion.
106#[derive(#[automatically_derived]
impl ::core::clone::Clone for ExpnId {
    #[inline]
    fn clone(&self) -> ExpnId {
        let _: ::core::clone::AssertParamIsClone<CrateNum>;
        let _: ::core::clone::AssertParamIsClone<ExpnIndex>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for ExpnId { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for ExpnId {
    #[inline]
    fn eq(&self, other: &ExpnId) -> bool {
        self.krate == other.krate && self.local_id == other.local_id
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for ExpnId {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<CrateNum>;
        let _: ::core::cmp::AssertParamIsEq<ExpnIndex>;
    }
}Eq, #[automatically_derived]
impl ::core::hash::Hash for ExpnId {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.krate, state);
        ::core::hash::Hash::hash(&self.local_id, state)
    }
}Hash)]
107pub struct ExpnId {
108    pub krate: CrateNum,
109    pub local_id: ExpnIndex,
110}
111
112impl fmt::Debug for ExpnId {
113    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114        // Generate crate_::{{expn_}}.
115        f.write_fmt(format_args!("{0:?}::{{{{expn{1}}}}}", self.krate,
        self.local_id.as_u32()))write!(f, "{:?}::{{{{expn{}}}}}", self.krate, self.local_id.as_u32())
116    }
117}
118
119impl ::std::fmt::Debug for LocalExpnId {
    fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
        fmt.write_fmt(format_args!("expn{0}", self.as_u32()))
    }
}rustc_index::newtype_index! {
120    /// A unique ID associated with a macro invocation and expansion.
121    #[debug_format = "expn{}"]
122    pub struct LocalExpnId {}
123}
124
125// To ensure correctness of incremental compilation,
126// `LocalExpnId` must not implement `Ord` or `PartialOrd`.
127// See https://github.com/rust-lang/rust/issues/90317.
128impl !Ord for LocalExpnId {}
129impl !PartialOrd for LocalExpnId {}
130
131/// A unique hash value associated to an expansion.
132#[derive(#[automatically_derived]
impl ::core::clone::Clone for ExpnHash {
    #[inline]
    fn clone(&self) -> ExpnHash {
        let _: ::core::clone::AssertParamIsClone<Fingerprint>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for ExpnHash { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for ExpnHash {
    #[inline]
    fn eq(&self, other: &ExpnHash) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for ExpnHash {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Fingerprint>;
    }
}Eq, #[automatically_derived]
impl ::core::hash::Hash for ExpnHash {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.0, state)
    }
}Hash, #[automatically_derived]
impl ::core::fmt::Debug for ExpnHash {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f, "ExpnHash",
            &&self.0)
    }
}Debug, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for ExpnHash {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    ExpnHash(ref __binding_0) => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                }
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
            for ExpnHash {
            fn decode(__decoder: &mut __D) -> Self {
                ExpnHash(::rustc_serialize::Decodable::decode(__decoder))
            }
        }
    };Decodable, const _: () =
    {
        impl ::rustc_data_structures::stable_hasher::HashStable for ExpnHash {
            #[inline]
            fn hash_stable<__Hcx: ::rustc_data_structures::stable_hasher::HashStableContext>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                match *self {
                    ExpnHash(ref __binding_0) => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                    }
                }
            }
        }
    };HashStable)]
133pub struct ExpnHash(Fingerprint);
134
135impl ExpnHash {
136    /// Returns the [StableCrateId] identifying the crate this [ExpnHash]
137    /// originates from.
138    #[inline]
139    pub fn stable_crate_id(self) -> StableCrateId {
140        StableCrateId(self.0.split().0)
141    }
142
143    /// Returns the crate-local part of the [ExpnHash].
144    ///
145    /// Used for assertions.
146    #[inline]
147    pub fn local_hash(self) -> Hash64 {
148        self.0.split().1
149    }
150
151    #[inline]
152    pub fn is_root(self) -> bool {
153        self.0 == Fingerprint::ZERO
154    }
155
156    /// Builds a new [ExpnHash] with the given [StableCrateId] and
157    /// `local_hash`, where `local_hash` must be unique within its crate.
158    fn new(stable_crate_id: StableCrateId, local_hash: Hash64) -> ExpnHash {
159        ExpnHash(Fingerprint::new(stable_crate_id.0, local_hash))
160    }
161}
162
163/// A property of a macro expansion that determines how identifiers
164/// produced by that expansion are resolved.
165#[derive(#[automatically_derived]
impl ::core::marker::Copy for Transparency { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Transparency {
    #[inline]
    fn clone(&self) -> Transparency { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for Transparency {
    #[inline]
    fn eq(&self, other: &Transparency) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for Transparency {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::cmp::PartialOrd for Transparency {
    #[inline]
    fn partial_cmp(&self, other: &Transparency)
        -> ::core::option::Option<::core::cmp::Ordering> {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        ::core::cmp::PartialOrd::partial_cmp(&__self_discr, &__arg1_discr)
    }
}PartialOrd, #[automatically_derived]
impl ::core::hash::Hash for Transparency {
    #[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)
    }
}Hash, #[automatically_derived]
impl ::core::fmt::Debug for Transparency {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                Transparency::Transparent => "Transparent",
                Transparency::SemiOpaque => "SemiOpaque",
                Transparency::Opaque => "Opaque",
            })
    }
}Debug, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for Transparency {
            fn encode(&self, __encoder: &mut __E) {
                let disc =
                    match *self {
                        Transparency::Transparent => { 0usize }
                        Transparency::SemiOpaque => { 1usize }
                        Transparency::Opaque => { 2usize }
                    };
                ::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
                match *self {
                    Transparency::Transparent => {}
                    Transparency::SemiOpaque => {}
                    Transparency::Opaque => {}
                }
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
            for Transparency {
            fn decode(__decoder: &mut __D) -> Self {
                match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
                    {
                    0usize => { Transparency::Transparent }
                    1usize => { Transparency::SemiOpaque }
                    2usize => { Transparency::Opaque }
                    n => {
                        ::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `Transparency`, expected 0..3, actual {0}",
                                n));
                    }
                }
            }
        }
    };Decodable)]
166#[derive(const _: () =
    {
        impl ::rustc_data_structures::stable_hasher::HashStable for
            Transparency {
            #[inline]
            fn hash_stable<__Hcx: ::rustc_data_structures::stable_hasher::HashStableContext>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                ::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
                match *self {
                    Transparency::Transparent => {}
                    Transparency::SemiOpaque => {}
                    Transparency::Opaque => {}
                }
            }
        }
    };HashStable)]
167pub enum Transparency {
168    /// Identifier produced by a transparent expansion is always resolved at call-site.
169    /// Call-site spans in procedural macros, hygiene opt-out in `macro` should use this.
170    Transparent,
171    /// Identifier produced by a semi-opaque expansion may be resolved
172    /// either at call-site or at definition-site.
173    /// If it's a local variable, label or `$crate` then it's resolved at def-site.
174    /// Otherwise it's resolved at call-site.
175    /// `macro_rules` macros behave like this, built-in macros currently behave like this too,
176    /// but that's an implementation detail.
177    SemiOpaque,
178    /// Identifier produced by an opaque expansion is always resolved at definition-site.
179    /// Def-site spans in procedural macros, identifiers from `macro` by default use this.
180    Opaque,
181}
182
183impl Transparency {
184    pub fn fallback(macro_rules: bool) -> Self {
185        if macro_rules { Transparency::SemiOpaque } else { Transparency::Opaque }
186    }
187}
188
189impl LocalExpnId {
190    /// The ID of the theoretical expansion that generates freshly parsed, unexpanded AST.
191    pub const ROOT: LocalExpnId = LocalExpnId::ZERO;
192
193    #[inline]
194    fn from_raw(idx: ExpnIndex) -> LocalExpnId {
195        LocalExpnId::from_u32(idx.as_u32())
196    }
197
198    #[inline]
199    pub fn as_raw(self) -> ExpnIndex {
200        ExpnIndex::from_u32(self.as_u32())
201    }
202
203    pub fn fresh_empty() -> LocalExpnId {
204        HygieneData::with(|data| {
205            let expn_id = data.local_expn_data.push(None);
206            let _eid = data.local_expn_hashes.push(ExpnHash(Fingerprint::ZERO));
207            if true {
    match (&expn_id, &_eid) {
        (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!(expn_id, _eid);
208            expn_id
209        })
210    }
211
212    pub fn fresh(mut expn_data: ExpnData, hcx: impl HashStableContext) -> LocalExpnId {
213        if true {
    match (&expn_data.parent.krate, &LOCAL_CRATE) {
        (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!(expn_data.parent.krate, LOCAL_CRATE);
214        let expn_hash = update_disambiguator(&mut expn_data, hcx);
215        HygieneData::with(|data| {
216            let expn_id = data.local_expn_data.push(Some(expn_data));
217            let _eid = data.local_expn_hashes.push(expn_hash);
218            if true {
    match (&expn_id, &_eid) {
        (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!(expn_id, _eid);
219            let _old_id = data.expn_hash_to_expn_id.insert(expn_hash, expn_id.to_expn_id());
220            if true {
    if !_old_id.is_none() {
        ::core::panicking::panic("assertion failed: _old_id.is_none()")
    };
};debug_assert!(_old_id.is_none());
221            expn_id
222        })
223    }
224
225    #[inline]
226    pub fn expn_data(self) -> ExpnData {
227        HygieneData::with(|data| data.local_expn_data(self).clone())
228    }
229
230    #[inline]
231    pub fn to_expn_id(self) -> ExpnId {
232        ExpnId { krate: LOCAL_CRATE, local_id: self.as_raw() }
233    }
234
235    #[inline]
236    pub fn set_expn_data(self, mut expn_data: ExpnData, hcx: impl HashStableContext) {
237        if true {
    match (&expn_data.parent.krate, &LOCAL_CRATE) {
        (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!(expn_data.parent.krate, LOCAL_CRATE);
238        let expn_hash = update_disambiguator(&mut expn_data, hcx);
239        HygieneData::with(|data| {
240            let old_expn_data = &mut data.local_expn_data[self];
241            if !old_expn_data.is_none() {
    {
        ::core::panicking::panic_fmt(format_args!("expansion data is reset for an expansion ID"));
    }
};assert!(old_expn_data.is_none(), "expansion data is reset for an expansion ID");
242            *old_expn_data = Some(expn_data);
243            if true {
    match (&data.local_expn_hashes[self].0, &Fingerprint::ZERO) {
        (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!(data.local_expn_hashes[self].0, Fingerprint::ZERO);
244            data.local_expn_hashes[self] = expn_hash;
245            let _old_id = data.expn_hash_to_expn_id.insert(expn_hash, self.to_expn_id());
246            if true {
    if !_old_id.is_none() {
        ::core::panicking::panic("assertion failed: _old_id.is_none()")
    };
};debug_assert!(_old_id.is_none());
247        });
248    }
249
250    #[inline]
251    pub fn is_descendant_of(self, ancestor: LocalExpnId) -> bool {
252        self.to_expn_id().is_descendant_of(ancestor.to_expn_id())
253    }
254
255    /// Returns span for the macro which originally caused this expansion to happen.
256    ///
257    /// Stops backtracing at include! boundary.
258    #[inline]
259    pub fn expansion_cause(self) -> Option<Span> {
260        self.to_expn_id().expansion_cause()
261    }
262}
263
264impl ExpnId {
265    /// The ID of the theoretical expansion that generates freshly parsed, unexpanded AST.
266    /// Invariant: we do not create any ExpnId with local_id == 0 and krate != 0.
267    pub const fn root() -> ExpnId {
268        ExpnId { krate: LOCAL_CRATE, local_id: ExpnIndex::ZERO }
269    }
270
271    #[inline]
272    pub fn expn_hash(self) -> ExpnHash {
273        HygieneData::with(|data| data.expn_hash(self))
274    }
275
276    #[inline]
277    pub fn from_hash(hash: ExpnHash) -> Option<ExpnId> {
278        HygieneData::with(|data| data.expn_hash_to_expn_id.get(&hash).copied())
279    }
280
281    #[inline]
282    pub fn as_local(self) -> Option<LocalExpnId> {
283        if self.krate == LOCAL_CRATE { Some(LocalExpnId::from_raw(self.local_id)) } else { None }
284    }
285
286    #[inline]
287    #[track_caller]
288    pub fn expect_local(self) -> LocalExpnId {
289        self.as_local().unwrap()
290    }
291
292    #[inline]
293    pub fn expn_data(self) -> ExpnData {
294        HygieneData::with(|data| data.expn_data(self).clone())
295    }
296
297    #[inline]
298    pub fn is_descendant_of(self, ancestor: ExpnId) -> bool {
299        // a few "fast path" cases to avoid locking HygieneData
300        if ancestor == ExpnId::root() || ancestor == self {
301            return true;
302        }
303        if ancestor.krate != self.krate {
304            return false;
305        }
306        HygieneData::with(|data| data.is_descendant_of(self, ancestor))
307    }
308
309    /// `expn_id.outer_expn_is_descendant_of(ctxt)` is equivalent to but faster than
310    /// `expn_id.is_descendant_of(ctxt.outer_expn())`.
311    #[inline]
312    pub fn outer_expn_is_descendant_of(self, ctxt: SyntaxContext) -> bool {
313        HygieneData::with(|data| data.is_descendant_of(self, data.outer_expn(ctxt)))
314    }
315
316    /// Returns span for the macro which originally caused this expansion to happen.
317    ///
318    /// Stops backtracing at include! boundary.
319    pub fn expansion_cause(mut self) -> Option<Span> {
320        let mut last_macro = None;
321        loop {
322            // Fast path to avoid locking.
323            if self == ExpnId::root() {
324                break;
325            }
326            let expn_data = self.expn_data();
327            // Stop going up the backtrace once include! is encountered
328            if expn_data.kind == ExpnKind::Macro(MacroKind::Bang, sym::include) {
329                break;
330            }
331            self = expn_data.call_site.ctxt().outer_expn();
332            last_macro = Some(expn_data.call_site);
333        }
334        last_macro
335    }
336}
337
338#[derive(#[automatically_derived]
impl ::core::fmt::Debug for HygieneData {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        let names: &'static _ =
            &["local_expn_data", "local_expn_hashes", "foreign_expn_data",
                        "foreign_expn_hashes", "expn_hash_to_expn_id",
                        "syntax_context_data", "syntax_context_map",
                        "expn_data_disambiguators"];
        let values: &[&dyn ::core::fmt::Debug] =
            &[&self.local_expn_data, &self.local_expn_hashes,
                        &self.foreign_expn_data, &self.foreign_expn_hashes,
                        &self.expn_hash_to_expn_id, &self.syntax_context_data,
                        &self.syntax_context_map, &&self.expn_data_disambiguators];
        ::core::fmt::Formatter::debug_struct_fields_finish(f, "HygieneData",
            names, values)
    }
}Debug)]
339pub(crate) struct HygieneData {
340    /// Each expansion should have an associated expansion data, but sometimes there's a delay
341    /// between creation of an expansion ID and obtaining its data (e.g. macros are collected
342    /// first and then resolved later), so we use an `Option` here.
343    local_expn_data: IndexVec<LocalExpnId, Option<ExpnData>>,
344    local_expn_hashes: IndexVec<LocalExpnId, ExpnHash>,
345    /// Data and hash information from external crates. We may eventually want to remove these
346    /// maps, and fetch the information directly from the other crate's metadata like DefIds do.
347    foreign_expn_data: FxHashMap<ExpnId, ExpnData>,
348    foreign_expn_hashes: FxHashMap<ExpnId, ExpnHash>,
349    expn_hash_to_expn_id: UnhashMap<ExpnHash, ExpnId>,
350    syntax_context_data: Vec<SyntaxContextData>,
351    syntax_context_map: FxHashMap<SyntaxContextKey, SyntaxContext>,
352    /// Maps the `local_hash` of an `ExpnData` to the next disambiguator value.
353    /// This is used by `update_disambiguator` to keep track of which `ExpnData`s
354    /// would have collisions without a disambiguator.
355    /// The keys of this map are always computed with `ExpnData.disambiguator`
356    /// set to 0.
357    expn_data_disambiguators: UnhashMap<Hash64, u32>,
358}
359
360impl HygieneData {
361    pub(crate) fn new(edition: Edition) -> Self {
362        let root_data = ExpnData::default(
363            ExpnKind::Root,
364            DUMMY_SP,
365            edition,
366            Some(CRATE_DEF_ID.to_def_id()),
367            None,
368        );
369
370        let root_ctxt_data = SyntaxContextData::root();
371        HygieneData {
372            local_expn_data: IndexVec::from_elem_n(Some(root_data), 1),
373            local_expn_hashes: IndexVec::from_elem_n(ExpnHash(Fingerprint::ZERO), 1),
374            foreign_expn_data: FxHashMap::default(),
375            foreign_expn_hashes: FxHashMap::default(),
376            expn_hash_to_expn_id: iter::once((ExpnHash(Fingerprint::ZERO), ExpnId::root()))
377                .collect(),
378            syntax_context_data: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [root_ctxt_data]))vec![root_ctxt_data],
379            syntax_context_map: iter::once((root_ctxt_data.key(), SyntaxContext(0))).collect(),
380            expn_data_disambiguators: UnhashMap::default(),
381        }
382    }
383
384    #[inline]
385    fn with<R>(f: impl FnOnce(&mut HygieneData) -> R) -> R {
386        with_session_globals(|session_globals| f(&mut session_globals.hygiene_data.borrow_mut()))
387    }
388
389    #[inline]
390    fn expn_hash(&self, expn_id: ExpnId) -> ExpnHash {
391        match expn_id.as_local() {
392            Some(expn_id) => self.local_expn_hashes[expn_id],
393            None => self.foreign_expn_hashes[&expn_id],
394        }
395    }
396
397    #[inline]
398    fn local_expn_data(&self, expn_id: LocalExpnId) -> &ExpnData {
399        self.local_expn_data[expn_id].as_ref().expect("no expansion data for an expansion ID")
400    }
401
402    fn expn_data(&self, expn_id: ExpnId) -> &ExpnData {
403        if let Some(expn_id) = expn_id.as_local() {
404            self.local_expn_data[expn_id].as_ref().expect("no expansion data for an expansion ID")
405        } else {
406            &self.foreign_expn_data[&expn_id]
407        }
408    }
409
410    fn is_descendant_of(&self, mut expn_id: ExpnId, ancestor: ExpnId) -> bool {
411        // a couple "fast path" cases to avoid traversing parents in the loop below
412        if ancestor == ExpnId::root() {
413            return true;
414        }
415        if expn_id.krate != ancestor.krate {
416            return false;
417        }
418        loop {
419            if expn_id == ancestor {
420                return true;
421            }
422            if expn_id == ExpnId::root() {
423                return false;
424            }
425            expn_id = self.expn_data(expn_id).parent;
426        }
427    }
428
429    #[inline]
430    fn normalize_to_macros_2_0(&self, ctxt: SyntaxContext) -> SyntaxContext {
431        self.syntax_context_data[ctxt.0 as usize].opaque
432    }
433
434    #[inline]
435    fn normalize_to_macro_rules(&self, ctxt: SyntaxContext) -> SyntaxContext {
436        self.syntax_context_data[ctxt.0 as usize].opaque_and_semiopaque
437    }
438
439    /// See [`SyntaxContextData::outer_expn`]
440    #[inline]
441    fn outer_expn(&self, ctxt: SyntaxContext) -> ExpnId {
442        self.syntax_context_data[ctxt.0 as usize].outer_expn
443    }
444
445    /// The last macro expansion and its Transparency
446    #[inline]
447    fn outer_mark(&self, ctxt: SyntaxContext) -> (ExpnId, Transparency) {
448        let data = &self.syntax_context_data[ctxt.0 as usize];
449        (data.outer_expn, data.outer_transparency)
450    }
451
452    #[inline]
453    fn parent_ctxt(&self, ctxt: SyntaxContext) -> SyntaxContext {
454        self.syntax_context_data[ctxt.0 as usize].parent
455    }
456
457    fn remove_mark(&self, ctxt: &mut SyntaxContext) -> (ExpnId, Transparency) {
458        let outer_mark = self.outer_mark(*ctxt);
459        *ctxt = self.parent_ctxt(*ctxt);
460        outer_mark
461    }
462
463    fn marks(&self, mut ctxt: SyntaxContext) -> Vec<(ExpnId, Transparency)> {
464        let mut marks = Vec::new();
465        while !ctxt.is_root() {
466            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_span/src/hygiene.rs:466",
                        "rustc_span::hygiene", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_span/src/hygiene.rs"),
                        ::tracing_core::__macro_support::Option::Some(466u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_span::hygiene"),
                        ::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!("marks: getting parent of {0:?}",
                                                    ctxt) as &dyn Value))])
            });
    } else { ; }
};debug!("marks: getting parent of {:?}", ctxt);
467            marks.push(self.outer_mark(ctxt));
468            ctxt = self.parent_ctxt(ctxt);
469        }
470        marks.reverse();
471        marks
472    }
473
474    fn walk_chain(&self, mut span: Span, to: SyntaxContext) -> Span {
475        let orig_span = span;
476        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_span/src/hygiene.rs:476",
                        "rustc_span::hygiene", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_span/src/hygiene.rs"),
                        ::tracing_core::__macro_support::Option::Some(476u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_span::hygiene"),
                        ::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!("walk_chain({0:?}, {1:?})",
                                                    span, to) as &dyn Value))])
            });
    } else { ; }
};debug!("walk_chain({:?}, {:?})", span, to);
477        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_span/src/hygiene.rs:477",
                        "rustc_span::hygiene", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_span/src/hygiene.rs"),
                        ::tracing_core::__macro_support::Option::Some(477u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_span::hygiene"),
                        ::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!("walk_chain: span ctxt = {0:?}",
                                                    span.ctxt()) as &dyn Value))])
            });
    } else { ; }
};debug!("walk_chain: span ctxt = {:?}", span.ctxt());
478        while span.ctxt() != to && span.from_expansion() {
479            let outer_expn = self.outer_expn(span.ctxt());
480            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_span/src/hygiene.rs:480",
                        "rustc_span::hygiene", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_span/src/hygiene.rs"),
                        ::tracing_core::__macro_support::Option::Some(480u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_span::hygiene"),
                        ::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!("walk_chain({0:?}): outer_expn={1:?}",
                                                    span, outer_expn) as &dyn Value))])
            });
    } else { ; }
};debug!("walk_chain({:?}): outer_expn={:?}", span, outer_expn);
481            let expn_data = self.expn_data(outer_expn);
482            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_span/src/hygiene.rs:482",
                        "rustc_span::hygiene", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_span/src/hygiene.rs"),
                        ::tracing_core::__macro_support::Option::Some(482u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_span::hygiene"),
                        ::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!("walk_chain({0:?}): expn_data={1:?}",
                                                    span, expn_data) as &dyn Value))])
            });
    } else { ; }
};debug!("walk_chain({:?}): expn_data={:?}", span, expn_data);
483            span = expn_data.call_site;
484        }
485        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_span/src/hygiene.rs:485",
                        "rustc_span::hygiene", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_span/src/hygiene.rs"),
                        ::tracing_core::__macro_support::Option::Some(485u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_span::hygiene"),
                        ::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!("walk_chain: for span {0:?} >>> return span = {1:?}",
                                                    orig_span, span) as &dyn Value))])
            });
    } else { ; }
};debug!("walk_chain: for span {:?} >>> return span = {:?}", orig_span, span);
486        span
487    }
488
489    fn walk_chain_collapsed(&self, mut span: Span, to: Span) -> Span {
490        let orig_span = span;
491        let mut ret_span = span;
492        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_span/src/hygiene.rs:492",
                        "rustc_span::hygiene", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_span/src/hygiene.rs"),
                        ::tracing_core::__macro_support::Option::Some(492u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_span::hygiene"),
                        ::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!("walk_chain_collapsed({0:?}, {1:?})",
                                                    span, to) as &dyn Value))])
            });
    } else { ; }
};debug!("walk_chain_collapsed({:?}, {:?})", span, to);
493        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_span/src/hygiene.rs:493",
                        "rustc_span::hygiene", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_span/src/hygiene.rs"),
                        ::tracing_core::__macro_support::Option::Some(493u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_span::hygiene"),
                        ::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!("walk_chain_collapsed: span ctxt = {0:?}",
                                                    span.ctxt()) as &dyn Value))])
            });
    } else { ; }
};debug!("walk_chain_collapsed: span ctxt = {:?}", span.ctxt());
494        while let ctxt = span.ctxt()
495            && !ctxt.is_root()
496            && ctxt != to.ctxt()
497        {
498            let outer_expn = self.outer_expn(ctxt);
499            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_span/src/hygiene.rs:499",
                        "rustc_span::hygiene", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_span/src/hygiene.rs"),
                        ::tracing_core::__macro_support::Option::Some(499u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_span::hygiene"),
                        ::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!("walk_chain_collapsed({0:?}): outer_expn={1:?}",
                                                    span, outer_expn) as &dyn Value))])
            });
    } else { ; }
};debug!("walk_chain_collapsed({:?}): outer_expn={:?}", span, outer_expn);
500            let expn_data = self.expn_data(outer_expn);
501            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_span/src/hygiene.rs:501",
                        "rustc_span::hygiene", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_span/src/hygiene.rs"),
                        ::tracing_core::__macro_support::Option::Some(501u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_span::hygiene"),
                        ::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!("walk_chain_collapsed({0:?}): expn_data={1:?}",
                                                    span, expn_data) as &dyn Value))])
            });
    } else { ; }
};debug!("walk_chain_collapsed({:?}): expn_data={:?}", span, expn_data);
502            span = expn_data.call_site;
503            if expn_data.collapse_debuginfo {
504                ret_span = span;
505            }
506        }
507        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_span/src/hygiene.rs:507",
                        "rustc_span::hygiene", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_span/src/hygiene.rs"),
                        ::tracing_core::__macro_support::Option::Some(507u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_span::hygiene"),
                        ::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!("walk_chain_collapsed: for span {0:?} >>> return span = {1:?}",
                                                    orig_span, ret_span) as &dyn Value))])
            });
    } else { ; }
};debug!("walk_chain_collapsed: for span {:?} >>> return span = {:?}", orig_span, ret_span);
508        ret_span
509    }
510
511    fn adjust(&self, ctxt: &mut SyntaxContext, expn_id: ExpnId) -> Option<ExpnId> {
512        let mut scope = None;
513        while !self.is_descendant_of(expn_id, self.outer_expn(*ctxt)) {
514            scope = Some(self.remove_mark(ctxt).0);
515        }
516        scope
517    }
518
519    fn apply_mark(
520        &mut self,
521        ctxt: SyntaxContext,
522        expn_id: ExpnId,
523        transparency: Transparency,
524    ) -> SyntaxContext {
525        match (&expn_id, &ExpnId::root()) {
    (left_val, right_val) => {
        if *left_val == *right_val {
            let kind = ::core::panicking::AssertKind::Ne;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_ne!(expn_id, ExpnId::root());
526        if transparency == Transparency::Opaque {
527            return self.alloc_ctxt(ctxt, expn_id, transparency);
528        }
529
530        let call_site_ctxt = self.expn_data(expn_id).call_site.ctxt();
531        let mut call_site_ctxt = if transparency == Transparency::SemiOpaque {
532            self.normalize_to_macros_2_0(call_site_ctxt)
533        } else {
534            self.normalize_to_macro_rules(call_site_ctxt)
535        };
536
537        if call_site_ctxt.is_root() {
538            return self.alloc_ctxt(ctxt, expn_id, transparency);
539        }
540
541        // Otherwise, `expn_id` is a macros 1.0 definition and the call site is in a
542        // macros 2.0 expansion, i.e., a macros 1.0 invocation is in a macros 2.0 definition.
543        //
544        // In this case, the tokens from the macros 1.0 definition inherit the hygiene
545        // at their invocation. That is, we pretend that the macros 1.0 definition
546        // was defined at its invocation (i.e., inside the macros 2.0 definition)
547        // so that the macros 2.0 definition remains hygienic.
548        //
549        // See the example at `test/ui/hygiene/legacy_interaction.rs`.
550        for (expn_id, transparency) in self.marks(ctxt) {
551            call_site_ctxt = self.alloc_ctxt(call_site_ctxt, expn_id, transparency);
552        }
553        self.alloc_ctxt(call_site_ctxt, expn_id, transparency)
554    }
555
556    /// Allocate a new context with the given key, or retrieve it from cache if the given key
557    /// already exists. The auxiliary fields are calculated from the key.
558    fn alloc_ctxt(
559        &mut self,
560        parent: SyntaxContext,
561        expn_id: ExpnId,
562        transparency: Transparency,
563    ) -> SyntaxContext {
564        // Look into the cache first.
565        let key = (parent, expn_id, transparency);
566        if let Some(ctxt) = self.syntax_context_map.get(&key) {
567            return *ctxt;
568        }
569
570        // Reserve a new syntax context.
571        // The inserted dummy data can only be potentially accessed by nested `alloc_ctxt` calls,
572        // the assert below ensures that it doesn't happen.
573        let ctxt = SyntaxContext::from_usize(self.syntax_context_data.len());
574        self.syntax_context_data
575            .push(SyntaxContextData { dollar_crate_name: sym::dummy, ..SyntaxContextData::root() });
576        self.syntax_context_map.insert(key, ctxt);
577
578        // Opaque and semi-opaque versions of the parent. Note that they may be equal to the
579        // parent itself. E.g. `parent_opaque` == `parent` if the expn chain contains only opaques,
580        // and `parent_opaque_and_semiopaque` == `parent` if the expn contains only (semi-)opaques.
581        let parent_data = &self.syntax_context_data[parent.0 as usize];
582        match (&parent_data.dollar_crate_name, &sym::dummy) {
    (left_val, right_val) => {
        if *left_val == *right_val {
            let kind = ::core::panicking::AssertKind::Ne;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_ne!(parent_data.dollar_crate_name, sym::dummy);
583        let parent_opaque = parent_data.opaque;
584        let parent_opaque_and_semiopaque = parent_data.opaque_and_semiopaque;
585
586        // Evaluate opaque and semi-opaque versions of the new syntax context.
587        let (opaque, opaque_and_semiopaque) = match transparency {
588            Transparency::Transparent => (parent_opaque, parent_opaque_and_semiopaque),
589            Transparency::SemiOpaque => (
590                parent_opaque,
591                // Will be the same as `ctxt` if the expn chain contains only (semi-)opaques.
592                self.alloc_ctxt(parent_opaque_and_semiopaque, expn_id, transparency),
593            ),
594            Transparency::Opaque => (
595                // Will be the same as `ctxt` if the expn chain contains only opaques.
596                self.alloc_ctxt(parent_opaque, expn_id, transparency),
597                // Will be the same as `ctxt` if the expn chain contains only (semi-)opaques.
598                self.alloc_ctxt(parent_opaque_and_semiopaque, expn_id, transparency),
599            ),
600        };
601
602        // Fill the full data, now that we have it.
603        self.syntax_context_data[ctxt.as_u32() as usize] = SyntaxContextData {
604            outer_expn: expn_id,
605            outer_transparency: transparency,
606            parent,
607            opaque,
608            opaque_and_semiopaque,
609            dollar_crate_name: kw::DollarCrate,
610        };
611        ctxt
612    }
613}
614
615pub fn walk_chain(span: Span, to: SyntaxContext) -> Span {
616    HygieneData::with(|data| data.walk_chain(span, to))
617}
618
619/// In order to have good line stepping behavior in debugger, for the given span we return its
620/// outermost macro call site that still has a `#[collapse_debuginfo(yes)]` property on it.
621/// We also stop walking call sites at the function body level because no line stepping can occur
622/// at the level above that.
623/// The returned span can then be used in emitted debuginfo.
624pub fn walk_chain_collapsed(span: Span, to: Span) -> Span {
625    HygieneData::with(|data| data.walk_chain_collapsed(span, to))
626}
627
628pub fn update_dollar_crate_names(mut get_name: impl FnMut(SyntaxContext) -> Symbol) {
629    // The new contexts that need updating are at the end of the list and have `$crate` as a name.
630    let mut to_update = ::alloc::vec::Vec::new()vec![];
631    HygieneData::with(|data| {
632        for (idx, scdata) in data.syntax_context_data.iter().enumerate().rev() {
633            if scdata.dollar_crate_name == kw::DollarCrate {
634                to_update.push((idx, kw::DollarCrate));
635            } else {
636                break;
637            }
638        }
639    });
640    // The callback must be called from outside of the `HygieneData` lock,
641    // since it will try to acquire it too.
642    for (idx, name) in &mut to_update {
643        *name = get_name(SyntaxContext::from_usize(*idx));
644    }
645    HygieneData::with(|data| {
646        for (idx, name) in to_update {
647            data.syntax_context_data[idx].dollar_crate_name = name;
648        }
649    })
650}
651
652pub fn debug_hygiene_data(verbose: bool) -> String {
653    HygieneData::with(|data| {
654        if verbose {
655            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:#?}", data))
    })format!("{data:#?}")
656        } else {
657            let mut s = String::from("Expansions:");
658            let mut debug_expn_data = |(id, expn_data): (&ExpnId, &ExpnData)| {
659                s.push_str(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("\n{0:?}: parent: {1:?}, call_site_ctxt: {2:?}, def_site_ctxt: {3:?}, kind: {4:?}",
                id, expn_data.parent, expn_data.call_site.ctxt(),
                expn_data.def_site.ctxt(), expn_data.kind))
    })format!(
660                    "\n{:?}: parent: {:?}, call_site_ctxt: {:?}, def_site_ctxt: {:?}, kind: {:?}",
661                    id,
662                    expn_data.parent,
663                    expn_data.call_site.ctxt(),
664                    expn_data.def_site.ctxt(),
665                    expn_data.kind,
666                ))
667            };
668            data.local_expn_data.iter_enumerated().for_each(|(id, expn_data)| {
669                let expn_data = expn_data.as_ref().expect("no expansion data for an expansion ID");
670                debug_expn_data((&id.to_expn_id(), expn_data))
671            });
672
673            // Sort the hash map for more reproducible output.
674            // Because of this, it is fine to rely on the unstable iteration order of the map.
675            #[allow(rustc::potential_query_instability)]
676            let mut foreign_expn_data: Vec<_> = data.foreign_expn_data.iter().collect();
677            foreign_expn_data.sort_by_key(|(id, _)| (id.krate, id.local_id));
678            foreign_expn_data.into_iter().for_each(debug_expn_data);
679            s.push_str("\n\nSyntaxContexts:");
680            data.syntax_context_data.iter().enumerate().for_each(|(id, ctxt)| {
681                s.push_str(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("\n#{0}: parent: {1:?}, outer_mark: ({2:?}, {3:?})",
                id, ctxt.parent, ctxt.outer_expn, ctxt.outer_transparency))
    })format!(
682                    "\n#{}: parent: {:?}, outer_mark: ({:?}, {:?})",
683                    id, ctxt.parent, ctxt.outer_expn, ctxt.outer_transparency,
684                ));
685            });
686            s
687        }
688    })
689}
690
691impl SyntaxContext {
692    #[inline]
693    pub const fn root() -> Self {
694        SyntaxContext(0)
695    }
696
697    #[inline]
698    pub const fn is_root(self) -> bool {
699        self.0 == SyntaxContext::root().as_u32()
700    }
701
702    #[inline]
703    pub(crate) const fn as_u32(self) -> u32 {
704        self.0
705    }
706
707    #[inline]
708    pub(crate) const fn from_u32(raw: u32) -> SyntaxContext {
709        SyntaxContext(raw)
710    }
711
712    #[inline]
713    pub(crate) const fn from_u16(raw: u16) -> SyntaxContext {
714        SyntaxContext(raw as u32)
715    }
716
717    #[inline]
718    fn from_usize(raw: usize) -> SyntaxContext {
719        SyntaxContext(u32::try_from(raw).unwrap())
720    }
721
722    /// Extend a syntax context with a given expansion and transparency.
723    #[inline]
724    pub fn apply_mark(self, expn_id: ExpnId, transparency: Transparency) -> SyntaxContext {
725        HygieneData::with(|data| data.apply_mark(self, expn_id, transparency))
726    }
727
728    /// Pulls a single mark off of the syntax context. This effectively moves the
729    /// context up one macro definition level. That is, if we have a nested macro
730    /// definition as follows:
731    ///
732    /// ```ignore (illustrative)
733    /// macro_rules! f {
734    ///    macro_rules! g {
735    ///        ...
736    ///    }
737    /// }
738    /// ```
739    ///
740    /// and we have a SyntaxContext that is referring to something declared by an invocation
741    /// of g (call it g1), calling remove_mark will result in the SyntaxContext for the
742    /// invocation of f that created g1.
743    /// Returns the mark that was removed.
744    #[inline]
745    pub fn remove_mark(&mut self) -> ExpnId {
746        HygieneData::with(|data| data.remove_mark(self).0)
747    }
748
749    #[inline]
750    pub fn marks(self) -> Vec<(ExpnId, Transparency)> {
751        HygieneData::with(|data| data.marks(self))
752    }
753
754    /// Adjust this context for resolution in a scope created by the given expansion.
755    /// For example, consider the following three resolutions of `f`:
756    ///
757    /// ```rust
758    /// #![feature(decl_macro)]
759    /// mod foo {
760    ///     pub fn f() {} // `f`'s `SyntaxContext` is empty.
761    /// }
762    /// m!(f);
763    /// macro m($f:ident) {
764    ///     mod bar {
765    ///         pub fn f() {} // `f`'s `SyntaxContext` has a single `ExpnId` from `m`.
766    ///         pub fn $f() {} // `$f`'s `SyntaxContext` is empty.
767    ///     }
768    ///     foo::f(); // `f`'s `SyntaxContext` has a single `ExpnId` from `m`
769    ///     //^ Since `mod foo` is outside this expansion, `adjust` removes the mark from `f`,
770    ///     //| and it resolves to `::foo::f`.
771    ///     bar::f(); // `f`'s `SyntaxContext` has a single `ExpnId` from `m`
772    ///     //^ Since `mod bar` not outside this expansion, `adjust` does not change `f`,
773    ///     //| and it resolves to `::bar::f`.
774    ///     bar::$f(); // `f`'s `SyntaxContext` is empty.
775    ///     //^ Since `mod bar` is not outside this expansion, `adjust` does not change `$f`,
776    ///     //| and it resolves to `::bar::$f`.
777    /// }
778    /// ```
779    /// This returns the expansion whose definition scope we use to privacy check the resolution,
780    /// or `None` if we privacy check as usual (i.e., not w.r.t. a macro definition scope).
781    #[inline]
782    pub fn adjust(&mut self, expn_id: ExpnId) -> Option<ExpnId> {
783        HygieneData::with(|data| data.adjust(self, expn_id))
784    }
785
786    /// Like `SyntaxContext::adjust`, but also normalizes `self` to macros 2.0.
787    #[inline]
788    pub fn normalize_to_macros_2_0_and_adjust(&mut self, expn_id: ExpnId) -> Option<ExpnId> {
789        HygieneData::with(|data| {
790            *self = data.normalize_to_macros_2_0(*self);
791            data.adjust(self, expn_id)
792        })
793    }
794
795    /// Adjust this context for resolution in a scope created by the given expansion
796    /// via a glob import with the given `SyntaxContext`.
797    /// For example:
798    ///
799    /// ```compile_fail,E0425
800    /// #![feature(decl_macro)]
801    /// m!(f);
802    /// macro m($i:ident) {
803    ///     mod foo {
804    ///         pub fn f() {} // `f`'s `SyntaxContext` has a single `ExpnId` from `m`.
805    ///         pub fn $i() {} // `$i`'s `SyntaxContext` is empty.
806    ///     }
807    ///     n!(f);
808    ///     macro n($j:ident) {
809    ///         use foo::*;
810    ///         f(); // `f`'s `SyntaxContext` has a mark from `m` and a mark from `n`
811    ///         //^ `glob_adjust` removes the mark from `n`, so this resolves to `foo::f`.
812    ///         $i(); // `$i`'s `SyntaxContext` has a mark from `n`
813    ///         //^ `glob_adjust` removes the mark from `n`, so this resolves to `foo::$i`.
814    ///         $j(); // `$j`'s `SyntaxContext` has a mark from `m`
815    ///         //^ This cannot be glob-adjusted, so this is a resolution error.
816    ///     }
817    /// }
818    /// ```
819    /// This returns `None` if the context cannot be glob-adjusted.
820    /// Otherwise, it returns the scope to use when privacy checking (see `adjust` for details).
821    pub fn glob_adjust(&mut self, expn_id: ExpnId, glob_span: Span) -> Option<Option<ExpnId>> {
822        HygieneData::with(|data| {
823            let mut scope = None;
824            let mut glob_ctxt = data.normalize_to_macros_2_0(glob_span.ctxt());
825            while !data.is_descendant_of(expn_id, data.outer_expn(glob_ctxt)) {
826                scope = Some(data.remove_mark(&mut glob_ctxt).0);
827                if data.remove_mark(self).0 != scope.unwrap() {
828                    return None;
829                }
830            }
831            if data.adjust(self, expn_id).is_some() {
832                return None;
833            }
834            Some(scope)
835        })
836    }
837
838    /// Undo `glob_adjust` if possible:
839    ///
840    /// ```ignore (illustrative)
841    /// if let Some(privacy_checking_scope) = self.reverse_glob_adjust(expansion, glob_ctxt) {
842    ///     assert!(self.glob_adjust(expansion, glob_ctxt) == Some(privacy_checking_scope));
843    /// }
844    /// ```
845    pub fn reverse_glob_adjust(
846        &mut self,
847        expn_id: ExpnId,
848        glob_span: Span,
849    ) -> Option<Option<ExpnId>> {
850        HygieneData::with(|data| {
851            if data.adjust(self, expn_id).is_some() {
852                return None;
853            }
854
855            let mut glob_ctxt = data.normalize_to_macros_2_0(glob_span.ctxt());
856            let mut marks = Vec::new();
857            while !data.is_descendant_of(expn_id, data.outer_expn(glob_ctxt)) {
858                marks.push(data.remove_mark(&mut glob_ctxt));
859            }
860
861            let scope = marks.last().map(|mark| mark.0);
862            while let Some((expn_id, transparency)) = marks.pop() {
863                *self = data.apply_mark(*self, expn_id, transparency);
864            }
865            Some(scope)
866        })
867    }
868
869    pub fn hygienic_eq(self, other: SyntaxContext, expn_id: ExpnId) -> bool {
870        HygieneData::with(|data| {
871            let mut self_normalized = data.normalize_to_macros_2_0(self);
872            data.adjust(&mut self_normalized, expn_id);
873            self_normalized == data.normalize_to_macros_2_0(other)
874        })
875    }
876
877    #[inline]
878    pub fn normalize_to_macros_2_0(self) -> SyntaxContext {
879        HygieneData::with(|data| data.normalize_to_macros_2_0(self))
880    }
881
882    #[inline]
883    pub fn normalize_to_macro_rules(self) -> SyntaxContext {
884        HygieneData::with(|data| data.normalize_to_macro_rules(self))
885    }
886
887    /// See [`SyntaxContextData::outer_expn`]
888    #[inline]
889    pub fn outer_expn(self) -> ExpnId {
890        HygieneData::with(|data| data.outer_expn(self))
891    }
892
893    /// `ctxt.outer_expn_data()` is equivalent to but faster than
894    /// `ctxt.outer_expn().expn_data()`.
895    #[inline]
896    pub fn outer_expn_data(self) -> ExpnData {
897        HygieneData::with(|data| data.expn_data(data.outer_expn(self)).clone())
898    }
899
900    /// See [`HygieneData::outer_mark`]
901    #[inline]
902    fn outer_mark(self) -> (ExpnId, Transparency) {
903        HygieneData::with(|data| data.outer_mark(self))
904    }
905
906    #[inline]
907    pub(crate) fn dollar_crate_name(self) -> Symbol {
908        HygieneData::with(|data| data.syntax_context_data[self.0 as usize].dollar_crate_name)
909    }
910
911    #[inline]
912    pub fn edition(self) -> Edition {
913        HygieneData::with(|data| data.expn_data(data.outer_expn(self)).edition)
914    }
915
916    /// Returns whether this context originates in a foreign crate's external macro.
917    ///
918    /// This is used to test whether a lint should not even begin to figure out whether it should
919    /// be reported on the current node.
920    pub fn in_external_macro(self, sm: &SourceMap) -> bool {
921        let expn_data = self.outer_expn_data();
922        match expn_data.kind {
923            ExpnKind::Root
924            | ExpnKind::Desugaring(
925                DesugaringKind::ForLoop
926                | DesugaringKind::WhileLoop
927                | DesugaringKind::OpaqueTy
928                | DesugaringKind::Async
929                | DesugaringKind::Await,
930            ) => false,
931            ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
932            ExpnKind::Macro(MacroKind::Bang, _) => {
933                // Dummy span for the `def_site` means it's an external macro.
934                expn_data.def_site.is_dummy() || sm.is_imported(expn_data.def_site)
935            }
936            ExpnKind::Macro { .. } => true, // definitely a plugin
937        }
938    }
939}
940
941impl fmt::Debug for SyntaxContext {
942    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
943        f.write_fmt(format_args!("#{0}", self.0))write!(f, "#{}", self.0)
944    }
945}
946
947impl Span {
948    /// Reuses the span but adds information like the kind of the desugaring and features that are
949    /// allowed inside this span.
950    pub fn mark_with_reason(
951        self,
952        allow_internal_unstable: Option<Arc<[Symbol]>>,
953        reason: DesugaringKind,
954        edition: Edition,
955        hcx: impl HashStableContext,
956    ) -> Span {
957        let expn_data = ExpnData {
958            allow_internal_unstable,
959            ..ExpnData::default(ExpnKind::Desugaring(reason), self, edition, None, None)
960        };
961        let expn_id = LocalExpnId::fresh(expn_data, hcx);
962        self.apply_mark(expn_id.to_expn_id(), Transparency::Transparent)
963    }
964}
965
966/// A subset of properties from both macro definition and macro call available through global data.
967/// Avoid using this if you have access to the original definition or call structures.
968#[derive(#[automatically_derived]
impl ::core::clone::Clone for ExpnData {
    #[inline]
    fn clone(&self) -> ExpnData {
        ExpnData {
            kind: ::core::clone::Clone::clone(&self.kind),
            parent: ::core::clone::Clone::clone(&self.parent),
            call_site: ::core::clone::Clone::clone(&self.call_site),
            disambiguator: ::core::clone::Clone::clone(&self.disambiguator),
            def_site: ::core::clone::Clone::clone(&self.def_site),
            allow_internal_unstable: ::core::clone::Clone::clone(&self.allow_internal_unstable),
            edition: ::core::clone::Clone::clone(&self.edition),
            macro_def_id: ::core::clone::Clone::clone(&self.macro_def_id),
            parent_module: ::core::clone::Clone::clone(&self.parent_module),
            allow_internal_unsafe: ::core::clone::Clone::clone(&self.allow_internal_unsafe),
            local_inner_macros: ::core::clone::Clone::clone(&self.local_inner_macros),
            collapse_debuginfo: ::core::clone::Clone::clone(&self.collapse_debuginfo),
            hide_backtrace: ::core::clone::Clone::clone(&self.hide_backtrace),
        }
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for ExpnData {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        let names: &'static _ =
            &["kind", "parent", "call_site", "disambiguator", "def_site",
                        "allow_internal_unstable", "edition", "macro_def_id",
                        "parent_module", "allow_internal_unsafe",
                        "local_inner_macros", "collapse_debuginfo",
                        "hide_backtrace"];
        let values: &[&dyn ::core::fmt::Debug] =
            &[&self.kind, &self.parent, &self.call_site, &self.disambiguator,
                        &self.def_site, &self.allow_internal_unstable,
                        &self.edition, &self.macro_def_id, &self.parent_module,
                        &self.allow_internal_unsafe, &self.local_inner_macros,
                        &self.collapse_debuginfo, &&self.hide_backtrace];
        ::core::fmt::Formatter::debug_struct_fields_finish(f, "ExpnData",
            names, values)
    }
}Debug, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for ExpnData {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    ExpnData {
                        kind: ref __binding_0,
                        parent: ref __binding_1,
                        call_site: ref __binding_2,
                        disambiguator: ref __binding_3,
                        def_site: ref __binding_4,
                        allow_internal_unstable: ref __binding_5,
                        edition: ref __binding_6,
                        macro_def_id: ref __binding_7,
                        parent_module: ref __binding_8,
                        allow_internal_unsafe: ref __binding_9,
                        local_inner_macros: ref __binding_10,
                        collapse_debuginfo: ref __binding_11,
                        hide_backtrace: ref __binding_12 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_2,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_3,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_4,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_5,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_6,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_7,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_8,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_9,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_10,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_11,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_12,
                            __encoder);
                    }
                }
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
            for ExpnData {
            fn decode(__decoder: &mut __D) -> Self {
                ExpnData {
                    kind: ::rustc_serialize::Decodable::decode(__decoder),
                    parent: ::rustc_serialize::Decodable::decode(__decoder),
                    call_site: ::rustc_serialize::Decodable::decode(__decoder),
                    disambiguator: ::rustc_serialize::Decodable::decode(__decoder),
                    def_site: ::rustc_serialize::Decodable::decode(__decoder),
                    allow_internal_unstable: ::rustc_serialize::Decodable::decode(__decoder),
                    edition: ::rustc_serialize::Decodable::decode(__decoder),
                    macro_def_id: ::rustc_serialize::Decodable::decode(__decoder),
                    parent_module: ::rustc_serialize::Decodable::decode(__decoder),
                    allow_internal_unsafe: ::rustc_serialize::Decodable::decode(__decoder),
                    local_inner_macros: ::rustc_serialize::Decodable::decode(__decoder),
                    collapse_debuginfo: ::rustc_serialize::Decodable::decode(__decoder),
                    hide_backtrace: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };Decodable, const _: () =
    {
        impl ::rustc_data_structures::stable_hasher::HashStable for ExpnData {
            #[inline]
            fn hash_stable<__Hcx: ::rustc_data_structures::stable_hasher::HashStableContext>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                match *self {
                    ExpnData {
                        kind: ref __binding_0,
                        parent: ref __binding_1,
                        call_site: ref __binding_2,
                        disambiguator: ref __binding_3,
                        def_site: ref __binding_4,
                        allow_internal_unstable: ref __binding_5,
                        edition: ref __binding_6,
                        macro_def_id: ref __binding_7,
                        parent_module: ref __binding_8,
                        allow_internal_unsafe: ref __binding_9,
                        local_inner_macros: ref __binding_10,
                        collapse_debuginfo: ref __binding_11,
                        hide_backtrace: ref __binding_12 } => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                        { __binding_1.hash_stable(__hcx, __hasher); }
                        { __binding_2.hash_stable(__hcx, __hasher); }
                        { __binding_3.hash_stable(__hcx, __hasher); }
                        { __binding_4.hash_stable(__hcx, __hasher); }
                        { __binding_5.hash_stable(__hcx, __hasher); }
                        { __binding_6.hash_stable(__hcx, __hasher); }
                        { __binding_7.hash_stable(__hcx, __hasher); }
                        { __binding_8.hash_stable(__hcx, __hasher); }
                        { __binding_9.hash_stable(__hcx, __hasher); }
                        { __binding_10.hash_stable(__hcx, __hasher); }
                        { __binding_11.hash_stable(__hcx, __hasher); }
                        { __binding_12.hash_stable(__hcx, __hasher); }
                    }
                }
            }
        }
    };HashStable)]
969pub struct ExpnData {
970    // --- The part unique to each expansion.
971    pub kind: ExpnKind,
972    /// The expansion that contains the definition of the macro for this expansion.
973    pub parent: ExpnId,
974    /// The span of the macro call which produced this expansion.
975    ///
976    /// This span will typically have a different `ExpnData` and `call_site`.
977    /// This recursively traces back through any macro calls which expanded into further
978    /// macro calls, until the "source call-site" is reached at the root SyntaxContext.
979    /// For example, if `food!()` expands to `fruit!()` which then expands to `grape`,
980    /// then the call-site of `grape` is `fruit!()` and the call-site of `fruit!()`
981    /// is `food!()`.
982    ///
983    /// For a desugaring expansion, this is the span of the expression or node that was
984    /// desugared.
985    pub call_site: Span,
986    /// Used to force two `ExpnData`s to have different `Fingerprint`s.
987    /// Due to macro expansion, it's possible to end up with two `ExpnId`s
988    /// that have identical `ExpnData`s. This violates the contract of `HashStable`
989    /// - the two `ExpnId`s are not equal, but their `Fingerprint`s are equal
990    /// (since the numerical `ExpnId` value is not considered by the `HashStable`
991    /// implementation).
992    ///
993    /// The `disambiguator` field is set by `update_disambiguator` when two distinct
994    /// `ExpnId`s would end up with the same `Fingerprint`. Since `ExpnData` includes
995    /// a `krate` field, this value only needs to be unique within a single crate.
996    disambiguator: u32,
997
998    // --- The part specific to the macro/desugaring definition.
999    // --- It may be reasonable to share this part between expansions with the same definition,
1000    // --- but such sharing is known to bring some minor inconveniences without also bringing
1001    // --- noticeable perf improvements (PR #62898).
1002    /// The span of the macro definition (possibly dummy).
1003    /// This span serves only informational purpose and is not used for resolution.
1004    pub def_site: Span,
1005    /// List of `#[unstable]`/feature-gated features that the macro is allowed to use
1006    /// internally without forcing the whole crate to opt-in
1007    /// to them.
1008    pub allow_internal_unstable: Option<Arc<[Symbol]>>,
1009    /// Edition of the crate in which the macro is defined.
1010    pub edition: Edition,
1011    /// The `DefId` of the macro being invoked,
1012    /// if this `ExpnData` corresponds to a macro invocation
1013    pub macro_def_id: Option<DefId>,
1014    /// The normal module (`mod`) in which the expanded macro was defined.
1015    pub parent_module: Option<DefId>,
1016    /// Suppresses the `unsafe_code` lint for code produced by this macro.
1017    pub(crate) allow_internal_unsafe: bool,
1018    /// Enables the macro helper hack (`ident!(...)` -> `$crate::ident!(...)`) for this macro.
1019    pub local_inner_macros: bool,
1020    /// Should debuginfo for the macro be collapsed to the outermost expansion site (in other
1021    /// words, was the macro definition annotated with `#[collapse_debuginfo]`)?
1022    pub(crate) collapse_debuginfo: bool,
1023    /// When true, we do not display the note telling people to use the `-Zmacro-backtrace` flag.
1024    pub hide_backtrace: bool,
1025}
1026
1027impl !PartialEq for ExpnData {}
1028impl !Hash for ExpnData {}
1029
1030impl ExpnData {
1031    pub fn new(
1032        kind: ExpnKind,
1033        parent: ExpnId,
1034        call_site: Span,
1035        def_site: Span,
1036        allow_internal_unstable: Option<Arc<[Symbol]>>,
1037        edition: Edition,
1038        macro_def_id: Option<DefId>,
1039        parent_module: Option<DefId>,
1040        allow_internal_unsafe: bool,
1041        local_inner_macros: bool,
1042        collapse_debuginfo: bool,
1043        hide_backtrace: bool,
1044    ) -> ExpnData {
1045        ExpnData {
1046            kind,
1047            parent,
1048            call_site,
1049            def_site,
1050            allow_internal_unstable,
1051            edition,
1052            macro_def_id,
1053            parent_module,
1054            disambiguator: 0,
1055            allow_internal_unsafe,
1056            local_inner_macros,
1057            collapse_debuginfo,
1058            hide_backtrace,
1059        }
1060    }
1061
1062    /// Constructs expansion data with default properties.
1063    pub fn default(
1064        kind: ExpnKind,
1065        call_site: Span,
1066        edition: Edition,
1067        macro_def_id: Option<DefId>,
1068        parent_module: Option<DefId>,
1069    ) -> ExpnData {
1070        ExpnData {
1071            kind,
1072            parent: ExpnId::root(),
1073            call_site,
1074            def_site: DUMMY_SP,
1075            allow_internal_unstable: None,
1076            edition,
1077            macro_def_id,
1078            parent_module,
1079            disambiguator: 0,
1080            allow_internal_unsafe: false,
1081            local_inner_macros: false,
1082            collapse_debuginfo: false,
1083            hide_backtrace: false,
1084        }
1085    }
1086
1087    pub fn allow_unstable(
1088        kind: ExpnKind,
1089        call_site: Span,
1090        edition: Edition,
1091        allow_internal_unstable: Arc<[Symbol]>,
1092        macro_def_id: Option<DefId>,
1093        parent_module: Option<DefId>,
1094    ) -> ExpnData {
1095        ExpnData {
1096            allow_internal_unstable: Some(allow_internal_unstable),
1097            ..ExpnData::default(kind, call_site, edition, macro_def_id, parent_module)
1098        }
1099    }
1100
1101    #[inline]
1102    pub fn is_root(&self) -> bool {
1103        #[allow(non_exhaustive_omitted_patterns)] match self.kind {
    ExpnKind::Root => true,
    _ => false,
}matches!(self.kind, ExpnKind::Root)
1104    }
1105
1106    #[inline]
1107    fn hash_expn(&self, hcx: &mut impl HashStableContext) -> Hash64 {
1108        let mut hasher = StableHasher::new();
1109        self.hash_stable(hcx, &mut hasher);
1110        hasher.finish()
1111    }
1112}
1113
1114/// Expansion kind.
1115#[derive(#[automatically_derived]
impl ::core::clone::Clone for ExpnKind {
    #[inline]
    fn clone(&self) -> ExpnKind {
        match self {
            ExpnKind::Root => ExpnKind::Root,
            ExpnKind::Macro(__self_0, __self_1) =>
                ExpnKind::Macro(::core::clone::Clone::clone(__self_0),
                    ::core::clone::Clone::clone(__self_1)),
            ExpnKind::AstPass(__self_0) =>
                ExpnKind::AstPass(::core::clone::Clone::clone(__self_0)),
            ExpnKind::Desugaring(__self_0) =>
                ExpnKind::Desugaring(::core::clone::Clone::clone(__self_0)),
        }
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for ExpnKind {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            ExpnKind::Root => ::core::fmt::Formatter::write_str(f, "Root"),
            ExpnKind::Macro(__self_0, __self_1) =>
                ::core::fmt::Formatter::debug_tuple_field2_finish(f, "Macro",
                    __self_0, &__self_1),
            ExpnKind::AstPass(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "AstPass", &__self_0),
            ExpnKind::Desugaring(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "Desugaring", &__self_0),
        }
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for ExpnKind {
    #[inline]
    fn eq(&self, other: &ExpnKind) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (ExpnKind::Macro(__self_0, __self_1),
                    ExpnKind::Macro(__arg1_0, __arg1_1)) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1,
                (ExpnKind::AstPass(__self_0), ExpnKind::AstPass(__arg1_0)) =>
                    __self_0 == __arg1_0,
                (ExpnKind::Desugaring(__self_0),
                    ExpnKind::Desugaring(__arg1_0)) => __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for ExpnKind {
            fn encode(&self, __encoder: &mut __E) {
                let disc =
                    match *self {
                        ExpnKind::Root => { 0usize }
                        ExpnKind::Macro(ref __binding_0, ref __binding_1) => {
                            1usize
                        }
                        ExpnKind::AstPass(ref __binding_0) => { 2usize }
                        ExpnKind::Desugaring(ref __binding_0) => { 3usize }
                    };
                ::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
                match *self {
                    ExpnKind::Root => {}
                    ExpnKind::Macro(ref __binding_0, ref __binding_1) => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                    }
                    ExpnKind::AstPass(ref __binding_0) => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                    ExpnKind::Desugaring(ref __binding_0) => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                }
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
            for ExpnKind {
            fn decode(__decoder: &mut __D) -> Self {
                match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
                    {
                    0usize => { ExpnKind::Root }
                    1usize => {
                        ExpnKind::Macro(::rustc_serialize::Decodable::decode(__decoder),
                            ::rustc_serialize::Decodable::decode(__decoder))
                    }
                    2usize => {
                        ExpnKind::AstPass(::rustc_serialize::Decodable::decode(__decoder))
                    }
                    3usize => {
                        ExpnKind::Desugaring(::rustc_serialize::Decodable::decode(__decoder))
                    }
                    n => {
                        ::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `ExpnKind`, expected 0..4, actual {0}",
                                n));
                    }
                }
            }
        }
    };Decodable, const _: () =
    {
        impl ::rustc_data_structures::stable_hasher::HashStable for ExpnKind {
            #[inline]
            fn hash_stable<__Hcx: ::rustc_data_structures::stable_hasher::HashStableContext>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                ::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
                match *self {
                    ExpnKind::Root => {}
                    ExpnKind::Macro(ref __binding_0, ref __binding_1) => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                        { __binding_1.hash_stable(__hcx, __hasher); }
                    }
                    ExpnKind::AstPass(ref __binding_0) => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                    }
                    ExpnKind::Desugaring(ref __binding_0) => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                    }
                }
            }
        }
    };HashStable)]
1116pub enum ExpnKind {
1117    /// No expansion, aka root expansion. Only `ExpnId::root()` has this kind.
1118    Root,
1119    /// Expansion produced by a macro.
1120    Macro(MacroKind, Symbol),
1121    /// Transform done by the compiler on the AST.
1122    AstPass(AstPass),
1123    /// Desugaring done by the compiler during AST lowering.
1124    Desugaring(DesugaringKind),
1125}
1126
1127impl ExpnKind {
1128    pub fn descr(&self) -> String {
1129        match *self {
1130            ExpnKind::Root => kw::PathRoot.to_string(),
1131            ExpnKind::Macro(macro_kind, name) => match macro_kind {
1132                MacroKind::Bang => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}!", name))
    })format!("{name}!"),
1133                MacroKind::Attr => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("#[{0}]", name))
    })format!("#[{name}]"),
1134                MacroKind::Derive => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("#[derive({0})]", name))
    })format!("#[derive({name})]"),
1135            },
1136            ExpnKind::AstPass(kind) => kind.descr().to_string(),
1137            ExpnKind::Desugaring(kind) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("desugaring of {0}", kind.descr()))
    })format!("desugaring of {}", kind.descr()),
1138        }
1139    }
1140}
1141
1142/// The kind of macro invocation or definition.
1143#[derive(#[automatically_derived]
impl ::core::clone::Clone for MacroKind {
    #[inline]
    fn clone(&self) -> MacroKind { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for MacroKind { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for MacroKind {
    #[inline]
    fn eq(&self, other: &MacroKind) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for MacroKind {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::cmp::PartialOrd for MacroKind {
    #[inline]
    fn partial_cmp(&self, other: &MacroKind)
        -> ::core::option::Option<::core::cmp::Ordering> {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        ::core::cmp::PartialOrd::partial_cmp(&__self_discr, &__arg1_discr)
    }
}PartialOrd, #[automatically_derived]
impl ::core::cmp::Ord for MacroKind {
    #[inline]
    fn cmp(&self, other: &MacroKind) -> ::core::cmp::Ordering {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        ::core::cmp::Ord::cmp(&__self_discr, &__arg1_discr)
    }
}Ord, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for MacroKind {
            fn encode(&self, __encoder: &mut __E) {
                let disc =
                    match *self {
                        MacroKind::Bang => { 0usize }
                        MacroKind::Attr => { 1usize }
                        MacroKind::Derive => { 2usize }
                    };
                ::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
                match *self {
                    MacroKind::Bang => {}
                    MacroKind::Attr => {}
                    MacroKind::Derive => {}
                }
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
            for MacroKind {
            fn decode(__decoder: &mut __D) -> Self {
                match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
                    {
                    0usize => { MacroKind::Bang }
                    1usize => { MacroKind::Attr }
                    2usize => { MacroKind::Derive }
                    n => {
                        ::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `MacroKind`, expected 0..3, actual {0}",
                                n));
                    }
                }
            }
        }
    };Decodable, #[automatically_derived]
impl ::core::hash::Hash for MacroKind {
    #[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)
    }
}Hash, #[automatically_derived]
impl ::core::fmt::Debug for MacroKind {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                MacroKind::Bang => "Bang",
                MacroKind::Attr => "Attr",
                MacroKind::Derive => "Derive",
            })
    }
}Debug)]
1144#[derive(const _: () =
    {
        impl ::rustc_data_structures::stable_hasher::HashStable for MacroKind
            {
            #[inline]
            fn hash_stable<__Hcx: ::rustc_data_structures::stable_hasher::HashStableContext>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                ::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
                match *self {
                    MacroKind::Bang => {}
                    MacroKind::Attr => {}
                    MacroKind::Derive => {}
                }
            }
        }
    };HashStable)]
1145pub enum MacroKind {
1146    /// A bang macro `foo!()`.
1147    Bang,
1148    /// An attribute macro `#[foo]`.
1149    Attr,
1150    /// A derive macro `#[derive(Foo)]`
1151    Derive,
1152}
1153
1154impl MacroKind {
1155    pub fn descr(self) -> &'static str {
1156        match self {
1157            MacroKind::Bang => "macro",
1158            MacroKind::Attr => "attribute macro",
1159            MacroKind::Derive => "derive macro",
1160        }
1161    }
1162
1163    pub fn descr_expected(self) -> &'static str {
1164        match self {
1165            MacroKind::Attr => "attribute",
1166            _ => self.descr(),
1167        }
1168    }
1169
1170    pub fn article(self) -> &'static str {
1171        match self {
1172            MacroKind::Attr => "an",
1173            _ => "a",
1174        }
1175    }
1176}
1177
1178/// The kind of AST transform.
1179#[derive(#[automatically_derived]
impl ::core::clone::Clone for AstPass {
    #[inline]
    fn clone(&self) -> AstPass { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for AstPass { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for AstPass {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                AstPass::StdImports => "StdImports",
                AstPass::TestHarness => "TestHarness",
                AstPass::ProcMacroHarness => "ProcMacroHarness",
            })
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for AstPass {
    #[inline]
    fn eq(&self, other: &AstPass) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for AstPass {
            fn encode(&self, __encoder: &mut __E) {
                let disc =
                    match *self {
                        AstPass::StdImports => { 0usize }
                        AstPass::TestHarness => { 1usize }
                        AstPass::ProcMacroHarness => { 2usize }
                    };
                ::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
                match *self {
                    AstPass::StdImports => {}
                    AstPass::TestHarness => {}
                    AstPass::ProcMacroHarness => {}
                }
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
            for AstPass {
            fn decode(__decoder: &mut __D) -> Self {
                match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
                    {
                    0usize => { AstPass::StdImports }
                    1usize => { AstPass::TestHarness }
                    2usize => { AstPass::ProcMacroHarness }
                    n => {
                        ::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `AstPass`, expected 0..3, actual {0}",
                                n));
                    }
                }
            }
        }
    };Decodable, const _: () =
    {
        impl ::rustc_data_structures::stable_hasher::HashStable for AstPass {
            #[inline]
            fn hash_stable<__Hcx: ::rustc_data_structures::stable_hasher::HashStableContext>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                ::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
                match *self {
                    AstPass::StdImports => {}
                    AstPass::TestHarness => {}
                    AstPass::ProcMacroHarness => {}
                }
            }
        }
    };HashStable)]
1180pub enum AstPass {
1181    StdImports,
1182    TestHarness,
1183    ProcMacroHarness,
1184}
1185
1186impl AstPass {
1187    pub fn descr(self) -> &'static str {
1188        match self {
1189            AstPass::StdImports => "standard library imports",
1190            AstPass::TestHarness => "test harness",
1191            AstPass::ProcMacroHarness => "proc macro harness",
1192        }
1193    }
1194}
1195
1196/// The kind of compiler desugaring.
1197#[derive(#[automatically_derived]
impl ::core::clone::Clone for DesugaringKind {
    #[inline]
    fn clone(&self) -> DesugaringKind {
        let _: ::core::clone::AssertParamIsClone<bool>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for DesugaringKind { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for DesugaringKind {
    #[inline]
    fn eq(&self, other: &DesugaringKind) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (DesugaringKind::FormatLiteral { source: __self_0 },
                    DesugaringKind::FormatLiteral { source: __arg1_0 }) =>
                    __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::fmt::Debug for DesugaringKind {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            DesugaringKind::QuestionMark =>
                ::core::fmt::Formatter::write_str(f, "QuestionMark"),
            DesugaringKind::TryBlock =>
                ::core::fmt::Formatter::write_str(f, "TryBlock"),
            DesugaringKind::YeetExpr =>
                ::core::fmt::Formatter::write_str(f, "YeetExpr"),
            DesugaringKind::OpaqueTy =>
                ::core::fmt::Formatter::write_str(f, "OpaqueTy"),
            DesugaringKind::Async =>
                ::core::fmt::Formatter::write_str(f, "Async"),
            DesugaringKind::Await =>
                ::core::fmt::Formatter::write_str(f, "Await"),
            DesugaringKind::ForLoop =>
                ::core::fmt::Formatter::write_str(f, "ForLoop"),
            DesugaringKind::WhileLoop =>
                ::core::fmt::Formatter::write_str(f, "WhileLoop"),
            DesugaringKind::BoundModifier =>
                ::core::fmt::Formatter::write_str(f, "BoundModifier"),
            DesugaringKind::Contract =>
                ::core::fmt::Formatter::write_str(f, "Contract"),
            DesugaringKind::PatTyRange =>
                ::core::fmt::Formatter::write_str(f, "PatTyRange"),
            DesugaringKind::FormatLiteral { source: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "FormatLiteral", "source", &__self_0),
            DesugaringKind::RangeExpr =>
                ::core::fmt::Formatter::write_str(f, "RangeExpr"),
        }
    }
}Debug, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for DesugaringKind {
            fn encode(&self, __encoder: &mut __E) {
                let disc =
                    match *self {
                        DesugaringKind::QuestionMark => { 0usize }
                        DesugaringKind::TryBlock => { 1usize }
                        DesugaringKind::YeetExpr => { 2usize }
                        DesugaringKind::OpaqueTy => { 3usize }
                        DesugaringKind::Async => { 4usize }
                        DesugaringKind::Await => { 5usize }
                        DesugaringKind::ForLoop => { 6usize }
                        DesugaringKind::WhileLoop => { 7usize }
                        DesugaringKind::BoundModifier => { 8usize }
                        DesugaringKind::Contract => { 9usize }
                        DesugaringKind::PatTyRange => { 10usize }
                        DesugaringKind::FormatLiteral { source: ref __binding_0 } =>
                            {
                            11usize
                        }
                        DesugaringKind::RangeExpr => { 12usize }
                    };
                ::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
                match *self {
                    DesugaringKind::QuestionMark => {}
                    DesugaringKind::TryBlock => {}
                    DesugaringKind::YeetExpr => {}
                    DesugaringKind::OpaqueTy => {}
                    DesugaringKind::Async => {}
                    DesugaringKind::Await => {}
                    DesugaringKind::ForLoop => {}
                    DesugaringKind::WhileLoop => {}
                    DesugaringKind::BoundModifier => {}
                    DesugaringKind::Contract => {}
                    DesugaringKind::PatTyRange => {}
                    DesugaringKind::FormatLiteral { source: ref __binding_0 } =>
                        {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                    DesugaringKind::RangeExpr => {}
                }
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
            for DesugaringKind {
            fn decode(__decoder: &mut __D) -> Self {
                match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
                    {
                    0usize => { DesugaringKind::QuestionMark }
                    1usize => { DesugaringKind::TryBlock }
                    2usize => { DesugaringKind::YeetExpr }
                    3usize => { DesugaringKind::OpaqueTy }
                    4usize => { DesugaringKind::Async }
                    5usize => { DesugaringKind::Await }
                    6usize => { DesugaringKind::ForLoop }
                    7usize => { DesugaringKind::WhileLoop }
                    8usize => { DesugaringKind::BoundModifier }
                    9usize => { DesugaringKind::Contract }
                    10usize => { DesugaringKind::PatTyRange }
                    11usize => {
                        DesugaringKind::FormatLiteral {
                            source: ::rustc_serialize::Decodable::decode(__decoder),
                        }
                    }
                    12usize => { DesugaringKind::RangeExpr }
                    n => {
                        ::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `DesugaringKind`, expected 0..13, actual {0}",
                                n));
                    }
                }
            }
        }
    };Decodable, const _: () =
    {
        impl ::rustc_data_structures::stable_hasher::HashStable for
            DesugaringKind {
            #[inline]
            fn hash_stable<__Hcx: ::rustc_data_structures::stable_hasher::HashStableContext>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                ::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
                match *self {
                    DesugaringKind::QuestionMark => {}
                    DesugaringKind::TryBlock => {}
                    DesugaringKind::YeetExpr => {}
                    DesugaringKind::OpaqueTy => {}
                    DesugaringKind::Async => {}
                    DesugaringKind::Await => {}
                    DesugaringKind::ForLoop => {}
                    DesugaringKind::WhileLoop => {}
                    DesugaringKind::BoundModifier => {}
                    DesugaringKind::Contract => {}
                    DesugaringKind::PatTyRange => {}
                    DesugaringKind::FormatLiteral { source: ref __binding_0 } =>
                        {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                    }
                    DesugaringKind::RangeExpr => {}
                }
            }
        }
    };HashStable)]
1198pub enum DesugaringKind {
1199    QuestionMark,
1200    TryBlock,
1201    YeetExpr,
1202    /// Desugaring of an `impl Trait` in return type position
1203    /// to an `type Foo = impl Trait;` and replacing the
1204    /// `impl Trait` with `Foo`.
1205    OpaqueTy,
1206    Async,
1207    Await,
1208    ForLoop,
1209    WhileLoop,
1210    /// `async Fn()` bound modifier
1211    BoundModifier,
1212    /// Calls to contract checks (`#[requires]` to precond, `#[ensures]` to postcond)
1213    Contract,
1214    /// A pattern type range start/end
1215    PatTyRange,
1216    /// A format literal.
1217    FormatLiteral {
1218        /// Was this format literal written in the source?
1219        /// - `format!("boo")` => Yes,
1220        /// - `format!(concat!("b", "o", "o"))` => No,
1221        /// - `format!(include_str!("boo.txt"))` => No,
1222        ///
1223        /// If it wasn't written in the source then we have to be careful with suggestions about
1224        /// rewriting it.
1225        source: bool,
1226    },
1227    RangeExpr,
1228}
1229
1230impl DesugaringKind {
1231    /// The description wording should combine well with "desugaring of {}".
1232    pub fn descr(self) -> &'static str {
1233        match self {
1234            DesugaringKind::Async => "`async` block or function",
1235            DesugaringKind::Await => "`await` expression",
1236            DesugaringKind::QuestionMark => "operator `?`",
1237            DesugaringKind::TryBlock => "`try` block",
1238            DesugaringKind::YeetExpr => "`do yeet` expression",
1239            DesugaringKind::OpaqueTy => "`impl Trait`",
1240            DesugaringKind::ForLoop => "`for` loop",
1241            DesugaringKind::WhileLoop => "`while` loop",
1242            DesugaringKind::BoundModifier => "trait bound modifier",
1243            DesugaringKind::Contract => "contract check",
1244            DesugaringKind::PatTyRange => "pattern type",
1245            DesugaringKind::FormatLiteral { source: true } => "format string literal",
1246            DesugaringKind::FormatLiteral { source: false } => {
1247                "expression that expanded into a format string literal"
1248            }
1249            DesugaringKind::RangeExpr => "range expression",
1250        }
1251    }
1252
1253    /// For use with `rustc_unimplemented` to support conditions
1254    /// like `from_desugaring = "QuestionMark"`
1255    pub fn matches(&self, value: &str) -> bool {
1256        match self {
1257            DesugaringKind::Async => value == "Async",
1258            DesugaringKind::Await => value == "Await",
1259            DesugaringKind::QuestionMark => value == "QuestionMark",
1260            DesugaringKind::TryBlock => value == "TryBlock",
1261            DesugaringKind::YeetExpr => value == "YeetExpr",
1262            DesugaringKind::OpaqueTy => value == "OpaqueTy",
1263            DesugaringKind::ForLoop => value == "ForLoop",
1264            DesugaringKind::WhileLoop => value == "WhileLoop",
1265            DesugaringKind::BoundModifier => value == "BoundModifier",
1266            DesugaringKind::Contract => value == "Contract",
1267            DesugaringKind::PatTyRange => value == "PatTyRange",
1268            DesugaringKind::FormatLiteral { .. } => value == "FormatLiteral",
1269            DesugaringKind::RangeExpr => value == "RangeExpr",
1270        }
1271    }
1272}
1273
1274#[derive(#[automatically_derived]
impl ::core::default::Default for HygieneEncodeContext {
    #[inline]
    fn default() -> HygieneEncodeContext {
        HygieneEncodeContext {
            serialized_ctxts: ::core::default::Default::default(),
            latest_ctxts: ::core::default::Default::default(),
            serialized_expns: ::core::default::Default::default(),
            latest_expns: ::core::default::Default::default(),
        }
    }
}Default)]
1275pub struct HygieneEncodeContext {
1276    /// All `SyntaxContexts` for which we have written `SyntaxContextData` into crate metadata.
1277    /// This is `None` after we finish encoding `SyntaxContexts`, to ensure
1278    /// that we don't accidentally try to encode any more `SyntaxContexts`
1279    serialized_ctxts: Lock<FxHashSet<SyntaxContext>>,
1280    /// The `SyntaxContexts` that we have serialized (e.g. as a result of encoding `Spans`)
1281    /// in the most recent 'round' of serializing. Serializing `SyntaxContextData`
1282    /// may cause us to serialize more `SyntaxContext`s, so serialize in a loop
1283    /// until we reach a fixed point.
1284    latest_ctxts: Lock<FxHashSet<SyntaxContext>>,
1285
1286    serialized_expns: Lock<FxHashSet<ExpnId>>,
1287
1288    latest_expns: Lock<FxHashSet<ExpnId>>,
1289}
1290
1291impl HygieneEncodeContext {
1292    /// Record the fact that we need to serialize the corresponding `ExpnData`.
1293    pub fn schedule_expn_data_for_encoding(&self, expn: ExpnId) {
1294        if !self.serialized_expns.lock().contains(&expn) {
1295            self.latest_expns.lock().insert(expn);
1296        }
1297    }
1298
1299    pub fn encode<T>(
1300        &self,
1301        encoder: &mut T,
1302        mut encode_ctxt: impl FnMut(&mut T, u32, &SyntaxContextKey),
1303        mut encode_expn: impl FnMut(&mut T, ExpnId, &ExpnData, ExpnHash),
1304    ) {
1305        // When we serialize a `SyntaxContextData`, we may end up serializing
1306        // a `SyntaxContext` that we haven't seen before
1307        while !self.latest_ctxts.lock().is_empty() || !self.latest_expns.lock().is_empty() {
1308            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_span/src/hygiene.rs:1308",
                        "rustc_span::hygiene", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_span/src/hygiene.rs"),
                        ::tracing_core::__macro_support::Option::Some(1308u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_span::hygiene"),
                        ::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!("encode_hygiene: Serializing a round of {0:?} SyntaxContextData: {1:?}",
                                                    self.latest_ctxts.lock().len(), self.latest_ctxts) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(
1309                "encode_hygiene: Serializing a round of {:?} SyntaxContextData: {:?}",
1310                self.latest_ctxts.lock().len(),
1311                self.latest_ctxts
1312            );
1313
1314            // Consume the current round of syntax contexts.
1315            // Drop the lock() temporary early.
1316            // It's fine to iterate over a HashMap, because the serialization of the table
1317            // that we insert data into doesn't depend on insertion order.
1318            #[allow(rustc::potential_query_instability)]
1319            let latest_ctxts = { mem::take(&mut *self.latest_ctxts.lock()) }.into_iter();
1320            let all_ctxt_data: Vec<_> = HygieneData::with(|data| {
1321                latest_ctxts
1322                    .map(|ctxt| (ctxt, data.syntax_context_data[ctxt.0 as usize].key()))
1323                    .collect()
1324            });
1325            for (ctxt, ctxt_key) in all_ctxt_data {
1326                if self.serialized_ctxts.lock().insert(ctxt) {
1327                    encode_ctxt(encoder, ctxt.0, &ctxt_key);
1328                }
1329            }
1330
1331            // Same as above, but for expansions instead of syntax contexts.
1332            #[allow(rustc::potential_query_instability)]
1333            let latest_expns = { mem::take(&mut *self.latest_expns.lock()) }.into_iter();
1334            let all_expn_data: Vec<_> = HygieneData::with(|data| {
1335                latest_expns
1336                    .map(|expn| (expn, data.expn_data(expn).clone(), data.expn_hash(expn)))
1337                    .collect()
1338            });
1339            for (expn, expn_data, expn_hash) in all_expn_data {
1340                if self.serialized_expns.lock().insert(expn) {
1341                    encode_expn(encoder, expn, &expn_data, expn_hash);
1342                }
1343            }
1344        }
1345        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_span/src/hygiene.rs:1345",
                        "rustc_span::hygiene", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_span/src/hygiene.rs"),
                        ::tracing_core::__macro_support::Option::Some(1345u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_span::hygiene"),
                        ::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!("encode_hygiene: Done serializing SyntaxContextData")
                                            as &dyn Value))])
            });
    } else { ; }
};debug!("encode_hygiene: Done serializing SyntaxContextData");
1346    }
1347}
1348
1349/// Additional information used to assist in decoding hygiene data
1350#[derive(#[automatically_derived]
impl ::core::default::Default for HygieneDecodeContext {
    #[inline]
    fn default() -> HygieneDecodeContext {
        HygieneDecodeContext {
            remapped_ctxts: ::core::default::Default::default(),
        }
    }
}Default)]
1351pub struct HygieneDecodeContext {
1352    // A cache mapping raw serialized per-crate syntax context ids to corresponding decoded
1353    // `SyntaxContext`s in the current global `HygieneData`.
1354    remapped_ctxts: Lock<IndexVec<u32, Option<SyntaxContext>>>,
1355}
1356
1357/// Register an expansion which has been decoded from the on-disk-cache for the local crate.
1358pub fn register_local_expn_id(data: ExpnData, hash: ExpnHash) -> ExpnId {
1359    HygieneData::with(|hygiene_data| {
1360        let expn_id = hygiene_data.local_expn_data.next_index();
1361        hygiene_data.local_expn_data.push(Some(data));
1362        let _eid = hygiene_data.local_expn_hashes.push(hash);
1363        if true {
    match (&expn_id, &_eid) {
        (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!(expn_id, _eid);
1364
1365        let expn_id = expn_id.to_expn_id();
1366
1367        let _old_id = hygiene_data.expn_hash_to_expn_id.insert(hash, expn_id);
1368        if true {
    if !_old_id.is_none() {
        ::core::panicking::panic("assertion failed: _old_id.is_none()")
    };
};debug_assert!(_old_id.is_none());
1369        expn_id
1370    })
1371}
1372
1373/// Register an expansion which has been decoded from the metadata of a foreign crate.
1374pub fn register_expn_id(
1375    krate: CrateNum,
1376    local_id: ExpnIndex,
1377    data: ExpnData,
1378    hash: ExpnHash,
1379) -> ExpnId {
1380    if true {
    if !(data.parent == ExpnId::root() || krate == data.parent.krate) {
        ::core::panicking::panic("assertion failed: data.parent == ExpnId::root() || krate == data.parent.krate")
    };
};debug_assert!(data.parent == ExpnId::root() || krate == data.parent.krate);
1381    let expn_id = ExpnId { krate, local_id };
1382    HygieneData::with(|hygiene_data| {
1383        let _old_data = hygiene_data.foreign_expn_data.insert(expn_id, data);
1384        let _old_hash = hygiene_data.foreign_expn_hashes.insert(expn_id, hash);
1385        if true {
    if !(_old_hash.is_none() || _old_hash == Some(hash)) {
        ::core::panicking::panic("assertion failed: _old_hash.is_none() || _old_hash == Some(hash)")
    };
};debug_assert!(_old_hash.is_none() || _old_hash == Some(hash));
1386        let _old_id = hygiene_data.expn_hash_to_expn_id.insert(hash, expn_id);
1387        if true {
    if !(_old_id.is_none() || _old_id == Some(expn_id)) {
        ::core::panicking::panic("assertion failed: _old_id.is_none() || _old_id == Some(expn_id)")
    };
};debug_assert!(_old_id.is_none() || _old_id == Some(expn_id));
1388    });
1389    expn_id
1390}
1391
1392/// Decode an expansion from the metadata of a foreign crate.
1393pub fn decode_expn_id(
1394    krate: CrateNum,
1395    index: u32,
1396    decode_data: impl FnOnce(ExpnId) -> (ExpnData, ExpnHash),
1397) -> ExpnId {
1398    if index == 0 {
1399        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_span/src/hygiene.rs:1399",
                        "rustc_span::hygiene", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_span/src/hygiene.rs"),
                        ::tracing_core::__macro_support::Option::Some(1399u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_span::hygiene"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::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!("decode_expn_id: deserialized root")
                                            as &dyn Value))])
            });
    } else { ; }
};trace!("decode_expn_id: deserialized root");
1400        return ExpnId::root();
1401    }
1402
1403    let index = ExpnIndex::from_u32(index);
1404
1405    // This function is used to decode metadata, so it cannot decode information about LOCAL_CRATE.
1406    if true {
    match (&krate, &LOCAL_CRATE) {
        (left_val, right_val) => {
            if *left_val == *right_val {
                let kind = ::core::panicking::AssertKind::Ne;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_ne!(krate, LOCAL_CRATE);
1407    let expn_id = ExpnId { krate, local_id: index };
1408
1409    // Fast path if the expansion has already been decoded.
1410    if HygieneData::with(|hygiene_data| hygiene_data.foreign_expn_data.contains_key(&expn_id)) {
1411        return expn_id;
1412    }
1413
1414    // Don't decode the data inside `HygieneData::with`, since we need to recursively decode
1415    // other ExpnIds
1416    let (expn_data, hash) = decode_data(expn_id);
1417
1418    register_expn_id(krate, index, expn_data, hash)
1419}
1420
1421// Decodes `SyntaxContext`, using the provided `HygieneDecodeContext`
1422// to track which `SyntaxContext`s we have already decoded.
1423// The provided closure will be invoked to deserialize a `SyntaxContextData`
1424// if we haven't already seen the id of the `SyntaxContext` we are deserializing.
1425pub fn decode_syntax_context<D: Decoder>(
1426    d: &mut D,
1427    context: &HygieneDecodeContext,
1428    decode_data: impl FnOnce(&mut D, u32) -> SyntaxContextKey,
1429) -> SyntaxContext {
1430    let raw_id: u32 = Decodable::decode(d);
1431    if raw_id == 0 {
1432        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_span/src/hygiene.rs:1432",
                        "rustc_span::hygiene", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_span/src/hygiene.rs"),
                        ::tracing_core::__macro_support::Option::Some(1432u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_span::hygiene"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::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!("decode_syntax_context: deserialized root")
                                            as &dyn Value))])
            });
    } else { ; }
};trace!("decode_syntax_context: deserialized root");
1433        // The root is special
1434        return SyntaxContext::root();
1435    }
1436
1437    // Look into the cache first.
1438    // Reminder: `HygieneDecodeContext` is per-crate, so there are no collisions between
1439    // raw ids from different crate metadatas.
1440    if let Some(Some(ctxt)) = context.remapped_ctxts.lock().get(raw_id) {
1441        return *ctxt;
1442    }
1443
1444    // Don't try to decode data while holding the lock, since we need to
1445    // be able to recursively decode a SyntaxContext
1446    let (parent, expn_id, transparency) = decode_data(d, raw_id);
1447    let ctxt =
1448        HygieneData::with(|hygiene_data| hygiene_data.alloc_ctxt(parent, expn_id, transparency));
1449
1450    context.remapped_ctxts.lock().insert(raw_id, ctxt);
1451
1452    ctxt
1453}
1454
1455impl<E: SpanEncoder> Encodable<E> for LocalExpnId {
1456    fn encode(&self, e: &mut E) {
1457        self.to_expn_id().encode(e);
1458    }
1459}
1460
1461impl<D: SpanDecoder> Decodable<D> for LocalExpnId {
1462    fn decode(d: &mut D) -> Self {
1463        ExpnId::expect_local(ExpnId::decode(d))
1464    }
1465}
1466
1467pub fn raw_encode_syntax_context(
1468    ctxt: SyntaxContext,
1469    context: &HygieneEncodeContext,
1470    e: &mut impl Encoder,
1471) {
1472    if !context.serialized_ctxts.lock().contains(&ctxt) {
1473        context.latest_ctxts.lock().insert(ctxt);
1474    }
1475    ctxt.0.encode(e);
1476}
1477
1478/// Updates the `disambiguator` field of the corresponding `ExpnData`
1479/// such that the `Fingerprint` of the `ExpnData` does not collide with
1480/// any other `ExpnIds`.
1481///
1482/// This method is called only when an `ExpnData` is first associated
1483/// with an `ExpnId` (when the `ExpnId` is initially constructed, or via
1484/// `set_expn_data`). It is *not* called for foreign `ExpnId`s deserialized
1485/// from another crate's metadata - since `ExpnHash` includes the stable crate id,
1486/// collisions are only possible between `ExpnId`s within the same crate.
1487fn update_disambiguator(expn_data: &mut ExpnData, mut hcx: impl HashStableContext) -> ExpnHash {
1488    // This disambiguator should not have been set yet.
1489    match (&expn_data.disambiguator, &0) {
    (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::Some(format_args!("Already set disambiguator for ExpnData: {0:?}",
                        expn_data)));
        }
    }
};assert_eq!(expn_data.disambiguator, 0, "Already set disambiguator for ExpnData: {expn_data:?}");
1490    hcx.assert_default_hashing_controls("ExpnData (disambiguator)");
1491    let mut expn_hash = expn_data.hash_expn(&mut hcx);
1492
1493    let disambiguator = HygieneData::with(|data| {
1494        // If this is the first ExpnData with a given hash, then keep our
1495        // disambiguator at 0 (the default u32 value)
1496        let disambig = data.expn_data_disambiguators.entry(expn_hash).or_default();
1497        let disambiguator = *disambig;
1498        *disambig += 1;
1499        disambiguator
1500    });
1501
1502    if disambiguator != 0 {
1503        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_span/src/hygiene.rs:1503",
                        "rustc_span::hygiene", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_span/src/hygiene.rs"),
                        ::tracing_core::__macro_support::Option::Some(1503u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_span::hygiene"),
                        ::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!("Set disambiguator for expn_data={0:?} expn_hash={1:?}",
                                                    expn_data, expn_hash) as &dyn Value))])
            });
    } else { ; }
};debug!("Set disambiguator for expn_data={:?} expn_hash={:?}", expn_data, expn_hash);
1504
1505        expn_data.disambiguator = disambiguator;
1506        expn_hash = expn_data.hash_expn(&mut hcx);
1507
1508        // Verify that the new disambiguator makes the hash unique
1509        #[cfg(debug_assertions)]
1510        HygieneData::with(|data| {
1511            match (&data.expn_data_disambiguators.get(&expn_hash), &None) {
    (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::Some(format_args!("Hash collision after disambiguator update!")));
        }
    }
};assert_eq!(
1512                data.expn_data_disambiguators.get(&expn_hash),
1513                None,
1514                "Hash collision after disambiguator update!",
1515            );
1516        });
1517    }
1518
1519    ExpnHash::new(LOCAL_CRATE.as_def_id().to_stable_hash_key(&mut hcx).stable_crate_id(), expn_hash)
1520}
1521
1522impl HashStable for SyntaxContext {
1523    fn hash_stable<Hcx: HashStableContext>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
1524        const TAG_EXPANSION: u8 = 0;
1525        const TAG_NO_EXPANSION: u8 = 1;
1526
1527        if self.is_root() {
1528            TAG_NO_EXPANSION.hash_stable(hcx, hasher);
1529        } else {
1530            TAG_EXPANSION.hash_stable(hcx, hasher);
1531            let (expn_id, transparency) = self.outer_mark();
1532            expn_id.hash_stable(hcx, hasher);
1533            transparency.hash_stable(hcx, hasher);
1534        }
1535    }
1536}
1537
1538impl HashStable for ExpnId {
1539    fn hash_stable<Hcx: HashStableContext>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
1540        hcx.assert_default_hashing_controls("ExpnId");
1541        let hash = if *self == ExpnId::root() {
1542            // Avoid fetching TLS storage for a trivial often-used value.
1543            Fingerprint::ZERO
1544        } else {
1545            self.expn_hash().0
1546        };
1547
1548        hash.hash_stable(hcx, hasher);
1549    }
1550}
1551
1552impl HashStable for LocalExpnId {
1553    fn hash_stable<Hcx: HashStableContext>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
1554        self.to_expn_id().hash_stable(hcx, hasher);
1555    }
1556}