summary refs log tree commit diff
path: root/src/libsyntax_pos
diff options
context:
space:
mode:
authorHunter Praska <hunter@wiggin-labs.com>2017-08-12 19:43:43 -0500
committerHunter Praska <hunter@wiggin-labs.com>2017-08-12 19:43:43 -0500
commit4acfef8f6319a1ae724ba174b4d87e0988d7e47d (patch)
tree6c6d0d74db5258b60b78474261c38a4afa03c4e7 /src/libsyntax_pos
parent045ca8b43b86f6a1671a59cc43cf535eebcb074e (diff)
downloadrust-4acfef8f6319a1ae724ba174b4d87e0988d7e47d.tar.gz
rust-4acfef8f6319a1ae724ba174b4d87e0988d7e47d.zip
Implement CompilerDesugaringKind enum
Diffstat (limited to 'src/libsyntax_pos')
-rw-r--r--src/libsyntax_pos/hygiene.rs38
-rw-r--r--src/libsyntax_pos/lib.rs13
2 files changed, 47 insertions, 4 deletions
diff --git a/src/libsyntax_pos/hygiene.rs b/src/libsyntax_pos/hygiene.rs
index 514cc26666e..6cef7775c34 100644
--- a/src/libsyntax_pos/hygiene.rs
+++ b/src/libsyntax_pos/hygiene.rs
@@ -323,8 +323,8 @@ impl NameAndSpan {
     pub fn name(&self) -> Symbol {
         match self.format {
             ExpnFormat::MacroAttribute(s) |
-            ExpnFormat::MacroBang(s) |
-            ExpnFormat::CompilerDesugaring(s) => s,
+            ExpnFormat::MacroBang(s) => s,
+            ExpnFormat::CompilerDesugaring(ref kind) => kind.as_symbol(),
         }
     }
 }
@@ -337,7 +337,39 @@ pub enum ExpnFormat {
     /// e.g. `format!()`
     MacroBang(Symbol),
     /// Desugaring done by the compiler during HIR lowering.
-    CompilerDesugaring(Symbol)
+    CompilerDesugaring(CompilerDesugaringKind)
+}
+
+/// The kind of compiler desugaring.
+#[derive(Clone, Hash, Debug, PartialEq, Eq)]
+pub enum CompilerDesugaringKind {
+    BackArrow,
+    DotFill,
+    QuestionMark,
+}
+
+impl CompilerDesugaringKind {
+    pub fn as_symbol(&self) -> Symbol {
+        use CompilerDesugaringKind::*;
+        let s = match *self {
+            BackArrow => "<-",
+            DotFill => "...",
+            QuestionMark => "?",
+        };
+        Symbol::intern(s)
+    }
+}
+
+impl<'a> From<&'a str> for CompilerDesugaringKind {
+    fn from(s: &'a str) -> Self {
+        use CompilerDesugaringKind::*;
+        match s {
+            "<-" => BackArrow,
+            "..." => DotFill,
+            "?" => QuestionMark,
+            _ => panic!("Invalid compiler desugaring"),
+        }
+    }
 }
 
 impl Encodable for SyntaxContext {
diff --git a/src/libsyntax_pos/lib.rs b/src/libsyntax_pos/lib.rs
index e162bc26412..f0e53c6408c 100644
--- a/src/libsyntax_pos/lib.rs
+++ b/src/libsyntax_pos/lib.rs
@@ -47,7 +47,7 @@ extern crate serialize;
 extern crate serialize as rustc_serialize; // used by deriving
 
 pub mod hygiene;
-pub use hygiene::{SyntaxContext, ExpnInfo, ExpnFormat, NameAndSpan};
+pub use hygiene::{SyntaxContext, ExpnInfo, ExpnFormat, NameAndSpan, CompilerDesugaringKind};
 
 pub mod symbol;
 
@@ -153,6 +153,17 @@ impl Span {
         }
     }
 
+    /// Check if this span arises from a compiler desugaring of kind `kind`.
+    pub fn is_compiler_desugaring(&self, kind: CompilerDesugaringKind) -> bool {
+        match self.ctxt.outer().expn_info() {
+            Some(info) => match info.callee.format {
+                ExpnFormat::CompilerDesugaring(k) => k == kind,
+                _ => false,
+            },
+            None => false,
+        }
+    }
+
     /// Check if a span is "internal" to a macro in which `unsafe`
     /// can be used without triggering the `unsafe_code` lint
     //  (that is, a macro marked with `#[allow_internal_unsafe]`).