Skip to content

Commit f4cbc23

Browse files
committed
Pass crate editions to macro expansions, update tests
1 parent 76bf345 commit f4cbc23

16 files changed

+107
-65
lines changed

src/librustc_metadata/creader.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use std::{cmp, fs};
3535

3636
use syntax::ast;
3737
use syntax::attr;
38+
use syntax::edition::Edition;
3839
use syntax::ext::base::SyntaxExtension;
3940
use syntax::symbol::Symbol;
4041
use syntax::visit;
@@ -535,7 +536,10 @@ impl<'a> CrateLoader<'a> {
535536
mem::transmute::<*mut u8, fn(&mut Registry)>(sym)
536537
};
537538

538-
struct MyRegistrar(Vec<(ast::Name, Lrc<SyntaxExtension>)>);
539+
struct MyRegistrar {
540+
extensions: Vec<(ast::Name, Lrc<SyntaxExtension>)>,
541+
edition: Edition,
542+
}
539543

540544
impl Registry for MyRegistrar {
541545
fn register_custom_derive(&mut self,
@@ -544,36 +548,38 @@ impl<'a> CrateLoader<'a> {
544548
attributes: &[&'static str]) {
545549
let attrs = attributes.iter().cloned().map(Symbol::intern).collect::<Vec<_>>();
546550
let derive = ProcMacroDerive::new(expand, attrs.clone());
547-
let derive = SyntaxExtension::ProcMacroDerive(Box::new(derive), attrs);
548-
self.0.push((Symbol::intern(trait_name), Lrc::new(derive)));
551+
let derive = SyntaxExtension::ProcMacroDerive(
552+
Box::new(derive), attrs, self.edition
553+
);
554+
self.extensions.push((Symbol::intern(trait_name), Lrc::new(derive)));
549555
}
550556

551557
fn register_attr_proc_macro(&mut self,
552558
name: &str,
553559
expand: fn(TokenStream, TokenStream) -> TokenStream) {
554560
let expand = SyntaxExtension::AttrProcMacro(
555-
Box::new(AttrProcMacro { inner: expand })
561+
Box::new(AttrProcMacro { inner: expand }), self.edition
556562
);
557-
self.0.push((Symbol::intern(name), Lrc::new(expand)));
563+
self.extensions.push((Symbol::intern(name), Lrc::new(expand)));
558564
}
559565

560566
fn register_bang_proc_macro(&mut self,
561567
name: &str,
562568
expand: fn(TokenStream) -> TokenStream) {
563569
let expand = SyntaxExtension::ProcMacro(
564-
Box::new(BangProcMacro { inner: expand })
570+
Box::new(BangProcMacro { inner: expand }), self.edition
565571
);
566-
self.0.push((Symbol::intern(name), Lrc::new(expand)));
572+
self.extensions.push((Symbol::intern(name), Lrc::new(expand)));
567573
}
568574
}
569575

570-
let mut my_registrar = MyRegistrar(Vec::new());
576+
let mut my_registrar = MyRegistrar { extensions: Vec::new(), edition: root.edition };
571577
registrar(&mut my_registrar);
572578

573579
// Intentionally leak the dynamic library. We can't ever unload it
574580
// since the library can make things that will live arbitrarily long.
575581
mem::forget(lib);
576-
my_registrar.0
582+
my_registrar.extensions
577583
}
578584

579585
/// Look for a plugin registrar. Returns library path, crate

src/librustc_metadata/cstore_impl.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,8 @@ impl CrateStore for cstore::CStore {
518518
return LoadedMacro::ProcMacro(proc_macros[id.index.to_proc_macro_index()].1.clone());
519519
} else if data.name == "proc_macro" &&
520520
self.get_crate_data(id.krate).item_name(id.index) == "quote" {
521-
let ext = SyntaxExtension::ProcMacro(Box::new(::proc_macro::__internal::Quoter));
521+
let ext = SyntaxExtension::ProcMacro(Box::new(::proc_macro::__internal::Quoter),
522+
data.edition());
522523
return LoadedMacro::ProcMacro(Lrc::new(ext));
523524
}
524525

src/librustc_plugin/registry.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc::session::Session;
1515

1616
use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT, IdentTT};
1717
use syntax::ext::base::MacroExpanderFn;
18+
use syntax::ext::hygiene;
1819
use syntax::symbol::Symbol;
1920
use syntax::ast;
2021
use syntax::feature_gate::AttributeType;
@@ -107,15 +108,17 @@ impl<'a> Registry<'a> {
107108
def_info: _,
108109
allow_internal_unstable,
109110
allow_internal_unsafe,
110-
unstable_feature
111+
unstable_feature,
112+
edition,
111113
} => {
112114
let nid = ast::CRATE_NODE_ID;
113115
NormalTT {
114116
expander,
115117
def_info: Some((nid, self.krate_span)),
116118
allow_internal_unstable,
117119
allow_internal_unsafe,
118-
unstable_feature
120+
unstable_feature,
121+
edition,
119122
}
120123
}
121124
IdentTT(ext, _, allow_internal_unstable) => {
@@ -150,6 +153,7 @@ impl<'a> Registry<'a> {
150153
allow_internal_unstable: false,
151154
allow_internal_unsafe: false,
152155
unstable_feature: None,
156+
edition: hygiene::default_edition(),
153157
});
154158
}
155159

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,8 @@ impl<'a> Resolver<'a> {
588588

589589
let ext = Lrc::new(macro_rules::compile(&self.session.parse_sess,
590590
&self.session.features_untracked(),
591-
&macro_def));
591+
&macro_def,
592+
self.cstore.crate_edition_untracked(def_id.krate)));
592593
self.macro_map.insert(def_id, ext.clone());
593594
ext
594595
}

