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