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