src/librustc_resolve/macros.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use syntax::errors::DiagnosticBuilder;
2424
use syntax::ext::base::{self, Annotatable, Determinacy, MultiModifier, MultiDecorator};
2525
use syntax::ext::base::{MacroKind, SyntaxExtension, Resolver as SyntaxResolver};
2626
use syntax::ext::expand::{Expansion, ExpansionKind, Invocation, InvocationKind, find_attr_invoc};
27-
use syntax::ext::hygiene::{Mark, MarkKind};
27+
use syntax::ext::hygiene::{self, Mark, MarkKind};
2828
use syntax::ext::placeholders::placeholder;
2929
use syntax::ext::tt::macro_rules;
3030
use syntax::feature_gate::{self, emit_feature_err, GateIssue};
@@ -328,7 +328,7 @@ impl<'a> base::Resolver for Resolver<'a> {
328328
for did in self.unused_macros.iter() {
329329
let id_span = match *self.macro_map[did] {
330330
SyntaxExtension::NormalTT { def_info, .. } => def_info,
331-
SyntaxExtension::DeclMacro(.., osp) => osp,
331+
SyntaxExtension::DeclMacro(.., osp, _) => osp,
332332
_ => None,
333333
};
334334
if let Some((id, span)) = id_span {
@@ -371,7 +371,7 @@ impl<'a> Resolver<'a> {
371371
};
372372
for path in traits {
373373
match self.resolve_macro(scope, path, MacroKind::Derive, force) {
374-
Ok(ext) => if let SyntaxExtension::ProcMacroDerive(_, ref inert_attrs) = *ext {
374+
Ok(ext) => if let SyntaxExtension::ProcMacroDerive(_, ref inert_attrs, _) = *ext {
375375
if inert_attrs.contains(&attr_name) {
376376
// FIXME(jseyfried) Avoid `mem::replace` here.
377377
let dummy_item = placeholder(ExpansionKind::Items, ast::DUMMY_NODE_ID)
@@ -755,7 +755,7 @@ impl<'a> Resolver<'a> {
755755
let def_id = self.definitions.local_def_id(item.id);
756756
let ext = Lrc::new(macro_rules::compile(&self.session.parse_sess,
757757
&self.session.features_untracked(),
758-
item));
758+
item, hygiene::default_edition()));
759759
self.macro_map.insert(def_id, ext);
760760

761761
let def = match item.node { ast::ItemKind::MacroDef(ref def) => def, _ => unreachable!() };
@@ -803,14 +803,15 @@ impl<'a> Resolver<'a> {
803803

804804
match *ext {
805805
// If `ext` is a procedural macro, check if we've already warned about it
806-
AttrProcMacro(_) | ProcMacro(_) => if !self.warned_proc_macros.insert(name) { return; },
806+
AttrProcMacro(..) | ProcMacro(..) =>
807+
if !self.warned_proc_macros.insert(name) { return; },
807808
_ => return,
808809
}
809810

810811
let warn_msg = match *ext {
811-
AttrProcMacro(_) => "attribute procedural macros cannot be \
812-
imported with `#[macro_use]`",
813-
ProcMacro(_) => "procedural macros cannot be imported with `#[macro_use]`",
812+
AttrProcMacro(..) => "attribute procedural macros cannot be \
813+
imported with `#[macro_use]`",
814+
ProcMacro(..) => "procedural macros cannot be imported with `#[macro_use]`",
814815
_ => return,
815816
};
816817

src/libsyntax/ext/base.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ use ast::{self, Attribute, Name, PatKind, MetaItem};
1414
use attr::HasAttrs;
1515
use codemap::{self, CodeMap, Spanned, respan};
1616
use syntax_pos::{Span, MultiSpan, DUMMY_SP};
17+
use edition::Edition;
1718
use errors::{DiagnosticBuilder, DiagnosticId};
1819
use ext::expand::{self, Expansion, Invocation};
19-
use ext::hygiene::{Mark, SyntaxContext};
20+
use ext::hygiene::{self, Mark, SyntaxContext};
2021
use fold::{self, Folder};
2122
use parse::{self, parser, DirectoryOwnership};
2223
use parse::token;
@@ -586,13 +587,13 @@ pub enum SyntaxExtension {
586587
MultiModifier(Box<MultiItemModifier + sync::Sync + sync::Send>),
587588

588589
/// A function-like procedural macro. TokenStream -> TokenStream.
589-
ProcMacro(Box<ProcMacro + sync::Sync + sync::Send>),
590+
ProcMacro(Box<ProcMacro + sync::Sync + sync::Send>, Edition),
590591

591592
/// An attribute-like procedural macro. TokenStream, TokenStream -> TokenStream.
592593
/// The first TokenSteam is the attribute, the second is the annotated item.
593594
/// Allows modification of the input items and adding new items, similar to
594595
/// MultiModifier, but uses TokenStreams, rather than AST nodes.
595-
AttrProcMacro(Box<AttrProcMacro + sync::Sync + sync::Send>),
596+
AttrProcMacro(Box<AttrProcMacro + sync::Sync + sync::Send>, Edition),
596597

597598
/// A normal, function-like syntax extension.
598599
///
@@ -608,6 +609,8 @@ pub enum SyntaxExtension {
608609
allow_internal_unsafe: bool,
609610
/// The macro's feature name if it is unstable, and the stability feature
610611
unstable_feature: Option<(Symbol, u32)>,
612+
/// Edition of the crate in which the macro is defined
613+
edition: Edition,
611614
},
612615

613616
/// A function-like syntax extension that has an extra ident before
@@ -619,17 +622,16 @@ pub enum SyntaxExtension {
619622
/// The input is the annotated item.
620623
/// Allows generating code to implement a Trait for a given struct
621624
/// or enum item.
622-
ProcMacroDerive(Box<MultiItemModifier +
623-
sync::Sync +
624-
sync::Send>, Vec<Symbol> /* inert attribute names */),
625+
ProcMacroDerive(Box<MultiItemModifier + sync::Sync + sync::Send>,
626+
Vec<Symbol> /* inert attribute names */, Edition),
625627

626628
/// An attribute-like procedural macro that derives a builtin trait.
627629
BuiltinDerive(BuiltinDeriveFn),
628630

629631
/// A declarative macro, e.g. `macro m() {}`.
630632
///
631633
/// The second element is the definition site span.
632-
DeclMacro(Box<TTMacroExpander + sync::Sync + sync::Send>, Option<(ast::NodeId, Span)>),
634+
DeclMacro(Box<TTMacroExpander + sync::Sync + sync::Send>, Option<(ast::NodeId, Span)>, Edition),
633635
}
634636

635637
impl SyntaxExtension {
@@ -660,6 +662,21 @@ impl SyntaxExtension {
660662
_ => false,
661663
}
662664
}
665+
666+
pub fn edition(&self) -> Edition {
667+
match *self {
668+
SyntaxExtension::NormalTT { edition, .. } |
669+
SyntaxExtension::DeclMacro(.., edition) |
670+
SyntaxExtension::ProcMacro(.., edition) |
671+
SyntaxExtension::AttrProcMacro(.., edition) |
672+
SyntaxExtension::ProcMacroDerive(.., edition) => edition,
673+
// Unstable legacy stuff
674+
SyntaxExtension::IdentTT(..) |
675+
SyntaxExtension::MultiDecorator(..) |
676+
SyntaxExtension::MultiModifier(..) |
677+
SyntaxExtension::BuiltinDerive(..) => hygiene::default_edition(),
678+
}
679+
}
663680
}
664681

665682
pub type NamedSyntaxExtension = (Name, SyntaxExtension);

src/libsyntax/ext/expand.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
502502
span: None,
503503
allow_internal_unstable: false,
504504
allow_internal_unsafe: false,
505-
edition: hygiene::default_edition(),
505+
edition: ext.edition(),
506506
}
507507
});
508508

@@ -521,7 +521,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
521521
items.push(item);
522522
Some(kind.expect_from_annotatables(items))
523523
}
524-
AttrProcMacro(ref mac) => {
524+
AttrProcMacro(ref mac, ..) => {
525525
self.gate_proc_macro_attr_item(attr.span, &item);
526526
let item_tok = TokenTree::Token(DUMMY_SP, Token::interpolated(match item {
527527
Annotatable::Item(item) => token::NtItem(item),
@@ -610,7 +610,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
610610
allow_internal_unstable,
611611
allow_internal_unsafe,
612612
// can't infer this type
613-
unstable_feature: Option<(Symbol, u32)>| {
613+
unstable_feature: Option<(Symbol, u32)>,
614+
edition| {
614615

615616
// feature-gate the macro invocation
616617
if let Some((feature, issue)) = unstable_feature {
@@ -643,16 +644,17 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
643644
span: def_site_span,
644645
allow_internal_unstable,
645646
allow_internal_unsafe,
646-
edition: hygiene::default_edition(),
647+
edition,
647648
},
648649
});
649650
Ok(())
650651
};
651652

652653
let opt_expanded = match *ext {
653-
DeclMacro(ref expand, def_span) => {
654+
DeclMacro(ref expand, def_span, edition) => {
654655
if let Err(dummy_span) = validate_and_set_expn_info(self, def_span.map(|(_, s)| s),
655-
false, false, None) {
656+
false, false, None,
657+
edition) {
656658
dummy_span
657659
} else {
658660
kind.make_from(expand.expand(self.cx, span, mac.node.stream()))
@@ -665,11 +667,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
665667
allow_internal_unstable,
666668
allow_internal_unsafe,
667669
unstable_feature,
670+
edition,
668671
} => {
669672
if let Err(dummy_span) = validate_and_set_expn_info(self, def_info.map(|(_, s)| s),
670673
allow_internal_unstable,
671674
allow_internal_unsafe,
672-
unstable_feature) {
675+
unstable_feature,
676+
edition) {
673677
dummy_span
674678
} else {
675679
kind.make_from(expander.expand(self.cx, span, mac.node.stream()))
@@ -712,7 +716,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
712716
kind.dummy(span)
713717
}
714718

715-
ProcMacro(ref expandfun) => {
719+
ProcMacro(ref expandfun, edition) => {
716720
if ident.name != keywords::Invalid.name() {
717721
let msg =
718722
format!("macro {}! expects no ident argument, given '{}'", path, ident);
@@ -731,7 +735,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
731735
// FIXME probably want to follow macro_rules macros here.
732736
allow_internal_unstable: false,
733737
allow_internal_unsafe: false,
734-
edition: hygiene::default_edition(),
738+
edition,
735739
},
736740
});
737741

@@ -806,12 +810,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
806810
span: None,
807811
allow_internal_unstable: false,
808812
allow_internal_unsafe: false,
809-
edition: hygiene::default_edition(),
813+
edition: ext.edition(),
810814
}
811815
};
812816

813817
match *ext {
814-
ProcMacroDerive(ref ext, _) => {
818+
ProcMacroDerive(ref ext, ..) => {
815819
invoc.expansion_data.mark.set_expn_info(expn_info);
816820
let span = span.with_ctxt(self.cx.backtrace());
817821
let dummy = ast::MetaItem { // FIXME(jseyfried) avoid this

src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use {ast, attr};
1212
use syntax_pos::{Span, DUMMY_SP};
13+
use edition::Edition;
1314
use ext::base::{DummyResult, ExtCtxt, MacResult, SyntaxExtension};
1415
use ext::base::{NormalTT, TTMacroExpander};
1516
use ext::expand::{Expansion, ExpansionKind};
@@ -183,7 +184,8 @@ fn generic_extension<'cx>(cx: &'cx mut ExtCtxt,
183184
// Holy self-referential!
184185

185186
/// Converts a `macro_rules!` invocation into a syntax extension.
186-
pub fn compile(sess: &ParseSess, features: &Features, def: &ast::Item) -> SyntaxExtension {
187+
pub fn compile(sess: &ParseSess, features: &Features, def: &ast::Item, edition: Edition)
188+
-> SyntaxExtension {
187189
let lhs_nm = ast::Ident::with_empty_ctxt(Symbol::gensym("lhs"));
188190
let rhs_nm = ast::Ident::with_empty_ctxt(Symbol::gensym("rhs"));
189191

@@ -298,10 +300,11 @@ pub fn compile(sess: &ParseSess, features: &Features, def: &ast::Item) -> Syntax
298300
def_info: Some((def.id, def.span)),
299301
allow_internal_unstable,
300302
allow_internal_unsafe,
301-
unstable_feature
303+
unstable_feature,
304+
edition,
302305
}
303306
} else {
304-
SyntaxExtension::DeclMacro(expander, Some((def.id, def.span)))
307+
SyntaxExtension::DeclMacro(expander, Some((def.id, def.span)), edition)
305308
}
306309
}
307310

0 commit comments

Comments
 (0)