1 //! Machinery for hygienic macros, inspired by the `MTWT[1]` paper.
3 //! `[1]` Matthew Flatt, Ryan Culpepper, David Darais, and Robert Bruce Findler. 2012.
4 //! *Macros that work together: Compile-time bindings, partial expansion,
5 //! and definition contexts*. J. Funct. Program. 22, 2 (March 2012), 181-216.
6 //! DOI=10.1017/S0956796812000093 <https://doi.org/10.1017/S0956796812000093>
8 // Hygiene data is stored in a global variable and accessed via TLS, which
9 // means that accesses are somewhat expensive. (`HygieneData::with`
10 // encapsulates a single access.) Therefore, on hot code paths it is worth
11 // ensuring that multiple HygieneData accesses are combined into a single
12 // `HygieneData::with`.
14 // This explains why `HygieneData`, `SyntaxContext` and `ExpnId` have interfaces
15 // with a certain amount of redundancy in them. For example,
16 // `SyntaxContext::outer_expn_info` combines `SyntaxContext::outer` and
17 // `ExpnId::expn_info` so that two `HygieneData` accesses can be performed within
18 // a single `HygieneData::with` call.
20 // It also explains why many functions appear in `HygieneData` and again in
21 // `SyntaxContext` or `ExpnId`. For example, `HygieneData::outer` and
22 // `SyntaxContext::outer` do the same thing, but the former is for use within a
23 // `HygieneData::with` call while the latter is for use outside such a call.
24 // When modifying this file it is important to understand this distinction,
25 // because getting it wrong can lead to nested `HygieneData::with` calls that
26 // trigger runtime aborts. (Fortunately these are obvious and easy to fix.)
29 use crate::{Span, DUMMY_SP};
30 use crate::edition::Edition;
31 use crate::symbol::{kw, Symbol};
33 use rustc_serialize::{Encodable, Decodable, Encoder, Decoder};
34 use rustc_data_structures::fx::FxHashMap;
35 use rustc_data_structures::sync::Lrc;
38 /// A `SyntaxContext` represents a chain of pairs `(ExpnId, Transparency)` named "marks".
39 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
40 pub struct SyntaxContext(u32);
43 struct SyntaxContextData {
45 outer_transparency: Transparency,
46 parent: SyntaxContext,
47 /// This context, but with all transparent and semi-transparent expansions filtered away.
48 opaque: SyntaxContext,
49 /// This context, but with all transparent expansions filtered away.
50 opaque_and_semitransparent: SyntaxContext,
51 /// Name of the crate to which `$crate` with this context would resolve.
52 dollar_crate_name: Symbol,
55 /// A unique ID associated with a macro invocation and expansion.
56 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
57 pub struct ExpnId(u32);
59 // FIXME: Find a way to merge this with `ExpnInfo`.
61 struct InternalExpnData {
63 /// Each expansion should have an associated expansion info, but sometimes there's a delay
64 /// between creation of an expansion ID and obtaining its info (e.g. macros are collected
65 /// first and then resolved later), so we use an `Option` here.
66 expn_info: Option<ExpnInfo>,
69 /// A property of a macro expansion that determines how identifiers
70 /// produced by that expansion are resolved.
71 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Hash, Debug, RustcEncodable, RustcDecodable)]
72 pub enum Transparency {
73 /// Identifier produced by a transparent expansion is always resolved at call-site.
74 /// Call-site spans in procedural macros, hygiene opt-out in `macro` should use this.
76 /// Identifier produced by a semi-transparent expansion may be resolved
77 /// either at call-site or at definition-site.
78 /// If it's a local variable, label or `$crate` then it's resolved at def-site.
79 /// Otherwise it's resolved at call-site.
80 /// `macro_rules` macros behave like this, built-in macros currently behave like this too,
81 /// but that's an implementation detail.
83 /// Identifier produced by an opaque expansion is always resolved at definition-site.
84 /// Def-site spans in procedural macros, identifiers from `macro` by default use this.
89 pub fn fresh(parent: ExpnId, expn_info: Option<ExpnInfo>) -> Self {
90 HygieneData::with(|data| data.fresh_expn(parent, expn_info))
93 /// The ID of the theoretical expansion that generates freshly parsed, unexpanded AST.
95 pub fn root() -> Self {
100 pub fn as_u32(self) -> u32 {
105 pub fn from_u32(raw: u32) -> ExpnId {
110 pub fn parent(self) -> ExpnId {
111 HygieneData::with(|data| data.parent_expn(self))
115 pub fn expn_info(self) -> Option<ExpnInfo> {
116 HygieneData::with(|data| data.expn_info(self).cloned())
120 pub fn set_expn_info(self, info: ExpnInfo) {
121 HygieneData::with(|data| {
122 let old_info = &mut data.expn_data[self.0 as usize].expn_info;
123 assert!(old_info.is_none(), "expansion info is reset for an expansion ID");
124 *old_info = Some(info);
128 pub fn is_descendant_of(self, ancestor: ExpnId) -> bool {
129 HygieneData::with(|data| data.is_descendant_of(self, ancestor))
132 /// `expn_id.outer_expn_is_descendant_of(ctxt)` is equivalent to but faster than
133 /// `expn_id.is_descendant_of(ctxt.outer_expn())`.
134 pub fn outer_expn_is_descendant_of(self, ctxt: SyntaxContext) -> bool {
135 HygieneData::with(|data| data.is_descendant_of(self, data.outer_expn(ctxt)))
138 // Used for enabling some compatibility fallback in resolve.
140 pub fn looks_like_proc_macro_derive(self) -> bool {
141 HygieneData::with(|data| {
142 if data.default_transparency(self) == Transparency::Opaque {
143 if let Some(expn_info) = data.expn_info(self) {
144 if let ExpnKind::Macro(MacroKind::Derive, _) = expn_info.kind {
155 crate struct HygieneData {
156 expn_data: Vec<InternalExpnData>,
157 syntax_context_data: Vec<SyntaxContextData>,
158 syntax_context_map: FxHashMap<(SyntaxContext, ExpnId, Transparency), SyntaxContext>,
162 crate fn new(edition: Edition) -> Self {
164 expn_data: vec![InternalExpnData {
165 parent: ExpnId::root(),
166 expn_info: Some(ExpnInfo::default(ExpnKind::Root, DUMMY_SP, edition)),
168 syntax_context_data: vec![SyntaxContextData {
169 outer_expn: ExpnId::root(),
170 outer_transparency: Transparency::Opaque,
171 parent: SyntaxContext(0),
172 opaque: SyntaxContext(0),
173 opaque_and_semitransparent: SyntaxContext(0),
174 dollar_crate_name: kw::DollarCrate,
176 syntax_context_map: FxHashMap::default(),
180 fn with<T, F: FnOnce(&mut HygieneData) -> T>(f: F) -> T {
181 GLOBALS.with(|globals| f(&mut *globals.hygiene_data.borrow_mut()))
184 fn fresh_expn(&mut self, parent: ExpnId, expn_info: Option<ExpnInfo>) -> ExpnId {
185 self.expn_data.push(InternalExpnData { parent, expn_info });
186 ExpnId(self.expn_data.len() as u32 - 1)
189 fn parent_expn(&self, expn_id: ExpnId) -> ExpnId {
190 self.expn_data[expn_id.0 as usize].parent
193 fn expn_info(&self, expn_id: ExpnId) -> Option<&ExpnInfo> {
194 if expn_id != ExpnId::root() {
195 Some(self.expn_data[expn_id.0 as usize].expn_info.as_ref()
196 .expect("no expansion info for an expansion ID"))
198 // FIXME: Some code relies on `expn_info().is_none()` meaning "no expansion".
199 // Introduce a method for checking for "no expansion" instead and always return
200 // `ExpnInfo` from this function instead of the `Option`.
205 fn is_descendant_of(&self, mut expn_id: ExpnId, ancestor: ExpnId) -> bool {
206 while expn_id != ancestor {
207 if expn_id == ExpnId::root() {
210 expn_id = self.parent_expn(expn_id);
215 fn default_transparency(&self, expn_id: ExpnId) -> Transparency {
216 self.expn_info(expn_id).map_or(
217 Transparency::SemiTransparent, |einfo| einfo.default_transparency
221 fn modern(&self, ctxt: SyntaxContext) -> SyntaxContext {
222 self.syntax_context_data[ctxt.0 as usize].opaque
225 fn modern_and_legacy(&self, ctxt: SyntaxContext) -> SyntaxContext {
226 self.syntax_context_data[ctxt.0 as usize].opaque_and_semitransparent
229 fn outer_expn(&self, ctxt: SyntaxContext) -> ExpnId {
230 self.syntax_context_data[ctxt.0 as usize].outer_expn
233 fn outer_transparency(&self, ctxt: SyntaxContext) -> Transparency {
234 self.syntax_context_data[ctxt.0 as usize].outer_transparency
237 fn parent_ctxt(&self, ctxt: SyntaxContext) -> SyntaxContext {
238 self.syntax_context_data[ctxt.0 as usize].parent
241 fn remove_mark(&self, ctxt: &mut SyntaxContext) -> ExpnId {
242 let outer_expn = self.outer_expn(*ctxt);
243 *ctxt = self.parent_ctxt(*ctxt);
247 fn marks(&self, mut ctxt: SyntaxContext) -> Vec<(ExpnId, Transparency)> {
248 let mut marks = Vec::new();
249 while ctxt != SyntaxContext::empty() {
250 marks.push((self.outer_expn(ctxt), self.outer_transparency(ctxt)));
251 ctxt = self.parent_ctxt(ctxt);
257 fn walk_chain(&self, mut span: Span, to: SyntaxContext) -> Span {
258 while span.ctxt() != crate::NO_EXPANSION && span.ctxt() != to {
259 if let Some(info) = self.expn_info(self.outer_expn(span.ctxt())) {
260 span = info.call_site;
268 fn adjust(&self, ctxt: &mut SyntaxContext, expn_id: ExpnId) -> Option<ExpnId> {
269 let mut scope = None;
270 while !self.is_descendant_of(expn_id, self.outer_expn(*ctxt)) {
271 scope = Some(self.remove_mark(ctxt));
276 fn apply_mark(&mut self, ctxt: SyntaxContext, expn_id: ExpnId) -> SyntaxContext {
277 assert_ne!(expn_id, ExpnId::root());
278 self.apply_mark_with_transparency(ctxt, expn_id, self.default_transparency(expn_id))
281 fn apply_mark_with_transparency(&mut self, ctxt: SyntaxContext, expn_id: ExpnId,
282 transparency: Transparency) -> SyntaxContext {
283 assert_ne!(expn_id, ExpnId::root());
284 if transparency == Transparency::Opaque {
285 return self.apply_mark_internal(ctxt, expn_id, transparency);
289 self.expn_info(expn_id).map_or(SyntaxContext::empty(), |info| info.call_site.ctxt());
290 let mut call_site_ctxt = if transparency == Transparency::SemiTransparent {
291 self.modern(call_site_ctxt)
293 self.modern_and_legacy(call_site_ctxt)
296 if call_site_ctxt == SyntaxContext::empty() {
297 return self.apply_mark_internal(ctxt, expn_id, transparency);
300 // Otherwise, `expn_id` is a macros 1.0 definition and the call site is in a
301 // macros 2.0 expansion, i.e., a macros 1.0 invocation is in a macros 2.0 definition.
303 // In this case, the tokens from the macros 1.0 definition inherit the hygiene
304 // at their invocation. That is, we pretend that the macros 1.0 definition
305 // was defined at its invocation (i.e., inside the macros 2.0 definition)
306 // so that the macros 2.0 definition remains hygienic.
308 // See the example at `test/ui/hygiene/legacy_interaction.rs`.
309 for (expn_id, transparency) in self.marks(ctxt) {
310 call_site_ctxt = self.apply_mark_internal(call_site_ctxt, expn_id, transparency);
312 self.apply_mark_internal(call_site_ctxt, expn_id, transparency)
315 fn apply_mark_internal(
316 &mut self, ctxt: SyntaxContext, expn_id: ExpnId, transparency: Transparency
318 let syntax_context_data = &mut self.syntax_context_data;
319 let mut opaque = syntax_context_data[ctxt.0 as usize].opaque;
320 let mut opaque_and_semitransparent =
321 syntax_context_data[ctxt.0 as usize].opaque_and_semitransparent;
323 if transparency >= Transparency::Opaque {
325 opaque = *self.syntax_context_map.entry((parent, expn_id, transparency))
327 let new_opaque = SyntaxContext(syntax_context_data.len() as u32);
328 syntax_context_data.push(SyntaxContextData {
330 outer_transparency: transparency,
333 opaque_and_semitransparent: new_opaque,
334 dollar_crate_name: kw::DollarCrate,
340 if transparency >= Transparency::SemiTransparent {
341 let parent = opaque_and_semitransparent;
342 opaque_and_semitransparent =
343 *self.syntax_context_map.entry((parent, expn_id, transparency))
345 let new_opaque_and_semitransparent =
346 SyntaxContext(syntax_context_data.len() as u32);
347 syntax_context_data.push(SyntaxContextData {
349 outer_transparency: transparency,
352 opaque_and_semitransparent: new_opaque_and_semitransparent,
353 dollar_crate_name: kw::DollarCrate,
355 new_opaque_and_semitransparent
360 *self.syntax_context_map.entry((parent, expn_id, transparency)).or_insert_with(|| {
361 let new_opaque_and_semitransparent_and_transparent =
362 SyntaxContext(syntax_context_data.len() as u32);
363 syntax_context_data.push(SyntaxContextData {
365 outer_transparency: transparency,
368 opaque_and_semitransparent,
369 dollar_crate_name: kw::DollarCrate,
371 new_opaque_and_semitransparent_and_transparent
376 pub fn clear_syntax_context_map() {
377 HygieneData::with(|data| data.syntax_context_map = FxHashMap::default());
380 pub fn walk_chain(span: Span, to: SyntaxContext) -> Span {
381 HygieneData::with(|data| data.walk_chain(span, to))
384 pub fn update_dollar_crate_names(mut get_name: impl FnMut(SyntaxContext) -> Symbol) {
385 // The new contexts that need updating are at the end of the list and have `$crate` as a name.
386 let (len, to_update) = HygieneData::with(|data| (
387 data.syntax_context_data.len(),
388 data.syntax_context_data.iter().rev()
389 .take_while(|scdata| scdata.dollar_crate_name == kw::DollarCrate).count()
391 // The callback must be called from outside of the `HygieneData` lock,
392 // since it will try to acquire it too.
393 let range_to_update = len - to_update .. len;
395 range_to_update.clone().map(|idx| get_name(SyntaxContext::from_u32(idx as u32))).collect();
396 HygieneData::with(|data| range_to_update.zip(names.into_iter()).for_each(|(idx, name)| {
397 data.syntax_context_data[idx].dollar_crate_name = name;
403 pub const fn empty() -> Self {
408 crate fn as_u32(self) -> u32 {
413 crate fn from_u32(raw: u32) -> SyntaxContext {
417 /// Extend a syntax context with a given expansion and default transparency for that expansion.
418 pub fn apply_mark(self, expn_id: ExpnId) -> SyntaxContext {
419 HygieneData::with(|data| data.apply_mark(self, expn_id))
422 /// Extend a syntax context with a given expansion and transparency.
423 pub fn apply_mark_with_transparency(self, expn_id: ExpnId, transparency: Transparency)
425 HygieneData::with(|data| data.apply_mark_with_transparency(self, expn_id, transparency))
428 /// Pulls a single mark off of the syntax context. This effectively moves the
429 /// context up one macro definition level. That is, if we have a nested macro
430 /// definition as follows:
440 /// and we have a SyntaxContext that is referring to something declared by an invocation
441 /// of g (call it g1), calling remove_mark will result in the SyntaxContext for the
442 /// invocation of f that created g1.
443 /// Returns the mark that was removed.
444 pub fn remove_mark(&mut self) -> ExpnId {
445 HygieneData::with(|data| data.remove_mark(self))
448 pub fn marks(self) -> Vec<(ExpnId, Transparency)> {
449 HygieneData::with(|data| data.marks(self))
452 /// Adjust this context for resolution in a scope created by the given expansion.
453 /// For example, consider the following three resolutions of `f`:
456 /// mod foo { pub fn f() {} } // `f`'s `SyntaxContext` is empty.
458 /// macro m($f:ident) {
460 /// pub fn f() {} // `f`'s `SyntaxContext` has a single `ExpnId` from `m`.
461 /// pub fn $f() {} // `$f`'s `SyntaxContext` is empty.
463 /// foo::f(); // `f`'s `SyntaxContext` has a single `ExpnId` from `m`
464 /// //^ Since `mod foo` is outside this expansion, `adjust` removes the mark from `f`,
465 /// //| and it resolves to `::foo::f`.
466 /// bar::f(); // `f`'s `SyntaxContext` has a single `ExpnId` from `m`
467 /// //^ Since `mod bar` not outside this expansion, `adjust` does not change `f`,
468 /// //| and it resolves to `::bar::f`.
469 /// bar::$f(); // `f`'s `SyntaxContext` is empty.
470 /// //^ Since `mod bar` is not outside this expansion, `adjust` does not change `$f`,
471 /// //| and it resolves to `::bar::$f`.
474 /// This returns the expansion whose definition scope we use to privacy check the resolution,
475 /// or `None` if we privacy check as usual (i.e., not w.r.t. a macro definition scope).
476 pub fn adjust(&mut self, expn_id: ExpnId) -> Option<ExpnId> {
477 HygieneData::with(|data| data.adjust(self, expn_id))
480 /// Like `SyntaxContext::adjust`, but also modernizes `self`.
481 pub fn modernize_and_adjust(&mut self, expn_id: ExpnId) -> Option<ExpnId> {
482 HygieneData::with(|data| {
483 *self = data.modern(*self);
484 data.adjust(self, expn_id)
488 /// Adjust this context for resolution in a scope created by the given expansion
489 /// via a glob import with the given `SyntaxContext`.
494 /// macro m($i:ident) {
496 /// pub fn f() {} // `f`'s `SyntaxContext` has a single `ExpnId` from `m`.
497 /// pub fn $i() {} // `$i`'s `SyntaxContext` is empty.
500 /// macro n($j:ident) {
502 /// f(); // `f`'s `SyntaxContext` has a mark from `m` and a mark from `n`
503 /// //^ `glob_adjust` removes the mark from `n`, so this resolves to `foo::f`.
504 /// $i(); // `$i`'s `SyntaxContext` has a mark from `n`
505 /// //^ `glob_adjust` removes the mark from `n`, so this resolves to `foo::$i`.
506 /// $j(); // `$j`'s `SyntaxContext` has a mark from `m`
507 /// //^ This cannot be glob-adjusted, so this is a resolution error.
511 /// This returns `None` if the context cannot be glob-adjusted.
512 /// Otherwise, it returns the scope to use when privacy checking (see `adjust` for details).
513 pub fn glob_adjust(&mut self, expn_id: ExpnId, glob_span: Span) -> Option<Option<ExpnId>> {
514 HygieneData::with(|data| {
515 let mut scope = None;
516 let mut glob_ctxt = data.modern(glob_span.ctxt());
517 while !data.is_descendant_of(expn_id, data.outer_expn(glob_ctxt)) {
518 scope = Some(data.remove_mark(&mut glob_ctxt));
519 if data.remove_mark(self) != scope.unwrap() {
523 if data.adjust(self, expn_id).is_some() {
530 /// Undo `glob_adjust` if possible:
533 /// if let Some(privacy_checking_scope) = self.reverse_glob_adjust(expansion, glob_ctxt) {
534 /// assert!(self.glob_adjust(expansion, glob_ctxt) == Some(privacy_checking_scope));
537 pub fn reverse_glob_adjust(&mut self, expn_id: ExpnId, glob_span: Span)
538 -> Option<Option<ExpnId>> {
539 HygieneData::with(|data| {
540 if data.adjust(self, expn_id).is_some() {
544 let mut glob_ctxt = data.modern(glob_span.ctxt());
545 let mut marks = Vec::new();
546 while !data.is_descendant_of(expn_id, data.outer_expn(glob_ctxt)) {
547 marks.push(data.remove_mark(&mut glob_ctxt));
550 let scope = marks.last().cloned();
551 while let Some(mark) = marks.pop() {
552 *self = data.apply_mark(*self, mark);
558 pub fn hygienic_eq(self, other: SyntaxContext, expn_id: ExpnId) -> bool {
559 HygieneData::with(|data| {
560 let mut self_modern = data.modern(self);
561 data.adjust(&mut self_modern, expn_id);
562 self_modern == data.modern(other)
567 pub fn modern(self) -> SyntaxContext {
568 HygieneData::with(|data| data.modern(self))
572 pub fn modern_and_legacy(self) -> SyntaxContext {
573 HygieneData::with(|data| data.modern_and_legacy(self))
577 pub fn outer_expn(self) -> ExpnId {
578 HygieneData::with(|data| data.outer_expn(self))
581 /// `ctxt.outer_expn_info()` is equivalent to but faster than
582 /// `ctxt.outer_expn().expn_info()`.
584 pub fn outer_expn_info(self) -> Option<ExpnInfo> {
585 HygieneData::with(|data| data.expn_info(data.outer_expn(self)).cloned())
588 /// `ctxt.outer_expn_with_info()` is equivalent to but faster than
589 /// `{ let outer = ctxt.outer_expn(); (outer, outer.expn_info()) }`.
591 pub fn outer_expn_with_info(self) -> (ExpnId, Option<ExpnInfo>) {
592 HygieneData::with(|data| {
593 let outer = data.outer_expn(self);
594 (outer, data.expn_info(outer).cloned())
598 pub fn dollar_crate_name(self) -> Symbol {
599 HygieneData::with(|data| data.syntax_context_data[self.0 as usize].dollar_crate_name)
603 impl fmt::Debug for SyntaxContext {
604 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
605 write!(f, "#{}", self.0)
610 /// Creates a fresh expansion with given properties.
611 /// Expansions are normally created by macros, but in some cases expansions are created for
612 /// other compiler-generated code to set per-span properties like allowed unstable features.
613 /// The returned span belongs to the created expansion and has the new properties,
614 /// but its location is inherited from the current span.
615 pub fn fresh_expansion(self, parent: ExpnId, expn_info: ExpnInfo) -> Span {
616 HygieneData::with(|data| {
617 let expn_id = data.fresh_expn(parent, Some(expn_info));
618 self.with_ctxt(data.apply_mark(SyntaxContext::empty(), expn_id))
623 /// A subset of properties from both macro definition and macro call available through global data.
624 /// Avoid using this if you have access to the original definition or call structures.
625 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
626 pub struct ExpnInfo {
627 // --- The part unique to each expansion.
628 /// The location of the actual macro invocation or syntax sugar , e.g.
629 /// `let x = foo!();` or `if let Some(y) = x {}`
631 /// This may recursively refer to other macro invocations, e.g., if
632 /// `foo!()` invoked `bar!()` internally, and there was an
633 /// expression inside `bar!`; the call_site of the expression in
634 /// the expansion would point to the `bar!` invocation; that
635 /// call_site span would have its own ExpnInfo, with the call_site
636 /// pointing to the `foo!` invocation.
638 /// The kind of this expansion - macro or compiler desugaring.
641 // --- The part specific to the macro/desugaring definition.
642 // --- FIXME: Share it between expansions with the same definition.
643 /// The span of the macro definition (possibly dummy).
644 /// This span serves only informational purpose and is not used for resolution.
646 /// Transparency used by `apply_mark` for the expansion with this expansion info by default.
647 pub default_transparency: Transparency,
648 /// List of #[unstable]/feature-gated features that the macro is allowed to use
649 /// internally without forcing the whole crate to opt-in
651 pub allow_internal_unstable: Option<Lrc<[Symbol]>>,
652 /// Whether the macro is allowed to use `unsafe` internally
653 /// even if the user crate has `#![forbid(unsafe_code)]`.
654 pub allow_internal_unsafe: bool,
655 /// Enables the macro helper hack (`ident!(...)` -> `$crate::ident!(...)`)
656 /// for a given macro.
657 pub local_inner_macros: bool,
658 /// Edition of the crate in which the macro is defined.
659 pub edition: Edition,
663 /// Constructs an expansion info with default properties.
664 pub fn default(kind: ExpnKind, call_site: Span, edition: Edition) -> ExpnInfo {
669 default_transparency: Transparency::SemiTransparent,
670 allow_internal_unstable: None,
671 allow_internal_unsafe: false,
672 local_inner_macros: false,
677 pub fn allow_unstable(kind: ExpnKind, call_site: Span, edition: Edition,
678 allow_internal_unstable: Lrc<[Symbol]>) -> ExpnInfo {
680 allow_internal_unstable: Some(allow_internal_unstable),
681 ..ExpnInfo::default(kind, call_site, edition)
687 #[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
689 /// No expansion, aka root expansion. Only `ExpnId::root()` has this kind.
691 /// Expansion produced by a macro.
692 /// FIXME: Some code injected by the compiler before HIR lowering also gets this kind.
693 Macro(MacroKind, Symbol),
694 /// Desugaring done by the compiler during HIR lowering.
695 Desugaring(DesugaringKind)
699 pub fn descr(&self) -> Symbol {
701 ExpnKind::Root => kw::PathRoot,
702 ExpnKind::Macro(_, descr) => descr,
703 ExpnKind::Desugaring(kind) => Symbol::intern(kind.descr()),
708 /// The kind of macro invocation or definition.
709 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
711 /// A bang macro `foo!()`.
713 /// An attribute macro `#[foo]`.
715 /// A derive macro `#[derive(Foo)]`
720 pub fn descr(self) -> &'static str {
722 MacroKind::Bang => "macro",
723 MacroKind::Attr => "attribute macro",
724 MacroKind::Derive => "derive macro",
728 pub fn article(self) -> &'static str {
730 MacroKind::Attr => "an",
736 /// The kind of compiler desugaring.
737 #[derive(Clone, Copy, PartialEq, Debug, RustcEncodable, RustcDecodable)]
738 pub enum DesugaringKind {
739 /// We desugar `if c { i } else { e }` to `match $ExprKind::Use(c) { true => i, _ => e }`.
740 /// However, we do not want to blame `c` for unreachability but rather say that `i`
741 /// is unreachable. This desugaring kind allows us to avoid blaming `c`.
742 /// This also applies to `while` loops.
746 /// Desugaring of an `impl Trait` in return type position
747 /// to an `type Foo = impl Trait;` and replacing the
748 /// `impl Trait` with `Foo`.
755 impl DesugaringKind {
756 /// The description wording should combine well with "desugaring of {}".
757 fn descr(self) -> &'static str {
759 DesugaringKind::CondTemporary => "`if` or `while` condition",
760 DesugaringKind::Async => "`async` block or function",
761 DesugaringKind::Await => "`await` expression",
762 DesugaringKind::QuestionMark => "operator `?`",
763 DesugaringKind::TryBlock => "`try` block",
764 DesugaringKind::OpaqueTy => "`impl Trait`",
765 DesugaringKind::ForLoop => "`for` loop",
770 impl Encodable for SyntaxContext {
771 fn encode<E: Encoder>(&self, _: &mut E) -> Result<(), E::Error> {
772 Ok(()) // FIXME(jseyfried) intercrate hygiene
776 impl Decodable for SyntaxContext {
777 fn decode<D: Decoder>(_: &mut D) -> Result<SyntaxContext, D::Error> {
778 Ok(SyntaxContext::empty()) // FIXME(jseyfried) intercrate hygiene