Skip to content

Commit 6cc1481

Browse files
committed
expansion: Give names to some fields of SyntaxExtension
1 parent 5150b98 commit 6cc1481

File tree

7 files changed

+44
-34
lines changed

7 files changed

+44
-34
lines changed

src/librustc_metadata/creader.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -569,9 +569,11 @@ impl<'a> CrateLoader<'a> {
569569
fn register_bang_proc_macro(&mut self,
570570
name: &str,
571571
expand: fn(TokenStream) -> TokenStream) {
572-
let expand = SyntaxExtension::ProcMacro(
573-
Box::new(BangProcMacro { inner: expand }), false, self.edition
574-
);
572+
let expand = SyntaxExtension::ProcMacro {
573+
expander: Box::new(BangProcMacro { inner: expand }),
574+
allow_internal_unstable: false,
575+
edition: self.edition,
576+
};
575577
self.extensions.push((Symbol::intern(name), Lrc::new(expand)));
576578
}
577579
}

src/librustc_metadata/cstore_impl.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,11 @@ 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),
522-
true, data.root.edition);
521+
let ext = SyntaxExtension::ProcMacro {
522+
expander: Box::new(::proc_macro::__internal::Quoter),
523+
allow_internal_unstable: true,
524+
edition: data.root.edition,
525+
};
523526
return LoadedMacro::ProcMacro(Lrc::new(ext));
524527
}
525528

