]> git.lizzy.rs Git - rust.git/commitdiff
proc_macro: Accept `$crate` as an identifier if it comes from the compiler
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Sun, 9 Dec 2018 14:31:12 +0000 (17:31 +0300)
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Wed, 19 Dec 2018 20:17:53 +0000 (23:17 +0300)
src/libsyntax_ext/proc_macro_server.rs
src/test/ui/proc-macro/auxiliary/dollar-crate.rs [new file with mode: 0644]
src/test/ui/proc-macro/dollar-crate.rs [new file with mode: 0644]

index ea4a8afc0aabd19ff764d1eba9328ddd8f0f6992..ca960cbe41b5d288a7e78aa3a590aa4f122f52dd 100644 (file)
@@ -150,6 +150,8 @@ macro_rules! op {
             Question => op!('?'),
             SingleQuote => op!('\''),
 
+            Ident(ident, false) if ident.name == keywords::DollarCrate.name() =>
+                tt!(Ident::dollar_crate()),
             Ident(ident, is_raw) => tt!(Ident::new(ident.name, is_raw)),
             Lifetime(ident) => {
                 let ident = ident.without_first_quote();
@@ -359,6 +361,10 @@ fn new(sym: Symbol, is_raw: bool, span: Span) -> Ident {
         }
         Ident { sym, is_raw, span }
     }
+    fn dollar_crate(span: Span) -> Ident {
+        // `$crate` is accepted as an ident only if it comes from the compiler.
+        Ident { sym: keywords::DollarCrate.name(), is_raw: false, span }
+    }
 }
 
 // FIXME(eddyb) `Literal` should not expose internal `Debug` impls.
diff --git a/src/test/ui/proc-macro/auxiliary/dollar-crate.rs b/src/test/ui/proc-macro/auxiliary/dollar-crate.rs
new file mode 100644 (file)
index 0000000..b0727a3
--- /dev/null
@@ -0,0 +1,12 @@
+// force-host
+// no-prefer-dynamic
+
+#![crate_type = "proc-macro"]
+
+extern crate proc_macro;
+use proc_macro::TokenStream;
+
+#[proc_macro]
+pub fn normalize(input: TokenStream) -> TokenStream {
+    input.into_iter().collect()
+}
diff --git a/src/test/ui/proc-macro/dollar-crate.rs b/src/test/ui/proc-macro/dollar-crate.rs
new file mode 100644 (file)
index 0000000..b8b1ddd
--- /dev/null
@@ -0,0 +1,16 @@
+// compile-pass
+// aux-build:dollar-crate.rs
+
+extern crate dollar_crate;
+
+type S = u8;
+
+macro_rules! check { () => {
+    dollar_crate::normalize! {
+        type A = $crate::S;
+    }
+}}
+
+check!();
+
+fn main() {}