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//! Also see <https://rustc-dev-guide.rust-lang.org/macro-expansion.html#hygiene-and-hierarchies>
8//!
9//! # The Expansion Order Hierarchy
10//!
11//! `ExpnData` in rustc, rust-analyzer's version is [`MacroCallLoc`]. Traversing the hierarchy
12//! upwards can be achieved by walking up [`MacroCallLoc::kind`]'s contained file id, as
13//! [`MacroFile`]s are interned [`MacroCallLoc`]s.
14//!
15//! # The Macro Definition Hierarchy
16//!
17//! `SyntaxContextData` in rustc and rust-analyzer. Basically the same in both.
18//!
19//! # The Call-site Hierarchy
20//!
21//! `ExpnData::call_site` in rustc, [`MacroCallLoc::call_site`] in rust-analyzer.
22use std::fmt;
23
24use crate::Edition;
25
26/// A syntax context describes a hierarchy tracking order of macro definitions.
27#[cfg(feature = "salsa")]
28#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
29pub struct SyntaxContext(
30    /// # Invariant
31    ///
32    /// This is either a valid `salsa::Id` or a root `SyntaxContext`.
33    u32,
34    std::marker::PhantomData<&'static salsa::plumbing::interned::Value<SyntaxContext>>,
35);
36
37#[cfg(feature = "salsa")]
38const _: () = {
39    use crate::MacroCallId;
40    use salsa::plumbing as zalsa_;
41    use salsa::plumbing::interned as zalsa_struct_;
42
43    #[derive(Clone, Eq, Debug)]
44    pub struct SyntaxContextData {
45        outer_expn: Option<MacroCallId>,
46        outer_transparency: Transparency,
47        edition: Edition,
48        parent: SyntaxContext,
49        opaque: SyntaxContext,
50        opaque_and_semitransparent: SyntaxContext,
51    }
52
53    impl PartialEq for SyntaxContextData {
54        fn eq(&self, other: &Self) -> bool {
55            self.outer_expn == other.outer_expn
56                && self.outer_transparency == other.outer_transparency
57                && self.edition == other.edition
58                && self.parent == other.parent
59        }
60    }
61
62    impl std::hash::Hash for SyntaxContextData {
63        fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
64            self.outer_expn.hash(state);
65            self.outer_transparency.hash(state);
66            self.edition.hash(state);
67            self.parent.hash(state);
68        }
69    }
70
71    impl zalsa_::HasJar for SyntaxContext {
72        type Jar = zalsa_struct_::JarImpl<SyntaxContext>;
73        const KIND: zalsa_::JarKind = zalsa_::JarKind::Struct;
74    }
75
76    zalsa_::register_jar! {
77        zalsa_::ErasedJar::erase::<SyntaxContext>()
78    }
79
80    /// Key to use during hash lookups. Each field is some type that implements `Lookup<T>`
81    /// for the owned type. This permits interning with an `&str` when a `String` is required and so forth.
82    #[derive(Hash)]
83    struct StructKey<'db, T0, T1, T2, T3>(T0, T1, T2, T3, std::marker::PhantomData<&'db ()>);
84
85    impl<'db, T0, T1, T2, T3> zalsa_::interned::HashEqLike<StructKey<'db, T0, T1, T2, T3>>
86        for SyntaxContextData
87    where
88        Option<MacroCallId>: zalsa_::interned::HashEqLike<T0>,
89        Transparency: zalsa_::interned::HashEqLike<T1>,
90        Edition: zalsa_::interned::HashEqLike<T2>,
91        SyntaxContext: zalsa_::interned::HashEqLike<T3>,
92    {
93        fn hash<H: std::hash::Hasher>(&self, h: &mut H) {
94            zalsa_::interned::HashEqLike::<T0>::hash(&self.outer_expn, &mut *h);
95            zalsa_::interned::HashEqLike::<T1>::hash(&self.outer_transparency, &mut *h);
96            zalsa_::interned::HashEqLike::<T2>::hash(&self.edition, &mut *h);
97            zalsa_::interned::HashEqLike::<T3>::hash(&self.parent, &mut *h);
98        }
99        fn eq(&self, data: &StructKey<'db, T0, T1, T2, T3>) -> bool {
100            zalsa_::interned::HashEqLike::<T0>::eq(&self.outer_expn, &data.0)
101                && zalsa_::interned::HashEqLike::<T1>::eq(&self.outer_transparency, &data.1)
102                && zalsa_::interned::HashEqLike::<T2>::eq(&self.edition, &data.2)
103                && zalsa_::interned::HashEqLike::<T3>::eq(&self.parent, &data.3)
104        }
105    }
106    impl zalsa_struct_::Configuration for SyntaxContext {
107        const LOCATION: salsa::plumbing::Location =
108            salsa::plumbing::Location { file: file!(), line: line!() };
109        const DEBUG_NAME: &'static str = "SyntaxContextData";
110        const REVISIONS: std::num::NonZeroUsize = std::num::NonZeroUsize::MAX;
111        const PERSIST: bool = false;
112
113        type Fields<'a> = SyntaxContextData;
114        type Struct<'a> = SyntaxContext;
115
116        fn serialize<S>(_: &Self::Fields<'_>, _: S) -> Result<S::Ok, S::Error>
117        where
118            S: zalsa_::serde::Serializer,
119        {
120            unimplemented!("attempted to serialize value that set `PERSIST` to false")
121        }
122
123        fn deserialize<'de, D>(_: D) -> Result<Self::Fields<'static>, D::Error>
124        where
125            D: zalsa_::serde::Deserializer<'de>,
126        {
127            unimplemented!("attempted to deserialize value that cannot set `PERSIST` to false");
128        }
129    }
130
131    impl SyntaxContext {
132        pub fn ingredient(zalsa: &zalsa_::Zalsa) -> &zalsa_struct_::IngredientImpl<Self> {
133            static CACHE: zalsa_::IngredientCache<zalsa_struct_::IngredientImpl<SyntaxContext>> =
134                zalsa_::IngredientCache::new();
135
136            // SAFETY: `lookup_jar_by_type` returns a valid ingredient index, and the only
137            // ingredient created by our jar is the struct ingredient.
138            unsafe {
139                CACHE.get_or_create(zalsa, || {
140                    zalsa.lookup_jar_by_type::<zalsa_struct_::JarImpl<SyntaxContext>>()
141                })
142            }
143        }
144    }
145    impl zalsa_::AsId for SyntaxContext {
146        fn as_id(&self) -> salsa::Id {
147            self.as_salsa_id().expect("`SyntaxContext::as_id()` called on a root `SyntaxContext`")
148        }
149    }
150    impl zalsa_::FromId for SyntaxContext {
151        fn from_id(id: salsa::Id) -> Self {
152            Self::from_salsa_id(id)
153        }
154    }
155    unsafe impl Send for SyntaxContext {}
156
157    unsafe impl Sync for SyntaxContext {}
158
159    impl zalsa_::SalsaStructInDb for SyntaxContext {
160        type MemoIngredientMap = salsa::plumbing::MemoIngredientSingletonIndex;
161
162        fn lookup_ingredient_index(aux: &zalsa_::Zalsa) -> salsa::plumbing::IngredientIndices {
163            aux.lookup_jar_by_type::<zalsa_struct_::JarImpl<SyntaxContext>>().into()
164        }
165
166        fn entries(zalsa: &zalsa_::Zalsa) -> impl Iterator<Item = zalsa_::DatabaseKeyIndex> + '_ {
167            let _ingredient_index =
168                zalsa.lookup_jar_by_type::<zalsa_struct_::JarImpl<SyntaxContext>>();
169            <SyntaxContext>::ingredient(zalsa).entries(zalsa).map(|entry| entry.key())
170        }
171
172        #[inline]
173        fn cast(id: salsa::Id, type_id: std::any::TypeId) -> Option<Self> {
174            if type_id == std::any::TypeId::of::<SyntaxContext>() {
175                Some(<Self as salsa::plumbing::FromId>::from_id(id))
176            } else {
177                None
178            }
179        }
180
181        #[inline]
182        unsafe fn memo_table(
183            zalsa: &zalsa_::Zalsa,
184            id: zalsa_::Id,
185            current_revision: zalsa_::Revision,
186        ) -> zalsa_::MemoTableWithTypes<'_> {
187            // SAFETY: Guaranteed by caller.
188            unsafe {
189                zalsa.table().memos::<zalsa_struct_::Value<SyntaxContext>>(id, current_revision)
190            }
191        }
192    }
193
194    unsafe impl salsa::plumbing::Update for SyntaxContext {
195        unsafe fn maybe_update(old_pointer: *mut Self, new_value: Self) -> bool {
196            if unsafe { *old_pointer } != new_value {
197                unsafe { *old_pointer = new_value };
198                true
199            } else {
200                false
201            }
202        }
203    }
204    impl<'db> SyntaxContext {
205        pub fn new<
206            Db,
207            T0: zalsa_::interned::Lookup<Option<MacroCallId>> + std::hash::Hash,
208            T1: zalsa_::interned::Lookup<Transparency> + std::hash::Hash,
209            T2: zalsa_::interned::Lookup<Edition> + std::hash::Hash,
210            T3: zalsa_::interned::Lookup<SyntaxContext> + std::hash::Hash,
211        >(
212            db: &'db Db,
213            outer_expn: T0,
214            outer_transparency: T1,
215            edition: T2,
216            parent: T3,
217            opaque: impl FnOnce(SyntaxContext) -> SyntaxContext,
218            opaque_and_semitransparent: impl FnOnce(SyntaxContext) -> SyntaxContext,
219        ) -> Self
220        where
221            Db: ?Sized + salsa::Database,
222            Option<MacroCallId>: zalsa_::interned::HashEqLike<T0>,
223            Transparency: zalsa_::interned::HashEqLike<T1>,
224            Edition: zalsa_::interned::HashEqLike<T2>,
225            SyntaxContext: zalsa_::interned::HashEqLike<T3>,
226        {
227            let (zalsa, zalsa_local) = db.zalsas();
228
229            SyntaxContext::ingredient(zalsa).intern(
230                zalsa,
231                zalsa_local,
232                StructKey::<'db>(
233                    outer_expn,
234                    outer_transparency,
235                    edition,
236                    parent,
237                    std::marker::PhantomData,
238                ),
239                |id, data| SyntaxContextData {
240                    outer_expn: zalsa_::interned::Lookup::into_owned(data.0),
241                    outer_transparency: zalsa_::interned::Lookup::into_owned(data.1),
242                    edition: zalsa_::interned::Lookup::into_owned(data.2),
243                    parent: zalsa_::interned::Lookup::into_owned(data.3),
244                    opaque: opaque(zalsa_::FromId::from_id(id)),
245                    opaque_and_semitransparent: opaque_and_semitransparent(
246                        zalsa_::FromId::from_id(id),
247                    ),
248                },
249            )
250        }
251
252        /// Invariant: Only the root [`SyntaxContext`] has a [`None`] outer expansion.
253        // FIXME: The None case needs to encode the context crate id. We can encode that as the MSB of
254        // MacroCallId is reserved anyways so we can do bit tagging here just fine.
255        // The bigger issue is that this will cause interning to now create completely separate chains
256        // per crate. Though that is likely not a problem as `MacroCallId`s are already crate calling dependent.
257        pub fn outer_expn<Db>(self, db: &'db Db) -> Option<MacroCallId>
258        where
259            Db: ?Sized + zalsa_::Database,
260        {
261            let id = self.as_salsa_id()?;
262            let zalsa = db.zalsa();
263            let fields = SyntaxContext::ingredient(zalsa).data(zalsa, id);
264            fields.outer_expn
265        }
266
267        pub fn outer_transparency<Db>(self, db: &'db Db) -> Transparency
268        where
269            Db: ?Sized + zalsa_::Database,
270        {
271            let Some(id) = self.as_salsa_id() else { return Transparency::Opaque };
272            let zalsa = db.zalsa();
273            let fields = SyntaxContext::ingredient(zalsa).data(zalsa, id);
274            fields.outer_transparency
275        }
276
277        pub fn edition<Db>(self, db: &'db Db) -> Edition
278        where
279            Db: ?Sized + zalsa_::Database,
280        {
281            match self.as_salsa_id() {
282                Some(id) => {
283                    let zalsa = db.zalsa();
284                    let fields = SyntaxContext::ingredient(zalsa).data(zalsa, id);
285                    fields.edition
286                }
287                None => Edition::from_u32(SyntaxContext::MAX_ID - self.into_u32()),
288            }
289        }
290
291        pub fn parent<Db>(self, db: &'db Db) -> SyntaxContext
292        where
293            Db: ?Sized + zalsa_::Database,
294        {
295            match self.as_salsa_id() {
296                Some(id) => {
297                    let zalsa = db.zalsa();
298                    let fields = SyntaxContext::ingredient(zalsa).data(zalsa, id);
299                    fields.parent
300                }
301                None => self,
302            }
303        }
304
305        /// This context, but with all transparent and semi-transparent expansions filtered away.
306        pub fn opaque<Db>(self, db: &'db Db) -> SyntaxContext
307        where
308            Db: ?Sized + zalsa_::Database,
309        {
310            match self.as_salsa_id() {
311                Some(id) => {
312                    let zalsa = db.zalsa();
313                    let fields = SyntaxContext::ingredient(zalsa).data(zalsa, id);
314                    fields.opaque
315                }
316                None => self,
317            }
318        }
319
320        /// This context, but with all transparent expansions filtered away.
321        pub fn opaque_and_semitransparent<Db>(self, db: &'db Db) -> SyntaxContext
322        where
323            Db: ?Sized + zalsa_::Database,
324        {
325            match self.as_salsa_id() {
326                Some(id) => {
327                    let zalsa = db.zalsa();
328                    let fields = SyntaxContext::ingredient(zalsa).data(zalsa, id);
329                    fields.opaque_and_semitransparent
330                }
331                None => self,
332            }
333        }
334    }
335};
336
337impl SyntaxContext {
338    #[inline]
339    pub fn is_root(self) -> bool {
340        (SyntaxContext::MAX_ID - Edition::LATEST as u32) <= self.into_u32()
341            && self.into_u32() <= (SyntaxContext::MAX_ID - Edition::Edition2015 as u32)
342    }
343
344    #[inline]
345    pub fn remove_root_edition(&mut self) {
346        if self.is_root() {
347            *self = Self::root(Edition::Edition2015);
348        }
349    }
350
351    /// The root context, which is the parent of all other contexts. All `FileId`s have this context.
352    #[inline]
353    pub const fn root(edition: Edition) -> Self {
354        let edition = edition as u32;
355        // SAFETY: Roots are valid `SyntaxContext`s
356        unsafe { SyntaxContext::from_u32(SyntaxContext::MAX_ID - edition) }
357    }
358}
359
360#[cfg(feature = "salsa")]
361impl<'db> SyntaxContext {
362    const MAX_ID: u32 = salsa::Id::MAX_U32 - 1;
363
364    #[inline]
365    pub const fn into_u32(self) -> u32 {
366        self.0
367    }
368
369    /// # Safety
370    ///
371    /// The ID must be a valid `SyntaxContext`.
372    #[inline]
373    pub const unsafe fn from_u32(u32: u32) -> Self {
374        // INVARIANT: Our precondition.
375        Self(u32, std::marker::PhantomData)
376    }
377
378    #[inline]
379    fn as_salsa_id(self) -> Option<salsa::Id> {
380        if self.is_root() {
381            None
382        } else {
383            // SAFETY: By our invariant, this is either a root (which we verified it's not) or a valid `salsa::Id`.
384            unsafe { Some(salsa::Id::from_index(self.0)) }
385        }
386    }
387
388    #[inline]
389    fn from_salsa_id(id: salsa::Id) -> Self {
390        // SAFETY: This comes from a Salsa ID.
391        unsafe { Self::from_u32(id.index()) }
392    }
393
394    #[inline]
395    pub fn outer_mark(
396        self,
397        db: &'db dyn salsa::Database,
398    ) -> (Option<crate::MacroCallId>, Transparency) {
399        (self.outer_expn(db), self.outer_transparency(db))
400    }
401
402    #[inline]
403    pub fn normalize_to_macros_2_0(self, db: &'db dyn salsa::Database) -> SyntaxContext {
404        self.opaque(db)
405    }
406
407    #[inline]
408    pub fn normalize_to_macro_rules(self, db: &'db dyn salsa::Database) -> SyntaxContext {
409        self.opaque_and_semitransparent(db)
410    }
411
412    pub fn is_opaque(self, db: &'db dyn salsa::Database) -> bool {
413        !self.is_root() && self.outer_transparency(db).is_opaque()
414    }
415
416    pub fn remove_mark(
417        &mut self,
418        db: &'db dyn salsa::Database,
419    ) -> (Option<crate::MacroCallId>, Transparency) {
420        let data = *self;
421        *self = data.parent(db);
422        (data.outer_expn(db), data.outer_transparency(db))
423    }
424
425    pub fn marks(
426        self,
427        db: &'db dyn salsa::Database,
428    ) -> impl Iterator<Item = (crate::MacroCallId, Transparency)> {
429        let mut marks = self.marks_rev(db).collect::<Vec<_>>();
430        marks.reverse();
431        marks.into_iter()
432    }
433
434    pub fn marks_rev(
435        self,
436        db: &'db dyn salsa::Database,
437    ) -> impl Iterator<Item = (crate::MacroCallId, Transparency)> {
438        std::iter::successors(Some(self), move |&mark| Some(mark.parent(db)))
439            .take_while(|&it| !it.is_root())
440            .map(|ctx| {
441                let mark = ctx.outer_mark(db);
442                // We stop before taking the root expansion, as such we cannot encounter a `None` outer
443                // expansion, as only the ROOT has it.
444                (mark.0.unwrap(), mark.1)
445            })
446    }
447}
448#[cfg(not(feature = "salsa"))]
449#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
450pub struct SyntaxContext(u32);
451
452#[allow(dead_code)]
453const SALSA_MAX_ID_MIRROR: u32 = u32::MAX - 0xFF;
454#[cfg(feature = "salsa")]
455const _: () = assert!(salsa::Id::MAX_U32 == SALSA_MAX_ID_MIRROR);
456
457#[cfg(not(feature = "salsa"))]
458impl SyntaxContext {
459    const MAX_ID: u32 = SALSA_MAX_ID_MIRROR - 1;
460
461    pub const fn into_u32(self) -> u32 {
462        self.0
463    }
464
465    /// # Safety
466    ///
467    /// None. This is always safe to call without the `salsa` feature.
468    pub const unsafe fn from_u32(u32: u32) -> Self {
469        Self(u32)
470    }
471}
472
473/// A property of a macro expansion that determines how identifiers
474/// produced by that expansion are resolved.
475#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Hash, Debug)]
476pub enum Transparency {
477    /// Identifier produced by a transparent expansion is always resolved at call-site.
478    /// Call-site spans in procedural macros, hygiene opt-out in `macro` should use this.
479    Transparent,
480    /// Identifier produced by a semi-transparent expansion may be resolved
481    /// either at call-site or at definition-site.
482    /// If it's a local variable, label or `$crate` then it's resolved at def-site.
483    /// Otherwise it's resolved at call-site.
484    /// `macro_rules` macros behave like this, built-in macros currently behave like this too,
485    /// but that's an implementation detail.
486    SemiTransparent,
487    /// Identifier produced by an opaque expansion is always resolved at definition-site.
488    /// Def-site spans in procedural macros, identifiers from `macro` by default use this.
489    Opaque,
490}
491
492impl Transparency {
493    /// Returns `true` if the transparency is [`Opaque`].
494    ///
495    /// [`Opaque`]: Transparency::Opaque
496    pub fn is_opaque(&self) -> bool {
497        matches!(self, Self::Opaque)
498    }
499}
500
501impl fmt::Display for SyntaxContext {
502    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
503        if self.is_root() {
504            write!(f, "ROOT{}", Edition::from_u32(SyntaxContext::MAX_ID - self.into_u32()).number())
505        } else {
506            write!(f, "{}", self.into_u32())
507        }
508    }
509}
510
511impl std::fmt::Debug for SyntaxContext {
512    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
513        if f.alternate() {
514            fmt::Display::fmt(self, f)
515        } else {
516            f.debug_tuple("SyntaxContext").field(&self.0).finish()
517        }
518    }
519}