src/librustc_resolve/macros.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ impl<'a> base::Resolver for Resolver<'a> {
350350
fn check_unused_macros(&self) {
351351
for did in self.unused_macros.iter() {
352352
let id_span = match *self.macro_map[did] {
353-
SyntaxExtension::NormalTT { def_info, .. } => def_info,
354-
SyntaxExtension::DeclMacro(.., osp, _) => osp,
353+
SyntaxExtension::NormalTT { def_info, .. } |
354+
SyntaxExtension::DeclMacro { def_info, .. } => def_info,
355355
_ => None,
356356
};
357357
if let Some((id, span)) = id_span {
@@ -841,8 +841,6 @@ impl<'a> Resolver<'a> {
841841
/// Error if `ext` is a Macros 1.1 procedural macro being imported by `#[macro_use]`
842842
fn err_if_macro_use_proc_macro(&mut self, name: Name, use_span: Span,
843843
binding: &NameBinding<'a>) {
844-
use self::SyntaxExtension::*;
845-
846844
let krate = binding.def().def_id().krate;
847845

848846
// Plugin-based syntax extensions are exempt from this check
@@ -852,15 +850,16 @@ impl<'a> Resolver<'a> {
852850

853851
match *ext {
854852
// If `ext` is a procedural macro, check if we've already warned about it
855-
AttrProcMacro(..) | ProcMacro(..) =>
853+
SyntaxExtension::AttrProcMacro(..) | SyntaxExtension::ProcMacro { .. } =>
856854
if !self.warned_proc_macros.insert(name) { return; },
857855
_ => return,
858856
}
859857

860858
let warn_msg = match *ext {
861-
AttrProcMacro(..) => "attribute procedural macros cannot be \
862-
imported with `#[macro_use]`",
863-
ProcMacro(..) => "procedural macros cannot be imported with `#[macro_use]`",
859+
SyntaxExtension::AttrProcMacro(..) =>
860+
"attribute procedural macros cannot be imported with `#[macro_use]`",
861+
SyntaxExtension::ProcMacro { .. } =>
862+
"procedural macros cannot be imported with `#[macro_use]`",
864863
_ => return,
865864
};
866865

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,7 @@ fn macro_resolve(cx: &DocContext, path_str: &str) -> Option<Def> {
12191219
let res = resolver
12201220
.resolve_macro_to_def_inner(mark, &path, MacroKind::Bang, false);
12211221
if let Ok(def) = res {
1222-
if let SyntaxExtension::DeclMacro(..) = *resolver.get_macro(def) {
1222+
if let SyntaxExtension::DeclMacro { .. } = *resolver.get_macro(def) {
12231223
Some(def)
12241224
} else {
12251225
None

src/libsyntax/ext/base.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -597,11 +597,11 @@ pub enum SyntaxExtension {
597597
MultiModifier(Box<MultiItemModifier + sync::Sync + sync::Send>),
598598

599599
/// A function-like procedural macro. TokenStream -> TokenStream.
600-
ProcMacro(
601-
/* expander: */ Box<ProcMacro + sync::Sync + sync::Send>,
602-
/* allow_internal_unstable: */ bool,
603-
/* edition: */ Edition,
604-
),
600+
ProcMacro {
601+
expander: Box<ProcMacro + sync::Sync + sync::Send>,
602+
allow_internal_unstable: bool,
603+
edition: Edition,
604+
},
605605

606606
/// An attribute-like procedural macro. TokenStream, TokenStream -> TokenStream.
607607
/// The first TokenSteam is the attribute, the second is the annotated item.
@@ -643,19 +643,21 @@ pub enum SyntaxExtension {
643643
BuiltinDerive(BuiltinDeriveFn),
644644

645645
/// A declarative macro, e.g. `macro m() {}`.
646-
///
647-
/// The second element is the definition site span.
648-
DeclMacro(Box<TTMacroExpander + sync::Sync + sync::Send>, Option<(ast::NodeId, Span)>, Edition),
646+
DeclMacro {
647+
expander: Box<TTMacroExpander + sync::Sync + sync::Send>,
648+
def_info: Option<(ast::NodeId, Span)>,
649+
edition: Edition,
650+
}
649651
}
650652

651653
impl SyntaxExtension {
652654
/// Return which kind of macro calls this syntax extension.
653655
pub fn kind(&self) -> MacroKind {
654656
match *self {
655-
SyntaxExtension::DeclMacro(..) |
657+
SyntaxExtension::DeclMacro { .. } |
656658
SyntaxExtension::NormalTT { .. } |
657659
SyntaxExtension::IdentTT(..) |
658-
SyntaxExtension::ProcMacro(..) =>
660+
SyntaxExtension::ProcMacro { .. } =>
659661
MacroKind::Bang,
660662
SyntaxExtension::MultiDecorator(..) |
661663
SyntaxExtension::MultiModifier(..) |
@@ -669,8 +671,8 @@ impl SyntaxExtension {
669671

670672
pub fn is_modern(&self) -> bool {
671673
match *self {
672-
SyntaxExtension::DeclMacro(..) |
673-
SyntaxExtension::ProcMacro(..) |
674+
SyntaxExtension::DeclMacro { .. } |
675+
SyntaxExtension::ProcMacro { .. } |
674676
SyntaxExtension::AttrProcMacro(..) |
675677
SyntaxExtension::ProcMacroDerive(..) => true,
676678
_ => false,
@@ -680,8 +682,8 @@ impl SyntaxExtension {
680682
pub fn edition(&self) -> Edition {
681683
match *self {
682684
SyntaxExtension::NormalTT { edition, .. } |
683-
SyntaxExtension::DeclMacro(.., edition) |
684-
SyntaxExtension::ProcMacro(.., edition) |
685+
SyntaxExtension::DeclMacro { edition, .. } |
686+
SyntaxExtension::ProcMacro { edition, .. } |
685687
SyntaxExtension::AttrProcMacro(.., edition) |
686688
SyntaxExtension::ProcMacroDerive(.., edition) => edition,
687689
// Unstable legacy stuff

src/libsyntax/ext/expand.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -735,13 +735,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
735735
};
736736

737737
let opt_expanded = match *ext {
738-
DeclMacro(ref expand, def_span, edition) => {
739-
if let Err(dummy_span) = validate_and_set_expn_info(self, def_span.map(|(_, s)| s),
738+
DeclMacro { ref expander, def_info, edition } => {
739+
if let Err(dummy_span) = validate_and_set_expn_info(self, def_info.map(|(_, s)| s),
740740
false, false, None,
741741
edition) {
742742
dummy_span
743743
} else {
744-
kind.make_from(expand.expand(self.cx, span, mac.node.stream()))
744+
kind.make_from(expander.expand(self.cx, span, mac.node.stream()))
745745
}
746746
}
747747

@@ -798,7 +798,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
798798
kind.dummy(span)
799799
}
800800

801-
ProcMacro(ref expandfun, allow_internal_unstable, edition) => {
801+
SyntaxExtension::ProcMacro { ref expander, allow_internal_unstable, edition } => {
802802
if ident.name != keywords::Invalid.name() {
803803
let msg =
804804
format!("macro {}! expects no ident argument, given '{}'", path, ident);
@@ -819,7 +819,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
819819
edition,
820820
});
821821

822-
let tok_result = expandfun.expand(self.cx, span, mac.node.stream());
822+
let tok_result = expander.expand(self.cx, span, mac.node.stream());
823823
let result = self.parse_ast_fragment(tok_result, kind, path, span);
824824
self.gate_proc_macro_expansion(span, &result);
825825
result

src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,11 @@ pub fn compile(sess: &ParseSess, features: &Features, def: &ast::Item, edition:
305305
edition,
306306
}
307307
} else {
308-
SyntaxExtension::DeclMacro(expander, Some((def.id, def.span)), edition)
308+
SyntaxExtension::DeclMacro {
309+
expander,
310+
def_info: Some((def.id, def.span)),
311+
edition,
312+
}
309313
}
310314
}
311315

0 commit comments

Comments
 (0)