]> git.lizzy.rs Git - rust.git/commitdiff
rustc: Tweak expansion of #[proc_macro] for 2018
authorAlex Crichton <alex@alexcrichton.com>
Thu, 12 Jul 2018 21:04:24 +0000 (14:04 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Fri, 13 Jul 2018 16:32:29 +0000 (09:32 -0700)
The syntactical expansion of `#[proc_macro]` and related attributes currently
contains absolute paths which conflicts with a lint for the 2018 edition,
causing issues like #52214. This commit puts a band-aid on the issue by ensuring
that procedural macros can also migrate to the 2018 edition for now by tweaking
the expansion based on what features are activated. A more long-term solution
would probably tweak the edition hygiene of spans, but this should do the trick
for now.

Closes #52214

src/libsyntax_ext/proc_macro_registrar.rs
src/test/ui-fulldeps/rust-2018/proc-macro-crate-in-paths.rs [new file with mode: 0644]

index 85aa84acc4221f71279c51be50294cad20257a33..07852e5714b871f2573cbb7ec24a00ef0c283231 100644 (file)
@@ -23,6 +23,7 @@
 use syntax::parse::ParseSess;
 use syntax::ptr::P;
 use syntax::symbol::Symbol;
+use syntax::symbol::keywords;
 use syntax::visit::{self, Visitor};
 
 use syntax_pos::{Span, DUMMY_SP};
@@ -385,9 +386,13 @@ fn mk_registrar(cx: &mut ExtCtxt,
     let register_custom_derive = Ident::from_str("register_custom_derive");
     let register_attr_proc_macro = Ident::from_str("register_attr_proc_macro");
     let register_bang_proc_macro = Ident::from_str("register_bang_proc_macro");
+    let crate_kw = Ident::with_empty_ctxt(keywords::Crate.name());
+    let local_path = |cx: &mut ExtCtxt, sp: Span, name: Ident| {
+        cx.path(sp.with_ctxt(span.ctxt()), vec![crate_kw, name])
+    };
 
     let mut stmts = custom_derives.iter().map(|cd| {
-        let path = cx.path_global(cd.span, vec![cd.function_name]);
+        let path = local_path(cx, cd.span, cd.function_name);
         let trait_name = cx.expr_str(cd.span, cd.trait_name);
         let attrs = cx.expr_vec_slice(
             span,
@@ -404,7 +409,7 @@ fn mk_registrar(cx: &mut ExtCtxt,
 
     stmts.extend(custom_attrs.iter().map(|ca| {
         let name = cx.expr_str(ca.span, ca.function_name.name);
-        let path = cx.path_global(ca.span, vec![ca.function_name]);
+        let path = local_path(cx, ca.span, ca.function_name);
         let registrar = cx.expr_ident(ca.span, registrar);
 
         let ufcs_path = cx.path(span,
@@ -416,7 +421,7 @@ fn mk_registrar(cx: &mut ExtCtxt,
 
     stmts.extend(custom_macros.iter().map(|cm| {
         let name = cx.expr_str(cm.span, cm.function_name.name);
-        let path = cx.path_global(cm.span, vec![cm.function_name]);
+        let path = local_path(cx, cm.span, cm.function_name);
         let registrar = cx.expr_ident(cm.span, registrar);
 
         let ufcs_path = cx.path(span,
diff --git a/src/test/ui-fulldeps/rust-2018/proc-macro-crate-in-paths.rs b/src/test/ui-fulldeps/rust-2018/proc-macro-crate-in-paths.rs
new file mode 100644 (file)
index 0000000..1068c05
--- /dev/null
@@ -0,0 +1,24 @@
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// compile-pass
+
+#![crate_type = "proc-macro"]
+#![deny(rust_2018_compatibility)]
+#![feature(rust_2018_preview)]
+
+extern crate proc_macro;
+
+use proc_macro::TokenStream;
+
+#[proc_macro_derive(Template, attributes(template))]
+pub fn derive_template(input: TokenStream) -> TokenStream {
+    input
+}