1 //! Machinery for hygienic macros.
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>.
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`.
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.
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.)
27 use crate::edition::Edition;
28 use crate::symbol::{kw, sym, Symbol};
29 use crate::SESSION_GLOBALS;
30 use crate::{BytePos, CachingSourceMapView, ExpnIdCache, SourceFile, Span, DUMMY_SP};
32 use crate::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
33 use rustc_data_structures::fingerprint::Fingerprint;
34 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
35 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
36 use rustc_data_structures::sync::{Lock, Lrc};
37 use rustc_macros::HashStable_Generic;
38 use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
41 use std::thread::LocalKey;
44 /// A `SyntaxContext` represents a chain of pairs `(ExpnId, Transparency)` named "marks".
45 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
46 pub struct SyntaxContext(u32);
48 #[derive(Debug, Encodable, Decodable, Clone)]
49 pub struct SyntaxContextData {
51 outer_transparency: Transparency,
52 parent: SyntaxContext,
53 /// This context, but with all transparent and semi-transparent expansions filtered away.
54 opaque: SyntaxContext,
55 /// This context, but with all transparent expansions filtered away.
56 opaque_and_semitransparent: SyntaxContext,
57 /// Name of the crate to which `$crate` with this context would resolve.
58 dollar_crate_name: Symbol,
61 /// A unique ID associated with a macro invocation and expansion.
62 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
63 pub struct ExpnId(u32);
65 /// A property of a macro expansion that determines how identifiers
66 /// produced by that expansion are resolved.
67 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Hash, Debug, Encodable, Decodable)]
68 #[derive(HashStable_Generic)]
69 pub enum Transparency {
70 /// Identifier produced by a transparent expansion is always resolved at call-site.
71 /// Call-site spans in procedural macros, hygiene opt-out in `macro` should use this.
73 /// Identifier produced by a semi-transparent expansion may be resolved
74 /// either at call-site or at definition-site.
75 /// If it's a local variable, label or `$crate` then it's resolved at def-site.
76 /// Otherwise it's resolved at call-site.
77 /// `macro_rules` macros behave like this, built-in macros currently behave like this too,
78 /// but that's an implementation detail.
80 /// Identifier produced by an opaque expansion is always resolved at definition-site.
81 /// Def-site spans in procedural macros, identifiers from `macro` by default use this.
86 pub fn fresh(expn_data: Option<ExpnData>) -> Self {
87 let has_data = expn_data.is_some();
88 let expn_id = HygieneData::with(|data| data.fresh_expn(expn_data));
90 update_disambiguator(expn_id);
95 /// The ID of the theoretical expansion that generates freshly parsed, unexpanded AST.
97 pub fn root() -> Self {
102 pub fn as_u32(self) -> u32 {
107 pub fn from_u32(raw: u32) -> ExpnId {
112 pub fn expn_data(self) -> ExpnData {
113 HygieneData::with(|data| data.expn_data(self).clone())
117 pub fn set_expn_data(self, mut expn_data: ExpnData) {
118 HygieneData::with(|data| {
119 let old_expn_data = &mut data.expn_data[self.0 as usize];
120 assert!(old_expn_data.is_none(), "expansion data is reset for an expansion ID");
121 expn_data.orig_id.replace(self.as_u32()).expect_none("orig_id should be None");
122 *old_expn_data = Some(expn_data);
124 update_disambiguator(self)
127 pub fn is_descendant_of(self, ancestor: ExpnId) -> bool {
128 HygieneData::with(|data| data.is_descendant_of(self, ancestor))
131 /// `expn_id.outer_expn_is_descendant_of(ctxt)` is equivalent to but faster than
132 /// `expn_id.is_descendant_of(ctxt.outer_expn())`.
133 pub fn outer_expn_is_descendant_of(self, ctxt: SyntaxContext) -> bool {
134 HygieneData::with(|data| data.is_descendant_of(self, data.outer_expn(ctxt)))
137 /// Returns span for the macro which originally caused this expansion to happen.
139 /// Stops backtracing at include! boundary.
140 pub fn expansion_cause(mut self) -> Option<Span> {
141 let mut last_macro = None;
143 let expn_data = self.expn_data();
144 // Stop going up the backtrace once include! is encountered
145 if expn_data.is_root()
146 || expn_data.kind == ExpnKind::Macro(MacroKind::Bang, sym::include)
150 self = expn_data.call_site.ctxt().outer_expn();
151 last_macro = Some(expn_data.call_site);
158 pub struct HygieneData {
159 /// Each expansion should have an associated expansion data, but sometimes there's a delay
160 /// between creation of an expansion ID and obtaining its data (e.g. macros are collected
161 /// first and then resolved later), so we use an `Option` here.
162 expn_data: Vec<Option<ExpnData>>,
163 syntax_context_data: Vec<SyntaxContextData>,
164 syntax_context_map: FxHashMap<(SyntaxContext, ExpnId, Transparency), SyntaxContext>,
165 /// Maps the `Fingerprint` of an `ExpnData` to the next disambiguator value.
166 /// This is used by `update_disambiguator` to keep track of which `ExpnData`s
167 /// would have collisions without a disambiguator.
168 /// The keys of this map are always computed with `ExpnData.disambiguator`
170 expn_data_disambiguators: FxHashMap<Fingerprint, u32>,
174 crate fn new(edition: Edition) -> Self {
175 let mut root_data = ExpnData::default(
179 Some(DefId::local(CRATE_DEF_INDEX)),
181 root_data.orig_id = Some(0);
184 expn_data: vec![Some(root_data)],
185 syntax_context_data: vec![SyntaxContextData {
186 outer_expn: ExpnId::root(),
187 outer_transparency: Transparency::Opaque,
188 parent: SyntaxContext(0),
189 opaque: SyntaxContext(0),
190 opaque_and_semitransparent: SyntaxContext(0),
191 dollar_crate_name: kw::DollarCrate,
193 syntax_context_map: FxHashMap::default(),
194 expn_data_disambiguators: FxHashMap::default(),
198 pub fn with<T, F: FnOnce(&mut HygieneData) -> T>(f: F) -> T {
199 SESSION_GLOBALS.with(|session_globals| f(&mut *session_globals.hygiene_data.borrow_mut()))
202 fn fresh_expn(&mut self, mut expn_data: Option<ExpnData>) -> ExpnId {
203 let raw_id = self.expn_data.len() as u32;
204 if let Some(data) = expn_data.as_mut() {
205 data.orig_id.replace(raw_id).expect_none("orig_id should be None");
207 self.expn_data.push(expn_data);
211 fn expn_data(&self, expn_id: ExpnId) -> &ExpnData {
212 self.expn_data[expn_id.0 as usize].as_ref().expect("no expansion data for an expansion ID")
215 fn is_descendant_of(&self, mut expn_id: ExpnId, ancestor: ExpnId) -> bool {
216 while expn_id != ancestor {
217 if expn_id == ExpnId::root() {
220 expn_id = self.expn_data(expn_id).parent;
225 fn normalize_to_macros_2_0(&self, ctxt: SyntaxContext) -> SyntaxContext {
226 self.syntax_context_data[ctxt.0 as usize].opaque
229 fn normalize_to_macro_rules(&self, ctxt: SyntaxContext) -> SyntaxContext {
230 self.syntax_context_data[ctxt.0 as usize].opaque_and_semitransparent
233 fn outer_expn(&self, ctxt: SyntaxContext) -> ExpnId {
234 self.syntax_context_data[ctxt.0 as usize].outer_expn
237 fn outer_mark(&self, ctxt: SyntaxContext) -> (ExpnId, Transparency) {
238 let data = &self.syntax_context_data[ctxt.0 as usize];
239 (data.outer_expn, data.outer_transparency)
242 fn parent_ctxt(&self, ctxt: SyntaxContext) -> SyntaxContext {
243 self.syntax_context_data[ctxt.0 as usize].parent
246 fn remove_mark(&self, ctxt: &mut SyntaxContext) -> (ExpnId, Transparency) {
247 let outer_mark = self.outer_mark(*ctxt);
248 *ctxt = self.parent_ctxt(*ctxt);
252 fn marks(&self, mut ctxt: SyntaxContext) -> Vec<(ExpnId, Transparency)> {
253 let mut marks = Vec::new();
254 while ctxt != SyntaxContext::root() {
255 debug!("marks: getting parent of {:?}", ctxt);
256 marks.push(self.outer_mark(ctxt));
257 ctxt = self.parent_ctxt(ctxt);
263 fn walk_chain(&self, mut span: Span, to: SyntaxContext) -> Span {
264 debug!("walk_chain({:?}, {:?})", span, to);
265 debug!("walk_chain: span ctxt = {:?}", span.ctxt());
266 while span.from_expansion() && span.ctxt() != to {
267 let outer_expn = self.outer_expn(span.ctxt());
268 debug!("walk_chain({:?}): outer_expn={:?}", span, outer_expn);
269 let expn_data = self.expn_data(outer_expn);
270 debug!("walk_chain({:?}): expn_data={:?}", span, expn_data);
271 span = expn_data.call_site;
276 fn adjust(&self, ctxt: &mut SyntaxContext, expn_id: ExpnId) -> Option<ExpnId> {
277 let mut scope = None;
278 while !self.is_descendant_of(expn_id, self.outer_expn(*ctxt)) {
279 scope = Some(self.remove_mark(ctxt).0);
288 transparency: Transparency,
290 assert_ne!(expn_id, ExpnId::root());
291 if transparency == Transparency::Opaque {
292 return self.apply_mark_internal(ctxt, expn_id, transparency);
295 let call_site_ctxt = self.expn_data(expn_id).call_site.ctxt();
296 let mut call_site_ctxt = if transparency == Transparency::SemiTransparent {
297 self.normalize_to_macros_2_0(call_site_ctxt)
299 self.normalize_to_macro_rules(call_site_ctxt)
302 if call_site_ctxt == SyntaxContext::root() {
303 return self.apply_mark_internal(ctxt, expn_id, transparency);
306 // Otherwise, `expn_id` is a macros 1.0 definition and the call site is in a
307 // macros 2.0 expansion, i.e., a macros 1.0 invocation is in a macros 2.0 definition.
309 // In this case, the tokens from the macros 1.0 definition inherit the hygiene
310 // at their invocation. That is, we pretend that the macros 1.0 definition
311 // was defined at its invocation (i.e., inside the macros 2.0 definition)
312 // so that the macros 2.0 definition remains hygienic.
314 // See the example at `test/ui/hygiene/legacy_interaction.rs`.
315 for (expn_id, transparency) in self.marks(ctxt) {
316 call_site_ctxt = self.apply_mark_internal(call_site_ctxt, expn_id, transparency);
318 self.apply_mark_internal(call_site_ctxt, expn_id, transparency)
321 fn apply_mark_internal(
325 transparency: Transparency,
327 let syntax_context_data = &mut self.syntax_context_data;
328 let mut opaque = syntax_context_data[ctxt.0 as usize].opaque;
329 let mut opaque_and_semitransparent =
330 syntax_context_data[ctxt.0 as usize].opaque_and_semitransparent;
332 if transparency >= Transparency::Opaque {
336 .entry((parent, expn_id, transparency))
338 let new_opaque = SyntaxContext(syntax_context_data.len() as u32);
339 syntax_context_data.push(SyntaxContextData {
341 outer_transparency: transparency,
344 opaque_and_semitransparent: new_opaque,
345 dollar_crate_name: kw::DollarCrate,
351 if transparency >= Transparency::SemiTransparent {
352 let parent = opaque_and_semitransparent;
353 opaque_and_semitransparent = *self
355 .entry((parent, expn_id, transparency))
357 let new_opaque_and_semitransparent =
358 SyntaxContext(syntax_context_data.len() as u32);
359 syntax_context_data.push(SyntaxContextData {
361 outer_transparency: transparency,
364 opaque_and_semitransparent: new_opaque_and_semitransparent,
365 dollar_crate_name: kw::DollarCrate,
367 new_opaque_and_semitransparent
372 *self.syntax_context_map.entry((parent, expn_id, transparency)).or_insert_with(|| {
373 let new_opaque_and_semitransparent_and_transparent =
374 SyntaxContext(syntax_context_data.len() as u32);
375 syntax_context_data.push(SyntaxContextData {
377 outer_transparency: transparency,
380 opaque_and_semitransparent,
381 dollar_crate_name: kw::DollarCrate,
383 new_opaque_and_semitransparent_and_transparent
388 pub fn clear_syntax_context_map() {
389 HygieneData::with(|data| data.syntax_context_map = FxHashMap::default());
392 pub fn walk_chain(span: Span, to: SyntaxContext) -> Span {
393 HygieneData::with(|data| data.walk_chain(span, to))
396 pub fn update_dollar_crate_names(mut get_name: impl FnMut(SyntaxContext) -> Symbol) {
397 // The new contexts that need updating are at the end of the list and have `$crate` as a name.
398 let (len, to_update) = HygieneData::with(|data| {
400 data.syntax_context_data.len(),
401 data.syntax_context_data
404 .take_while(|scdata| scdata.dollar_crate_name == kw::DollarCrate)
408 // The callback must be called from outside of the `HygieneData` lock,
409 // since it will try to acquire it too.
410 let range_to_update = len - to_update..len;
412 range_to_update.clone().map(|idx| get_name(SyntaxContext::from_u32(idx as u32))).collect();
413 HygieneData::with(|data| {
414 range_to_update.zip(names.into_iter()).for_each(|(idx, name)| {
415 data.syntax_context_data[idx].dollar_crate_name = name;
420 pub fn debug_hygiene_data(verbose: bool) -> String {
421 HygieneData::with(|data| {
423 format!("{:#?}", data)
425 let mut s = String::from("");
426 s.push_str("Expansions:");
427 data.expn_data.iter().enumerate().for_each(|(id, expn_info)| {
428 let expn_info = expn_info.as_ref().expect("no expansion data for an expansion ID");
430 "\n{}: parent: {:?}, call_site_ctxt: {:?}, def_site_ctxt: {:?}, kind: {:?}",
433 expn_info.call_site.ctxt(),
434 expn_info.def_site.ctxt(),
438 s.push_str("\n\nSyntaxContexts:");
439 data.syntax_context_data.iter().enumerate().for_each(|(id, ctxt)| {
441 "\n#{}: parent: {:?}, outer_mark: ({:?}, {:?})",
442 id, ctxt.parent, ctxt.outer_expn, ctxt.outer_transparency,
452 pub const fn root() -> Self {
457 crate fn as_u32(self) -> u32 {
462 crate fn from_u32(raw: u32) -> SyntaxContext {
466 /// Extend a syntax context with a given expansion and transparency.
467 crate fn apply_mark(self, expn_id: ExpnId, transparency: Transparency) -> SyntaxContext {
468 HygieneData::with(|data| data.apply_mark(self, expn_id, transparency))
471 /// Pulls a single mark off of the syntax context. This effectively moves the
472 /// context up one macro definition level. That is, if we have a nested macro
473 /// definition as follows:
483 /// and we have a SyntaxContext that is referring to something declared by an invocation
484 /// of g (call it g1), calling remove_mark will result in the SyntaxContext for the
485 /// invocation of f that created g1.
486 /// Returns the mark that was removed.
487 pub fn remove_mark(&mut self) -> ExpnId {
488 HygieneData::with(|data| data.remove_mark(self).0)
491 pub fn marks(self) -> Vec<(ExpnId, Transparency)> {
492 HygieneData::with(|data| data.marks(self))
495 /// Adjust this context for resolution in a scope created by the given expansion.
496 /// For example, consider the following three resolutions of `f`:
499 /// mod foo { pub fn f() {} } // `f`'s `SyntaxContext` is empty.
501 /// macro m($f:ident) {
503 /// pub fn f() {} // `f`'s `SyntaxContext` has a single `ExpnId` from `m`.
504 /// pub fn $f() {} // `$f`'s `SyntaxContext` is empty.
506 /// foo::f(); // `f`'s `SyntaxContext` has a single `ExpnId` from `m`
507 /// //^ Since `mod foo` is outside this expansion, `adjust` removes the mark from `f`,
508 /// //| and it resolves to `::foo::f`.
509 /// bar::f(); // `f`'s `SyntaxContext` has a single `ExpnId` from `m`
510 /// //^ Since `mod bar` not outside this expansion, `adjust` does not change `f`,
511 /// //| and it resolves to `::bar::f`.
512 /// bar::$f(); // `f`'s `SyntaxContext` is empty.
513 /// //^ Since `mod bar` is not outside this expansion, `adjust` does not change `$f`,
514 /// //| and it resolves to `::bar::$f`.
517 /// This returns the expansion whose definition scope we use to privacy check the resolution,
518 /// or `None` if we privacy check as usual (i.e., not w.r.t. a macro definition scope).
519 pub fn adjust(&mut self, expn_id: ExpnId) -> Option<ExpnId> {
520 HygieneData::with(|data| data.adjust(self, expn_id))
523 /// Like `SyntaxContext::adjust`, but also normalizes `self` to macros 2.0.
524 pub fn normalize_to_macros_2_0_and_adjust(&mut self, expn_id: ExpnId) -> Option<ExpnId> {
525 HygieneData::with(|data| {
526 *self = data.normalize_to_macros_2_0(*self);
527 data.adjust(self, expn_id)
531 /// Adjust this context for resolution in a scope created by the given expansion
532 /// via a glob import with the given `SyntaxContext`.
537 /// macro m($i:ident) {
539 /// pub fn f() {} // `f`'s `SyntaxContext` has a single `ExpnId` from `m`.
540 /// pub fn $i() {} // `$i`'s `SyntaxContext` is empty.
543 /// macro n($j:ident) {
545 /// f(); // `f`'s `SyntaxContext` has a mark from `m` and a mark from `n`
546 /// //^ `glob_adjust` removes the mark from `n`, so this resolves to `foo::f`.
547 /// $i(); // `$i`'s `SyntaxContext` has a mark from `n`
548 /// //^ `glob_adjust` removes the mark from `n`, so this resolves to `foo::$i`.
549 /// $j(); // `$j`'s `SyntaxContext` has a mark from `m`
550 /// //^ This cannot be glob-adjusted, so this is a resolution error.
554 /// This returns `None` if the context cannot be glob-adjusted.
555 /// Otherwise, it returns the scope to use when privacy checking (see `adjust` for details).
556 pub fn glob_adjust(&mut self, expn_id: ExpnId, glob_span: Span) -> Option<Option<ExpnId>> {
557 HygieneData::with(|data| {
558 let mut scope = None;
559 let mut glob_ctxt = data.normalize_to_macros_2_0(glob_span.ctxt());
560 while !data.is_descendant_of(expn_id, data.outer_expn(glob_ctxt)) {
561 scope = Some(data.remove_mark(&mut glob_ctxt).0);
562 if data.remove_mark(self).0 != scope.unwrap() {
566 if data.adjust(self, expn_id).is_some() {
573 /// Undo `glob_adjust` if possible:
576 /// if let Some(privacy_checking_scope) = self.reverse_glob_adjust(expansion, glob_ctxt) {
577 /// assert!(self.glob_adjust(expansion, glob_ctxt) == Some(privacy_checking_scope));
580 pub fn reverse_glob_adjust(
584 ) -> Option<Option<ExpnId>> {
585 HygieneData::with(|data| {
586 if data.adjust(self, expn_id).is_some() {
590 let mut glob_ctxt = data.normalize_to_macros_2_0(glob_span.ctxt());
591 let mut marks = Vec::new();
592 while !data.is_descendant_of(expn_id, data.outer_expn(glob_ctxt)) {
593 marks.push(data.remove_mark(&mut glob_ctxt));
596 let scope = marks.last().map(|mark| mark.0);
597 while let Some((expn_id, transparency)) = marks.pop() {
598 *self = data.apply_mark(*self, expn_id, transparency);
604 pub fn hygienic_eq(self, other: SyntaxContext, expn_id: ExpnId) -> bool {
605 HygieneData::with(|data| {
606 let mut self_normalized = data.normalize_to_macros_2_0(self);
607 data.adjust(&mut self_normalized, expn_id);
608 self_normalized == data.normalize_to_macros_2_0(other)
613 pub fn normalize_to_macros_2_0(self) -> SyntaxContext {
614 HygieneData::with(|data| data.normalize_to_macros_2_0(self))
618 pub fn normalize_to_macro_rules(self) -> SyntaxContext {
619 HygieneData::with(|data| data.normalize_to_macro_rules(self))
623 pub fn outer_expn(self) -> ExpnId {
624 HygieneData::with(|data| data.outer_expn(self))
627 /// `ctxt.outer_expn_data()` is equivalent to but faster than
628 /// `ctxt.outer_expn().expn_data()`.
630 pub fn outer_expn_data(self) -> ExpnData {
631 HygieneData::with(|data| data.expn_data(data.outer_expn(self)).clone())
635 pub fn outer_mark(self) -> (ExpnId, Transparency) {
636 HygieneData::with(|data| data.outer_mark(self))
639 pub fn dollar_crate_name(self) -> Symbol {
640 HygieneData::with(|data| data.syntax_context_data[self.0 as usize].dollar_crate_name)
643 pub fn edition(self) -> Edition {
644 self.outer_expn_data().edition
648 impl fmt::Debug for SyntaxContext {
649 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
650 write!(f, "#{}", self.0)
655 /// Creates a fresh expansion with given properties.
656 /// Expansions are normally created by macros, but in some cases expansions are created for
657 /// other compiler-generated code to set per-span properties like allowed unstable features.
658 /// The returned span belongs to the created expansion and has the new properties,
659 /// but its location is inherited from the current span.
660 pub fn fresh_expansion(self, expn_data: ExpnData) -> Span {
661 self.fresh_expansion_with_transparency(expn_data, Transparency::Transparent)
664 pub fn fresh_expansion_with_transparency(
667 transparency: Transparency,
669 let expn_id = ExpnId::fresh(Some(expn_data));
670 HygieneData::with(|data| {
671 self.with_ctxt(data.apply_mark(SyntaxContext::root(), expn_id, transparency))
675 /// Reuses the span but adds information like the kind of the desugaring and features that are
676 /// allowed inside this span.
677 pub fn mark_with_reason(
679 allow_internal_unstable: Option<Lrc<[Symbol]>>,
680 reason: DesugaringKind,
683 self.fresh_expansion(ExpnData {
684 allow_internal_unstable,
685 ..ExpnData::default(ExpnKind::Desugaring(reason), self, edition, None)
690 /// A subset of properties from both macro definition and macro call available through global data.
691 /// Avoid using this if you have access to the original definition or call structures.
692 #[derive(Clone, Debug, Encodable, Decodable, HashStable_Generic)]
693 pub struct ExpnData {
694 // --- The part unique to each expansion.
695 /// The kind of this expansion - macro or compiler desugaring.
697 /// The expansion that produced this expansion.
699 /// The location of the actual macro invocation or syntax sugar , e.g.
700 /// `let x = foo!();` or `if let Some(y) = x {}`
702 /// This may recursively refer to other macro invocations, e.g., if
703 /// `foo!()` invoked `bar!()` internally, and there was an
704 /// expression inside `bar!`; the call_site of the expression in
705 /// the expansion would point to the `bar!` invocation; that
706 /// call_site span would have its own ExpnData, with the call_site
707 /// pointing to the `foo!` invocation.
710 // --- The part specific to the macro/desugaring definition.
711 // --- It may be reasonable to share this part between expansions with the same definition,
712 // --- but such sharing is known to bring some minor inconveniences without also bringing
713 // --- noticeable perf improvements (PR #62898).
714 /// The span of the macro definition (possibly dummy).
715 /// This span serves only informational purpose and is not used for resolution.
717 /// List of `#[unstable]`/feature-gated features that the macro is allowed to use
718 /// internally without forcing the whole crate to opt-in
720 pub allow_internal_unstable: Option<Lrc<[Symbol]>>,
721 /// Whether the macro is allowed to use `unsafe` internally
722 /// even if the user crate has `#![forbid(unsafe_code)]`.
723 pub allow_internal_unsafe: bool,
724 /// Enables the macro helper hack (`ident!(...)` -> `$crate::ident!(...)`)
725 /// for a given macro.
726 pub local_inner_macros: bool,
727 /// Edition of the crate in which the macro is defined.
728 pub edition: Edition,
729 /// The `DefId` of the macro being invoked,
730 /// if this `ExpnData` corresponds to a macro invocation
731 pub macro_def_id: Option<DefId>,
732 /// The crate that originally created this `ExpnData`. During
733 /// metadata serialization, we only encode `ExpnData`s that were
734 /// created locally - when our serialized metadata is decoded,
735 /// foreign `ExpnId`s will have their `ExpnData` looked up
736 /// from the crate specified by `Crate
738 /// The raw that this `ExpnData` had in its original crate.
739 /// An `ExpnData` can be created before being assigned an `ExpnId`,
740 /// so this might be `None` until `set_expn_data` is called
741 // This is used only for serialization/deserialization purposes:
742 // two `ExpnData`s that differ only in their `orig_id` should
743 // be considered equivalent.
744 #[stable_hasher(ignore)]
745 orig_id: Option<u32>,
747 /// Used to force two `ExpnData`s to have different `Fingerprint`s.
748 /// Due to macro expansion, it's possible to end up with two `ExpnId`s
749 /// that have identical `ExpnData`s. This violates the constract of `HashStable`
750 /// - the two `ExpnId`s are not equal, but their `Fingerprint`s are equal
751 /// (since the numerical `ExpnId` value is not considered by the `HashStable`
754 /// The `disambiguator` field is set by `update_disambiguator` when two distinct
755 /// `ExpnId`s would end up with the same `Fingerprint`. Since `ExpnData` includes
756 /// a `krate` field, this value only needs to be unique within a single crate.
760 // These would require special handling of `orig_id`.
761 impl !PartialEq for ExpnData {}
762 impl !Hash for ExpnData {}
770 allow_internal_unstable: Option<Lrc<[Symbol]>>,
771 allow_internal_unsafe: bool,
772 local_inner_macros: bool,
774 macro_def_id: Option<DefId>,
781 allow_internal_unstable,
782 allow_internal_unsafe,
792 /// Constructs expansion data with default properties.
797 macro_def_id: Option<DefId>,
801 parent: ExpnId::root(),
804 allow_internal_unstable: None,
805 allow_internal_unsafe: false,
806 local_inner_macros: false,
815 pub fn allow_unstable(
819 allow_internal_unstable: Lrc<[Symbol]>,
820 macro_def_id: Option<DefId>,
823 allow_internal_unstable: Some(allow_internal_unstable),
824 ..ExpnData::default(kind, call_site, edition, macro_def_id)
829 pub fn is_root(&self) -> bool {
830 matches!(self.kind, ExpnKind::Root)
835 #[derive(Clone, Debug, PartialEq, Encodable, Decodable, HashStable_Generic)]
837 /// No expansion, aka root expansion. Only `ExpnId::root()` has this kind.
839 /// Expansion produced by a macro.
840 Macro(MacroKind, Symbol),
841 /// Transform done by the compiler on the AST.
843 /// Desugaring done by the compiler during HIR lowering.
844 Desugaring(DesugaringKind),
850 pub fn descr(&self) -> String {
852 ExpnKind::Root => kw::PathRoot.to_string(),
853 ExpnKind::Macro(macro_kind, name) => match macro_kind {
854 MacroKind::Bang => format!("{}!", name),
855 MacroKind::Attr => format!("#[{}]", name),
856 MacroKind::Derive => format!("#[derive({})]", name),
858 ExpnKind::AstPass(kind) => kind.descr().to_string(),
859 ExpnKind::Desugaring(kind) => format!("desugaring of {}", kind.descr()),
860 ExpnKind::Inlined => "inlined source".to_string(),
865 /// The kind of macro invocation or definition.
866 #[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug)]
867 #[derive(HashStable_Generic)]
869 /// A bang macro `foo!()`.
871 /// An attribute macro `#[foo]`.
873 /// A derive macro `#[derive(Foo)]`
878 pub fn descr(self) -> &'static str {
880 MacroKind::Bang => "macro",
881 MacroKind::Attr => "attribute macro",
882 MacroKind::Derive => "derive macro",
886 pub fn descr_expected(self) -> &'static str {
888 MacroKind::Attr => "attribute",
893 pub fn article(self) -> &'static str {
895 MacroKind::Attr => "an",
901 /// The kind of AST transform.
902 #[derive(Clone, Copy, Debug, PartialEq, Encodable, Decodable, HashStable_Generic)]
910 fn descr(self) -> &'static str {
912 AstPass::StdImports => "standard library imports",
913 AstPass::TestHarness => "test harness",
914 AstPass::ProcMacroHarness => "proc macro harness",
919 /// The kind of compiler desugaring.
920 #[derive(Clone, Copy, PartialEq, Debug, Encodable, Decodable, HashStable_Generic)]
921 pub enum DesugaringKind {
922 /// We desugar `if c { i } else { e }` to `match $ExprKind::Use(c) { true => i, _ => e }`.
923 /// However, we do not want to blame `c` for unreachability but rather say that `i`
924 /// is unreachable. This desugaring kind allows us to avoid blaming `c`.
925 /// This also applies to `while` loops.
929 /// Desugaring of an `impl Trait` in return type position
930 /// to an `type Foo = impl Trait;` and replacing the
931 /// `impl Trait` with `Foo`.
938 /// A location in the desugaring of a `for` loop
939 #[derive(Clone, Copy, PartialEq, Debug, Encodable, Decodable, HashStable_Generic)]
940 pub enum ForLoopLoc {
945 impl DesugaringKind {
946 /// The description wording should combine well with "desugaring of {}".
947 fn descr(self) -> &'static str {
949 DesugaringKind::CondTemporary => "`if` or `while` condition",
950 DesugaringKind::Async => "`async` block or function",
951 DesugaringKind::Await => "`await` expression",
952 DesugaringKind::QuestionMark => "operator `?`",
953 DesugaringKind::TryBlock => "`try` block",
954 DesugaringKind::OpaqueTy => "`impl Trait`",
955 DesugaringKind::ForLoop(_) => "`for` loop",
961 pub struct HygieneEncodeContext {
962 /// All `SyntaxContexts` for which we have written `SyntaxContextData` into crate metadata.
963 /// This is `None` after we finish encoding `SyntaxContexts`, to ensure
964 /// that we don't accidentally try to encode any more `SyntaxContexts`
965 serialized_ctxts: Lock<FxHashSet<SyntaxContext>>,
966 /// The `SyntaxContexts` that we have serialized (e.g. as a result of encoding `Spans`)
967 /// in the most recent 'round' of serializnig. Serializing `SyntaxContextData`
968 /// may cause us to serialize more `SyntaxContext`s, so serialize in a loop
969 /// until we reach a fixed point.
970 latest_ctxts: Lock<FxHashSet<SyntaxContext>>,
972 serialized_expns: Lock<FxHashSet<ExpnId>>,
974 latest_expns: Lock<FxHashSet<ExpnId>>,
977 impl HygieneEncodeContext {
981 F: FnMut(&mut T, u32, &SyntaxContextData) -> Result<(), R>,
982 G: FnMut(&mut T, u32, &ExpnData) -> Result<(), R>,
989 // When we serialize a `SyntaxContextData`, we may end up serializing
990 // a `SyntaxContext` that we haven't seen before
991 while !self.latest_ctxts.lock().is_empty() || !self.latest_expns.lock().is_empty() {
993 "encode_hygiene: Serializing a round of {:?} SyntaxContextDatas: {:?}",
994 self.latest_ctxts.lock().len(),
998 // Consume the current round of SyntaxContexts.
999 // Drop the lock() temporary early
1000 let latest_ctxts = { std::mem::take(&mut *self.latest_ctxts.lock()) };
1002 // It's fine to iterate over a HashMap, because the serialization
1003 // of the table that we insert data into doesn't depend on insertion
1005 for_all_ctxts_in(latest_ctxts.into_iter(), |(index, ctxt, data)| {
1006 if self.serialized_ctxts.lock().insert(ctxt) {
1007 encode_ctxt(encoder, index, data)?;
1012 let latest_expns = { std::mem::take(&mut *self.latest_expns.lock()) };
1014 for_all_expns_in(latest_expns.into_iter(), |index, expn, data| {
1015 if self.serialized_expns.lock().insert(expn) {
1016 encode_expn(encoder, index, data)?;
1021 debug!("encode_hygiene: Done serializing SyntaxContextData");
1027 /// Additional information used to assist in decoding hygiene data
1028 pub struct HygieneDecodeContext {
1029 // Maps serialized `SyntaxContext` ids to a `SyntaxContext` in the current
1030 // global `HygieneData`. When we deserialize a `SyntaxContext`, we need to create
1031 // a new id in the global `HygieneData`. This map tracks the ID we end up picking,
1032 // so that multiple occurrences of the same serialized id are decoded to the same
1034 remapped_ctxts: Lock<Vec<Option<SyntaxContext>>>,
1035 // The same as `remapepd_ctxts`, but for `ExpnId`s
1036 remapped_expns: Lock<Vec<Option<ExpnId>>>,
1039 pub fn decode_expn_id<
1042 F: FnOnce(&mut D, u32) -> Result<ExpnData, D::Error>,
1043 G: FnOnce(CrateNum) -> &'a HygieneDecodeContext,
1046 mode: ExpnDataDecodeMode<'a, G>,
1048 ) -> Result<ExpnId, D::Error> {
1049 let index = u32::decode(d)?;
1050 let context = match mode {
1051 ExpnDataDecodeMode::IncrComp(context) => context,
1052 ExpnDataDecodeMode::Metadata(get_context) => {
1053 let krate = CrateNum::decode(d)?;
1058 // Do this after decoding, so that we decode a `CrateNum`
1060 if index == ExpnId::root().as_u32() {
1061 debug!("decode_expn_id: deserialized root");
1062 return Ok(ExpnId::root());
1065 let outer_expns = &context.remapped_expns;
1067 // Ensure that the lock() temporary is dropped early
1069 if let Some(expn_id) = outer_expns.lock().get(index as usize).copied().flatten() {
1074 // Don't decode the data inside `HygieneData::with`, since we need to recursively decode
1076 let mut expn_data = decode_data(d, index)?;
1078 let expn_id = HygieneData::with(|hygiene_data| {
1079 let expn_id = ExpnId(hygiene_data.expn_data.len() as u32);
1081 // If we just deserialized an `ExpnData` owned by
1082 // the local crate, its `orig_id` will be stale,
1083 // so we need to update it to its own value.
1084 // This only happens when we deserialize the incremental cache,
1085 // since a crate will never decode its own metadata.
1086 if expn_data.krate == LOCAL_CRATE {
1087 expn_data.orig_id = Some(expn_id.0);
1090 hygiene_data.expn_data.push(Some(expn_data));
1092 let mut expns = outer_expns.lock();
1093 let new_len = index as usize + 1;
1094 if expns.len() < new_len {
1095 expns.resize(new_len, None);
1097 expns[index as usize] = Some(expn_id);
1104 // Decodes `SyntaxContext`, using the provided `HygieneDecodeContext`
1105 // to track which `SyntaxContext`s we have already decoded.
1106 // The provided closure will be invoked to deserialize a `SyntaxContextData`
1107 // if we haven't already seen the id of the `SyntaxContext` we are deserializing.
1108 pub fn decode_syntax_context<
1110 F: FnOnce(&mut D, u32) -> Result<SyntaxContextData, D::Error>,
1113 context: &HygieneDecodeContext,
1115 ) -> Result<SyntaxContext, D::Error> {
1116 let raw_id: u32 = Decodable::decode(d)?;
1118 debug!("decode_syntax_context: deserialized root");
1119 // The root is special
1120 return Ok(SyntaxContext::root());
1123 let outer_ctxts = &context.remapped_ctxts;
1125 // Ensure that the lock() temporary is dropped early
1127 if let Some(ctxt) = outer_ctxts.lock().get(raw_id as usize).copied().flatten() {
1132 // Allocate and store SyntaxContext id *before* calling the decoder function,
1133 // as the SyntaxContextData may reference itself.
1134 let new_ctxt = HygieneData::with(|hygiene_data| {
1135 let new_ctxt = SyntaxContext(hygiene_data.syntax_context_data.len() as u32);
1136 // Push a dummy SyntaxContextData to ensure that nobody else can get the
1137 // same ID as us. This will be overwritten after call `decode_Data`
1138 hygiene_data.syntax_context_data.push(SyntaxContextData {
1139 outer_expn: ExpnId::root(),
1140 outer_transparency: Transparency::Transparent,
1141 parent: SyntaxContext::root(),
1142 opaque: SyntaxContext::root(),
1143 opaque_and_semitransparent: SyntaxContext::root(),
1144 dollar_crate_name: kw::Empty,
1146 let mut ctxts = outer_ctxts.lock();
1147 let new_len = raw_id as usize + 1;
1148 if ctxts.len() < new_len {
1149 ctxts.resize(new_len, None);
1151 ctxts[raw_id as usize] = Some(new_ctxt);
1156 // Don't try to decode data while holding the lock, since we need to
1157 // be able to recursively decode a SyntaxContext
1158 let mut ctxt_data = decode_data(d, raw_id)?;
1159 // Reset `dollar_crate_name` so that it will be updated by `update_dollar_crate_names`
1160 // We don't care what the encoding crate set this to - we want to resolve it
1161 // from the perspective of the current compilation session
1162 ctxt_data.dollar_crate_name = kw::DollarCrate;
1164 // Overwrite the dummy data with our decoded SyntaxContextData
1165 HygieneData::with(|hygiene_data| {
1166 let dummy = std::mem::replace(
1167 &mut hygiene_data.syntax_context_data[new_ctxt.as_u32() as usize],
1170 // Make sure nothing weird happening while `decode_data` was running
1171 assert_eq!(dummy.dollar_crate_name, kw::Empty);
1177 pub fn num_syntax_ctxts() -> usize {
1178 HygieneData::with(|data| data.syntax_context_data.len())
1181 pub fn for_all_ctxts_in<E, F: FnMut((u32, SyntaxContext, &SyntaxContextData)) -> Result<(), E>>(
1182 ctxts: impl Iterator<Item = SyntaxContext>,
1184 ) -> Result<(), E> {
1185 let all_data: Vec<_> = HygieneData::with(|data| {
1186 ctxts.map(|ctxt| (ctxt, data.syntax_context_data[ctxt.0 as usize].clone())).collect()
1188 for (ctxt, data) in all_data.into_iter() {
1189 f((ctxt.0, ctxt, &data))?;
1194 pub fn for_all_expns_in<E, F: FnMut(u32, ExpnId, &ExpnData) -> Result<(), E>>(
1195 expns: impl Iterator<Item = ExpnId>,
1197 ) -> Result<(), E> {
1198 let all_data: Vec<_> = HygieneData::with(|data| {
1199 expns.map(|expn| (expn, data.expn_data[expn.0 as usize].clone())).collect()
1201 for (expn, data) in all_data.into_iter() {
1202 f(expn.0, expn, &data.unwrap_or_else(|| panic!("Missing data for {:?}", expn)))?;
1207 pub fn for_all_data<E, F: FnMut((u32, SyntaxContext, &SyntaxContextData)) -> Result<(), E>>(
1209 ) -> Result<(), E> {
1210 let all_data = HygieneData::with(|data| data.syntax_context_data.clone());
1211 for (i, data) in all_data.into_iter().enumerate() {
1212 f((i as u32, SyntaxContext(i as u32), &data))?;
1217 impl<E: Encoder> Encodable<E> for ExpnId {
1218 default fn encode(&self, _: &mut E) -> Result<(), E::Error> {
1219 panic!("cannot encode `ExpnId` with `{}`", std::any::type_name::<E>());
1223 impl<D: Decoder> Decodable<D> for ExpnId {
1224 default fn decode(_: &mut D) -> Result<Self, D::Error> {
1225 panic!("cannot decode `ExpnId` with `{}`", std::any::type_name::<D>());
1229 pub fn for_all_expn_data<E, F: FnMut(u32, &ExpnData) -> Result<(), E>>(mut f: F) -> Result<(), E> {
1230 let all_data = HygieneData::with(|data| data.expn_data.clone());
1231 for (i, data) in all_data.into_iter().enumerate() {
1232 f(i as u32, &data.unwrap_or_else(|| panic!("Missing ExpnData!")))?;
1237 pub fn raw_encode_syntax_context<E: Encoder>(
1238 ctxt: SyntaxContext,
1239 context: &HygieneEncodeContext,
1241 ) -> Result<(), E::Error> {
1242 if !context.serialized_ctxts.lock().contains(&ctxt) {
1243 context.latest_ctxts.lock().insert(ctxt);
1248 pub fn raw_encode_expn_id<E: Encoder>(
1250 context: &HygieneEncodeContext,
1251 mode: ExpnDataEncodeMode,
1253 ) -> Result<(), E::Error> {
1254 // Record the fact that we need to serialize the corresponding
1256 let needs_data = || {
1257 if !context.serialized_expns.lock().contains(&expn) {
1258 context.latest_expns.lock().insert(expn);
1263 ExpnDataEncodeMode::IncrComp => {
1264 // Always serialize the `ExpnData` in incr comp mode
1268 ExpnDataEncodeMode::Metadata => {
1269 let data = expn.expn_data();
1270 // We only need to serialize the ExpnData
1271 // if it comes from this crate.
1272 // We currently don't serialize any hygiene information data for
1273 // proc-macro crates: see the `SpecializedEncoder<Span>` impl
1274 // for crate metadata.
1275 if data.krate == LOCAL_CRATE {
1278 data.orig_id.expect("Missing orig_id").encode(e)?;
1279 data.krate.encode(e)
1284 pub enum ExpnDataEncodeMode {
1289 pub enum ExpnDataDecodeMode<'a, F: FnOnce(CrateNum) -> &'a HygieneDecodeContext> {
1290 IncrComp(&'a HygieneDecodeContext),
1294 impl<'a> ExpnDataDecodeMode<'a, Box<dyn FnOnce(CrateNum) -> &'a HygieneDecodeContext>> {
1295 pub fn incr_comp(ctxt: &'a HygieneDecodeContext) -> Self {
1296 ExpnDataDecodeMode::IncrComp(ctxt)
1300 impl<E: Encoder> Encodable<E> for SyntaxContext {
1301 default fn encode(&self, _: &mut E) -> Result<(), E::Error> {
1302 panic!("cannot encode `SyntaxContext` with `{}`", std::any::type_name::<E>());
1306 impl<D: Decoder> Decodable<D> for SyntaxContext {
1307 default fn decode(_: &mut D) -> Result<Self, D::Error> {
1308 panic!("cannot decode `SyntaxContext` with `{}`", std::any::type_name::<D>());
1312 /// Updates the `disambiguator` field of the corresponding `ExpnData`
1313 /// such that the `Fingerprint` of the `ExpnData` does not collide with
1314 /// any other `ExpnIds`.
1316 /// This method is called only when an `ExpnData` is first associated
1317 /// with an `ExpnId` (when the `ExpnId` is initially constructed, or via
1318 /// `set_expn_data`). It is *not* called for foreign `ExpnId`s deserialized
1319 /// from another crate's metadata - since `ExpnData` includes a `krate` field,
1320 /// collisions are only possible between `ExpnId`s within the same crate.
1321 fn update_disambiguator(expn_id: ExpnId) {
1322 /// A `HashStableContext` which hashes the raw id values for `DefId`
1323 /// and `CrateNum`, rather than using their computed stable hash.
1325 /// This allows us to use the `HashStable` implementation on `ExpnId`
1326 /// early on in compilation, before we've constructed a `TyCtxt`.
1327 /// The `Fingerprint`s created by this context are not 'stable', since
1328 /// the raw `CrateNum` and `DefId` values for an item may change between
1329 /// sessions due to unrelated changes (e.g. adding/removing an different item).
1331 /// However, this is fine for our purposes - we only need to detect
1332 /// when two `ExpnData`s have the same `Fingerprint`. Since the hashes produced
1333 /// by this context still obey the properties of `HashStable`, we have
1335 /// `hash_stable(expn1, DummyHashStableContext) == hash_stable(expn2, DummyHashStableContext)`
1336 /// iff `hash_stable(expn1, StableHashingContext) == hash_stable(expn2, StableHasingContext)`.
1338 /// This is sufficient for determining when we need to update the disambiguator.
1339 struct DummyHashStableContext<'a> {
1340 caching_source_map: CachingSourceMapView<'a>,
1343 impl<'a> crate::HashStableContext for DummyHashStableContext<'a> {
1344 fn hash_def_id(&mut self, def_id: DefId, hasher: &mut StableHasher) {
1345 def_id.krate.as_u32().hash_stable(self, hasher);
1346 def_id.index.as_u32().hash_stable(self, hasher);
1349 fn expn_id_cache() -> &'static LocalKey<ExpnIdCache> {
1350 // This cache is only used by `DummyHashStableContext`,
1351 // so we won't pollute the cache values of the normal `StableHashingContext`
1353 static CACHE: ExpnIdCache = Default::default();
1359 fn hash_crate_num(&mut self, krate: CrateNum, hasher: &mut StableHasher) {
1360 krate.as_u32().hash_stable(self, hasher);
1362 fn hash_spans(&self) -> bool {
1365 fn byte_pos_to_line_and_col(
1368 ) -> Option<(Lrc<SourceFile>, usize, BytePos)> {
1369 self.caching_source_map.byte_pos_to_line_and_col(byte)
1371 fn span_data_to_lines_and_cols(
1373 span: &crate::SpanData,
1374 ) -> Option<(Lrc<SourceFile>, usize, BytePos, usize, BytePos)> {
1375 self.caching_source_map.span_data_to_lines_and_cols(span)
1379 let source_map = SESSION_GLOBALS
1380 .with(|session_globals| session_globals.source_map.borrow().as_ref().unwrap().clone());
1383 DummyHashStableContext { caching_source_map: CachingSourceMapView::new(&source_map) };
1385 let mut hasher = StableHasher::new();
1387 let expn_data = expn_id.expn_data();
1388 // This disambiguator should not have been set yet.
1390 expn_data.disambiguator, 0,
1391 "Already set disambiguator for ExpnData: {:?}",
1394 expn_data.hash_stable(&mut ctx, &mut hasher);
1395 let first_hash = hasher.finish();
1397 let modified = HygieneData::with(|data| {
1398 // If this is the first ExpnData with a given hash, then keep our
1399 // disambiguator at 0 (the default u32 value)
1400 let disambig = data.expn_data_disambiguators.entry(first_hash).or_default();
1401 data.expn_data[expn_id.0 as usize].as_mut().unwrap().disambiguator = *disambig;
1408 info!("Set disambiguator for {:?} (hash {:?})", expn_id, first_hash);
1409 info!("expn_data = {:?}", expn_id.expn_data());
1411 // Verify that the new disambiguator makes the hash unique
1412 #[cfg(debug_assertions)]
1414 hasher = StableHasher::new();
1415 expn_id.expn_data().hash_stable(&mut ctx, &mut hasher);
1416 let new_hash: Fingerprint = hasher.finish();
1418 HygieneData::with(|data| {
1419 data.expn_data_disambiguators
1421 .expect_none("Hash collision after disambiguator update!");