]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_pos/hygiene.rs
Introduce and use `SyntaxContext::outer_expn_info()`.
[rust.git] / src / libsyntax_pos / hygiene.rs
1 //! Machinery for hygienic macros, inspired by the `MTWT[1]` paper.
2 //!
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>
7
8 use crate::GLOBALS;
9 use crate::Span;
10 use crate::edition::Edition;
11 use crate::symbol::{kw, Symbol};
12
13 use serialize::{Encodable, Decodable, Encoder, Decoder};
14 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
15 use rustc_data_structures::sync::Lrc;
16 use std::{fmt, mem};
17
18 /// A SyntaxContext represents a chain of macro expansions (represented by marks).
19 #[derive(Clone, Copy, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
20 pub struct SyntaxContext(u32);
21
22 #[derive(Copy, Clone, Debug)]
23 struct SyntaxContextData {
24     outer_mark: Mark,
25     transparency: Transparency,
26     prev_ctxt: SyntaxContext,
27     /// This context, but with all transparent and semi-transparent marks filtered away.
28     opaque: SyntaxContext,
29     /// This context, but with all transparent marks filtered away.
30     opaque_and_semitransparent: SyntaxContext,
31     /// Name of the crate to which `$crate` with this context would resolve.
32     dollar_crate_name: Symbol,
33 }
34
35 /// A mark is a unique ID associated with a macro expansion.
36 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
37 pub struct Mark(u32);
38
39 #[derive(Clone, Debug)]
40 struct MarkData {
41     parent: Mark,
42     default_transparency: Transparency,
43     expn_info: Option<ExpnInfo>,
44 }
45
46 /// A property of a macro expansion that determines how identifiers
47 /// produced by that expansion are resolved.
48 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Hash, Debug)]
49 pub enum Transparency {
50     /// Identifier produced by a transparent expansion is always resolved at call-site.
51     /// Call-site spans in procedural macros, hygiene opt-out in `macro` should use this.
52     Transparent,
53     /// Identifier produced by a semi-transparent expansion may be resolved
54     /// either at call-site or at definition-site.
55     /// If it's a local variable, label or `$crate` then it's resolved at def-site.
56     /// Otherwise it's resolved at call-site.
57     /// `macro_rules` macros behave like this, built-in macros currently behave like this too,
58     /// but that's an implementation detail.
59     SemiTransparent,
60     /// Identifier produced by an opaque expansion is always resolved at definition-site.
61     /// Def-site spans in procedural macros, identifiers from `macro` by default use this.
62     Opaque,
63 }
64
65 impl Mark {
66     pub fn fresh(parent: Mark) -> Self {
67         HygieneData::with(|data| {
68             data.marks.push(MarkData {
69                 parent,
70                 // By default expansions behave like `macro_rules`.
71                 default_transparency: Transparency::SemiTransparent,
72                 expn_info: None,
73             });
74             Mark(data.marks.len() as u32 - 1)
75         })
76     }
77
78     /// The mark of the theoretical expansion that generates freshly parsed, unexpanded AST.
79     #[inline]
80     pub fn root() -> Self {
81         Mark(0)
82     }
83
84     #[inline]
85     pub fn as_u32(self) -> u32 {
86         self.0
87     }
88
89     #[inline]
90     pub fn from_u32(raw: u32) -> Mark {
91         Mark(raw)
92     }
93
94     #[inline]
95     pub fn parent(self) -> Mark {
96         HygieneData::with(|data| data.marks[self.0 as usize].parent)
97     }
98
99     #[inline]
100     pub fn expn_info(self) -> Option<ExpnInfo> {
101         HygieneData::with(|data| data.marks[self.0 as usize].expn_info.clone())
102     }
103
104     #[inline]
105     pub fn set_expn_info(self, info: ExpnInfo) {
106         HygieneData::with(|data| data.marks[self.0 as usize].expn_info = Some(info))
107     }
108
109     #[inline]
110     pub fn set_default_transparency(self, transparency: Transparency) {
111         assert_ne!(self, Mark::root());
112         HygieneData::with(|data| data.marks[self.0 as usize].default_transparency = transparency)
113     }
114
115     pub fn is_descendant_of(mut self, ancestor: Mark) -> bool {
116         HygieneData::with(|data| {
117             while self != ancestor {
118                 if self == Mark::root() {
119                     return false;
120                 }
121                 self = data.marks[self.0 as usize].parent;
122             }
123             true
124         })
125     }
126
127     /// `mark.outer_is_descendant_of(ctxt)` is equivalent to but faster than
128     /// `mark.is_descendant_of(ctxt.outer())`.
129     pub fn outer_is_descendant_of(mut self, ctxt: SyntaxContext) -> bool {
130         HygieneData::with(|data| {
131             let outer = data.syntax_contexts[ctxt.0 as usize].outer_mark;
132             while self != outer {
133                 if self == Mark::root() {
134                     return false;
135                 }
136                 self = data.marks[self.0 as usize].parent;
137             }
138             true
139         })
140     }
141
142     /// Computes a mark such that both input marks are descendants of (or equal to) the returned
143     /// mark. That is, the following holds:
144     ///
145     /// ```rust
146     /// let la = least_ancestor(a, b);
147     /// assert!(a.is_descendant_of(la))
148     /// assert!(b.is_descendant_of(la))
149     /// ```
150     pub fn least_ancestor(mut a: Mark, mut b: Mark) -> Mark {
151         HygieneData::with(|data| {
152             // Compute the path from a to the root
153             let mut a_path = FxHashSet::<Mark>::default();
154             while a != Mark::root() {
155                 a_path.insert(a);
156                 a = data.marks[a.0 as usize].parent;
157             }
158
159             // While the path from b to the root hasn't intersected, move up the tree
160             while !a_path.contains(&b) {
161                 b = data.marks[b.0 as usize].parent;
162             }
163
164             b
165         })
166     }
167
168     // Used for enabling some compatibility fallback in resolve.
169     #[inline]
170     pub fn looks_like_proc_macro_derive(self) -> bool {
171         HygieneData::with(|data| {
172             let mark_data = &data.marks[self.0 as usize];
173             if mark_data.default_transparency == Transparency::Opaque {
174                 if let Some(expn_info) = &mark_data.expn_info {
175                     if let ExpnFormat::MacroAttribute(name) = expn_info.format {
176                         if name.as_str().starts_with("derive(") {
177                             return true;
178                         }
179                     }
180                 }
181             }
182             false
183         })
184     }
185 }
186
187 #[derive(Debug)]
188 crate struct HygieneData {
189     marks: Vec<MarkData>,
190     syntax_contexts: Vec<SyntaxContextData>,
191     markings: FxHashMap<(SyntaxContext, Mark, Transparency), SyntaxContext>,
192 }
193
194 impl HygieneData {
195     crate fn new() -> Self {
196         HygieneData {
197             marks: vec![MarkData {
198                 parent: Mark::root(),
199                 // If the root is opaque, then loops searching for an opaque mark
200                 // will automatically stop after reaching it.
201                 default_transparency: Transparency::Opaque,
202                 expn_info: None,
203             }],
204             syntax_contexts: vec![SyntaxContextData {
205                 outer_mark: Mark::root(),
206                 transparency: Transparency::Opaque,
207                 prev_ctxt: SyntaxContext(0),
208                 opaque: SyntaxContext(0),
209                 opaque_and_semitransparent: SyntaxContext(0),
210                 dollar_crate_name: kw::DollarCrate,
211             }],
212             markings: FxHashMap::default(),
213         }
214     }
215
216     fn with<T, F: FnOnce(&mut HygieneData) -> T>(f: F) -> T {
217         GLOBALS.with(|globals| f(&mut *globals.hygiene_data.borrow_mut()))
218     }
219 }
220
221 pub fn clear_markings() {
222     HygieneData::with(|data| data.markings = FxHashMap::default());
223 }
224
225 impl SyntaxContext {
226     #[inline]
227     pub const fn empty() -> Self {
228         SyntaxContext(0)
229     }
230
231     #[inline]
232     crate fn as_u32(self) -> u32 {
233         self.0
234     }
235
236     #[inline]
237     crate fn from_u32(raw: u32) -> SyntaxContext {
238         SyntaxContext(raw)
239     }
240
241     // Allocate a new SyntaxContext with the given ExpnInfo. This is used when
242     // deserializing Spans from the incr. comp. cache.
243     // FIXME(mw): This method does not restore MarkData::parent or
244     // SyntaxContextData::prev_ctxt or SyntaxContextData::opaque. These things
245     // don't seem to be used after HIR lowering, so everything should be fine
246     // as long as incremental compilation does not kick in before that.
247     pub fn allocate_directly(expansion_info: ExpnInfo) -> Self {
248         HygieneData::with(|data| {
249             data.marks.push(MarkData {
250                 parent: Mark::root(),
251                 default_transparency: Transparency::SemiTransparent,
252                 expn_info: Some(expansion_info),
253             });
254
255             let mark = Mark(data.marks.len() as u32 - 1);
256
257             data.syntax_contexts.push(SyntaxContextData {
258                 outer_mark: mark,
259                 transparency: Transparency::SemiTransparent,
260                 prev_ctxt: SyntaxContext::empty(),
261                 opaque: SyntaxContext::empty(),
262                 opaque_and_semitransparent: SyntaxContext::empty(),
263                 dollar_crate_name: kw::DollarCrate,
264             });
265             SyntaxContext(data.syntax_contexts.len() as u32 - 1)
266         })
267     }
268
269     /// Extend a syntax context with a given mark and default transparency for that mark.
270     pub fn apply_mark(self, mark: Mark) -> SyntaxContext {
271         assert_ne!(mark, Mark::root());
272         self.apply_mark_with_transparency(
273             mark, HygieneData::with(|data| data.marks[mark.0 as usize].default_transparency)
274         )
275     }
276
277     /// Extend a syntax context with a given mark and transparency
278     pub fn apply_mark_with_transparency(self, mark: Mark, transparency: Transparency)
279                                         -> SyntaxContext {
280         assert_ne!(mark, Mark::root());
281         if transparency == Transparency::Opaque {
282             return self.apply_mark_internal(mark, transparency);
283         }
284
285         let call_site_ctxt =
286             mark.expn_info().map_or(SyntaxContext::empty(), |info| info.call_site.ctxt());
287         let call_site_ctxt = if transparency == Transparency::SemiTransparent {
288             call_site_ctxt.modern()
289         } else {
290             call_site_ctxt.modern_and_legacy()
291         };
292
293         if call_site_ctxt == SyntaxContext::empty() {
294             return self.apply_mark_internal(mark, transparency);
295         }
296
297         // Otherwise, `mark` is a macros 1.0 definition and the call site is in a
298         // macros 2.0 expansion, i.e., a macros 1.0 invocation is in a macros 2.0 definition.
299         //
300         // In this case, the tokens from the macros 1.0 definition inherit the hygiene
301         // at their invocation. That is, we pretend that the macros 1.0 definition
302         // was defined at its invocation (i.e., inside the macros 2.0 definition)
303         // so that the macros 2.0 definition remains hygienic.
304         //
305         // See the example at `test/run-pass/hygiene/legacy_interaction.rs`.
306         let mut ctxt = call_site_ctxt;
307         for (mark, transparency) in self.marks() {
308             ctxt = ctxt.apply_mark_internal(mark, transparency);
309         }
310         ctxt.apply_mark_internal(mark, transparency)
311     }
312
313     fn apply_mark_internal(self, mark: Mark, transparency: Transparency) -> SyntaxContext {
314         HygieneData::with(|data| {
315             let syntax_contexts = &mut data.syntax_contexts;
316             let mut opaque = syntax_contexts[self.0 as usize].opaque;
317             let mut opaque_and_semitransparent =
318                 syntax_contexts[self.0 as usize].opaque_and_semitransparent;
319
320             if transparency >= Transparency::Opaque {
321                 let prev_ctxt = opaque;
322                 opaque = *data.markings.entry((prev_ctxt, mark, transparency)).or_insert_with(|| {
323                     let new_opaque = SyntaxContext(syntax_contexts.len() as u32);
324                     syntax_contexts.push(SyntaxContextData {
325                         outer_mark: mark,
326                         transparency,
327                         prev_ctxt,
328                         opaque: new_opaque,
329                         opaque_and_semitransparent: new_opaque,
330                         dollar_crate_name: kw::DollarCrate,
331                     });
332                     new_opaque
333                 });
334             }
335
336             if transparency >= Transparency::SemiTransparent {
337                 let prev_ctxt = opaque_and_semitransparent;
338                 opaque_and_semitransparent =
339                         *data.markings.entry((prev_ctxt, mark, transparency)).or_insert_with(|| {
340                     let new_opaque_and_semitransparent =
341                         SyntaxContext(syntax_contexts.len() as u32);
342                     syntax_contexts.push(SyntaxContextData {
343                         outer_mark: mark,
344                         transparency,
345                         prev_ctxt,
346                         opaque,
347                         opaque_and_semitransparent: new_opaque_and_semitransparent,
348                         dollar_crate_name: kw::DollarCrate,
349                     });
350                     new_opaque_and_semitransparent
351                 });
352             }
353
354             let prev_ctxt = self;
355             *data.markings.entry((prev_ctxt, mark, transparency)).or_insert_with(|| {
356                 let new_opaque_and_semitransparent_and_transparent =
357                     SyntaxContext(syntax_contexts.len() as u32);
358                 syntax_contexts.push(SyntaxContextData {
359                     outer_mark: mark,
360                     transparency,
361                     prev_ctxt,
362                     opaque,
363                     opaque_and_semitransparent,
364                     dollar_crate_name: kw::DollarCrate,
365                 });
366                 new_opaque_and_semitransparent_and_transparent
367             })
368         })
369     }
370
371     /// Pulls a single mark off of the syntax context. This effectively moves the
372     /// context up one macro definition level. That is, if we have a nested macro
373     /// definition as follows:
374     ///
375     /// ```rust
376     /// macro_rules! f {
377     ///    macro_rules! g {
378     ///        ...
379     ///    }
380     /// }
381     /// ```
382     ///
383     /// and we have a SyntaxContext that is referring to something declared by an invocation
384     /// of g (call it g1), calling remove_mark will result in the SyntaxContext for the
385     /// invocation of f that created g1.
386     /// Returns the mark that was removed.
387     pub fn remove_mark(&mut self) -> Mark {
388         HygieneData::with(|data| {
389             let outer_mark = data.syntax_contexts[self.0 as usize].outer_mark;
390             *self = data.syntax_contexts[self.0 as usize].prev_ctxt;
391             outer_mark
392         })
393     }
394
395     pub fn marks(mut self) -> Vec<(Mark, Transparency)> {
396         HygieneData::with(|data| {
397             let mut marks = Vec::new();
398             while self != SyntaxContext::empty() {
399                 let ctxt_data = &data.syntax_contexts[self.0 as usize];
400                 marks.push((ctxt_data.outer_mark, ctxt_data.transparency));
401                 self = ctxt_data.prev_ctxt;
402             }
403             marks.reverse();
404             marks
405         })
406     }
407
408     /// Adjust this context for resolution in a scope created by the given expansion.
409     /// For example, consider the following three resolutions of `f`:
410     ///
411     /// ```rust
412     /// mod foo { pub fn f() {} } // `f`'s `SyntaxContext` is empty.
413     /// m!(f);
414     /// macro m($f:ident) {
415     ///     mod bar {
416     ///         pub fn f() {} // `f`'s `SyntaxContext` has a single `Mark` from `m`.
417     ///         pub fn $f() {} // `$f`'s `SyntaxContext` is empty.
418     ///     }
419     ///     foo::f(); // `f`'s `SyntaxContext` has a single `Mark` from `m`
420     ///     //^ Since `mod foo` is outside this expansion, `adjust` removes the mark from `f`,
421     ///     //| and it resolves to `::foo::f`.
422     ///     bar::f(); // `f`'s `SyntaxContext` has a single `Mark` from `m`
423     ///     //^ Since `mod bar` not outside this expansion, `adjust` does not change `f`,
424     ///     //| and it resolves to `::bar::f`.
425     ///     bar::$f(); // `f`'s `SyntaxContext` is empty.
426     ///     //^ Since `mod bar` is not outside this expansion, `adjust` does not change `$f`,
427     ///     //| and it resolves to `::bar::$f`.
428     /// }
429     /// ```
430     /// This returns the expansion whose definition scope we use to privacy check the resolution,
431     /// or `None` if we privacy check as usual (i.e., not w.r.t. a macro definition scope).
432     pub fn adjust(&mut self, expansion: Mark) -> Option<Mark> {
433         let mut scope = None;
434         while !expansion.outer_is_descendant_of(*self) {
435             scope = Some(self.remove_mark());
436         }
437         scope
438     }
439
440     /// Adjust this context for resolution in a scope created by the given expansion
441     /// via a glob import with the given `SyntaxContext`.
442     /// For example:
443     ///
444     /// ```rust
445     /// m!(f);
446     /// macro m($i:ident) {
447     ///     mod foo {
448     ///         pub fn f() {} // `f`'s `SyntaxContext` has a single `Mark` from `m`.
449     ///         pub fn $i() {} // `$i`'s `SyntaxContext` is empty.
450     ///     }
451     ///     n(f);
452     ///     macro n($j:ident) {
453     ///         use foo::*;
454     ///         f(); // `f`'s `SyntaxContext` has a mark from `m` and a mark from `n`
455     ///         //^ `glob_adjust` removes the mark from `n`, so this resolves to `foo::f`.
456     ///         $i(); // `$i`'s `SyntaxContext` has a mark from `n`
457     ///         //^ `glob_adjust` removes the mark from `n`, so this resolves to `foo::$i`.
458     ///         $j(); // `$j`'s `SyntaxContext` has a mark from `m`
459     ///         //^ This cannot be glob-adjusted, so this is a resolution error.
460     ///     }
461     /// }
462     /// ```
463     /// This returns `None` if the context cannot be glob-adjusted.
464     /// Otherwise, it returns the scope to use when privacy checking (see `adjust` for details).
465     pub fn glob_adjust(&mut self, expansion: Mark, mut glob_ctxt: SyntaxContext)
466                        -> Option<Option<Mark>> {
467         let mut scope = None;
468         while !expansion.outer_is_descendant_of(glob_ctxt) {
469             scope = Some(glob_ctxt.remove_mark());
470             if self.remove_mark() != scope.unwrap() {
471                 return None;
472             }
473         }
474         if self.adjust(expansion).is_some() {
475             return None;
476         }
477         Some(scope)
478     }
479
480     /// Undo `glob_adjust` if possible:
481     ///
482     /// ```rust
483     /// if let Some(privacy_checking_scope) = self.reverse_glob_adjust(expansion, glob_ctxt) {
484     ///     assert!(self.glob_adjust(expansion, glob_ctxt) == Some(privacy_checking_scope));
485     /// }
486     /// ```
487     pub fn reverse_glob_adjust(&mut self, expansion: Mark, mut glob_ctxt: SyntaxContext)
488                                -> Option<Option<Mark>> {
489         if self.adjust(expansion).is_some() {
490             return None;
491         }
492
493         let mut marks = Vec::new();
494         while !expansion.outer_is_descendant_of(glob_ctxt) {
495             marks.push(glob_ctxt.remove_mark());
496         }
497
498         let scope = marks.last().cloned();
499         while let Some(mark) = marks.pop() {
500             *self = self.apply_mark(mark);
501         }
502         Some(scope)
503     }
504
505     #[inline]
506     pub fn modern(self) -> SyntaxContext {
507         HygieneData::with(|data| data.syntax_contexts[self.0 as usize].opaque)
508     }
509
510     #[inline]
511     pub fn modern_and_legacy(self) -> SyntaxContext {
512         HygieneData::with(|data| data.syntax_contexts[self.0 as usize].opaque_and_semitransparent)
513     }
514
515     #[inline]
516     pub fn outer(self) -> Mark {
517         HygieneData::with(|data| data.syntax_contexts[self.0 as usize].outer_mark)
518     }
519
520     /// `ctxt.outer_expn_info()` is equivalent to but faster than
521     /// `ctxt.outer().expn_info()`.
522     #[inline]
523     pub fn outer_expn_info(self) -> Option<ExpnInfo> {
524         HygieneData::with(|data| {
525             let outer = data.syntax_contexts[self.0 as usize].outer_mark;
526             data.marks[outer.0 as usize].expn_info.clone()
527         })
528     }
529
530     pub fn dollar_crate_name(self) -> Symbol {
531         HygieneData::with(|data| data.syntax_contexts[self.0 as usize].dollar_crate_name)
532     }
533
534     pub fn set_dollar_crate_name(self, dollar_crate_name: Symbol) {
535         HygieneData::with(|data| {
536             let prev_dollar_crate_name = mem::replace(
537                 &mut data.syntax_contexts[self.0 as usize].dollar_crate_name, dollar_crate_name
538             );
539             assert!(dollar_crate_name == prev_dollar_crate_name ||
540                     prev_dollar_crate_name == kw::DollarCrate,
541                     "$crate name is reset for a syntax context");
542         })
543     }
544 }
545
546 impl fmt::Debug for SyntaxContext {
547     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
548         write!(f, "#{}", self.0)
549     }
550 }
551
552 /// Extra information for tracking spans of macro and syntax sugar expansion
553 #[derive(Clone, Hash, Debug, RustcEncodable, RustcDecodable)]
554 pub struct ExpnInfo {
555     /// The location of the actual macro invocation or syntax sugar , e.g.
556     /// `let x = foo!();` or `if let Some(y) = x {}`
557     ///
558     /// This may recursively refer to other macro invocations, e.g., if
559     /// `foo!()` invoked `bar!()` internally, and there was an
560     /// expression inside `bar!`; the call_site of the expression in
561     /// the expansion would point to the `bar!` invocation; that
562     /// call_site span would have its own ExpnInfo, with the call_site
563     /// pointing to the `foo!` invocation.
564     pub call_site: Span,
565     /// The span of the macro definition itself. The macro may not
566     /// have a sensible definition span (e.g., something defined
567     /// completely inside libsyntax) in which case this is None.
568     /// This span serves only informational purpose and is not used for resolution.
569     pub def_site: Option<Span>,
570     /// The format with which the macro was invoked.
571     pub format: ExpnFormat,
572     /// List of #[unstable]/feature-gated features that the macro is allowed to use
573     /// internally without forcing the whole crate to opt-in
574     /// to them.
575     pub allow_internal_unstable: Option<Lrc<[Symbol]>>,
576     /// Whether the macro is allowed to use `unsafe` internally
577     /// even if the user crate has `#![forbid(unsafe_code)]`.
578     pub allow_internal_unsafe: bool,
579     /// Enables the macro helper hack (`ident!(...)` -> `$crate::ident!(...)`)
580     /// for a given macro.
581     pub local_inner_macros: bool,
582     /// Edition of the crate in which the macro is defined.
583     pub edition: Edition,
584 }
585
586 /// The source of expansion.
587 #[derive(Clone, Hash, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
588 pub enum ExpnFormat {
589     /// e.g., #[derive(...)] <item>
590     MacroAttribute(Symbol),
591     /// e.g., `format!()`
592     MacroBang(Symbol),
593     /// Desugaring done by the compiler during HIR lowering.
594     CompilerDesugaring(CompilerDesugaringKind)
595 }
596
597 impl ExpnFormat {
598     pub fn name(&self) -> Symbol {
599         match *self {
600             ExpnFormat::MacroBang(name) | ExpnFormat::MacroAttribute(name) => name,
601             ExpnFormat::CompilerDesugaring(kind) => kind.name(),
602         }
603     }
604 }
605
606 /// The kind of compiler desugaring.
607 #[derive(Clone, Copy, Hash, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
608 pub enum CompilerDesugaringKind {
609     /// We desugar `if c { i } else { e }` to `match $ExprKind::Use(c) { true => i, _ => e }`.
610     /// However, we do not want to blame `c` for unreachability but rather say that `i`
611     /// is unreachable. This desugaring kind allows us to avoid blaming `c`.
612     IfTemporary,
613     QuestionMark,
614     TryBlock,
615     /// Desugaring of an `impl Trait` in return type position
616     /// to an `existential type Foo: Trait;` and replacing the
617     /// `impl Trait` with `Foo`.
618     ExistentialReturnType,
619     Async,
620     Await,
621     ForLoop,
622 }
623
624 impl CompilerDesugaringKind {
625     pub fn name(self) -> Symbol {
626         Symbol::intern(match self {
627             CompilerDesugaringKind::IfTemporary => "if",
628             CompilerDesugaringKind::Async => "async",
629             CompilerDesugaringKind::Await => "await",
630             CompilerDesugaringKind::QuestionMark => "?",
631             CompilerDesugaringKind::TryBlock => "try block",
632             CompilerDesugaringKind::ExistentialReturnType => "existential type",
633             CompilerDesugaringKind::ForLoop => "for loop",
634         })
635     }
636 }
637
638 impl Encodable for SyntaxContext {
639     fn encode<E: Encoder>(&self, _: &mut E) -> Result<(), E::Error> {
640         Ok(()) // FIXME(jseyfried) intercrate hygiene
641     }
642 }
643
644 impl Decodable for SyntaxContext {
645     fn decode<D: Decoder>(_: &mut D) -> Result<SyntaxContext, D::Error> {
646         Ok(SyntaxContext::empty()) // FIXME(jseyfried) intercrate hygiene
647     }
648 }