Skip to content

Commit b486d3d

Browse files
committed
Auto merge of #52326 - alexcrichton:tweak-proc-macro-expand, r=petrochenkov
rustc: Tweak expansion of #[proc_macro] for 2018 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
2 parents cbcd81a + 94c9ea4 commit b486d3d

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/libsyntax_ext/proc_macro_registrar.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use syntax::fold::Folder;
2323
use syntax::parse::ParseSess;
2424
use syntax::ptr::P;
2525
use syntax::symbol::Symbol;
26+
use syntax::symbol::keywords;
2627
use syntax::visit::{self, Visitor};
2728

2829
use syntax_pos::{Span, DUMMY_SP};
@@ -380,9 +381,13 @@ fn mk_registrar(cx: &mut ExtCtxt,
380381
let register_custom_derive = Ident::from_str("register_custom_derive");
381382
let register_attr_proc_macro = Ident::from_str("register_attr_proc_macro");
382383
let register_bang_proc_macro = Ident::from_str("register_bang_proc_macro");
384+
let crate_kw = Ident::with_empty_ctxt(keywords::Crate.name());
385+
let local_path = |cx: &mut ExtCtxt, sp: Span, name: Ident| {
386+
cx.path(sp.with_ctxt(span.ctxt()), vec![crate_kw, name])
387+
};
383388

384389
let mut stmts = custom_derives.iter().map(|cd| {
385-
let path = cx.path_global(cd.span, vec![cd.function_name]);
390+
let path = local_path(cx, cd.span, cd.function_name);
386391
let trait_name = cx.expr_str(cd.span, cd.trait_name);
387392
let attrs = cx.expr_vec_slice(
388393
span,
@@ -399,7 +404,7 @@ fn mk_registrar(cx: &mut ExtCtxt,
399404

400405
stmts.extend(custom_attrs.iter().map(|ca| {
401406
let name = cx.expr_str(ca.span, ca.function_name.name);
402-
let path = cx.path_global(ca.span, vec![ca.function_name]);
407+
let path = local_path(cx, ca.span, ca.function_name);
403408
let registrar = cx.expr_ident(ca.span, registrar);
404409

405410
let ufcs_path = cx.path(span,
@@ -411,7 +416,7 @@ fn mk_registrar(cx: &mut ExtCtxt,
411416

412417
stmts.extend(custom_macros.iter().map(|cm| {
413418
let name = cx.expr_str(cm.span, cm.function_name.name);
414-
let path = cx.path_global(cm.span, vec![cm.function_name]);
419+
let path = local_path(cx, cm.span, cm.function_name);
415420
let registrar = cx.expr_ident(cm.span, registrar);
416421

417422
let ufcs_path = cx.path(span,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-pass
12+
13+
#![crate_type = "proc-macro"]
14+
#![deny(rust_2018_compatibility)]
15+
#![feature(rust_2018_preview)]
16+
17+
extern crate proc_macro;
18+
19+
use proc_macro::TokenStream;
20+
21+
#[proc_macro_derive(Template, attributes(template))]
22+
pub fn derive_template(input: TokenStream) -> TokenStream {
23+
input
24+
}

0 commit comments

Comments
 (0)