Skip to content

Commit 09856c8

Browse files
committed
expansion: Give names to some fields of SyntaxExtension
1 parent 1328bde commit 09856c8

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 {
@@ -848,8 +848,6 @@ impl<'a> Resolver<'a> {
848848
/// Error if `ext` is a Macros 1.1 procedural macro being imported by `#[macro_use]`
849849
fn err_if_macro_use_proc_macro(&mut self, name: Name, use_span: Span,
850850
binding: &NameBinding<'a>) {
851-
use self::SyntaxExtension::*;
852-
853851
let krate = binding.def().def_id().krate;
854852

855853
// Plugin-based syntax extensions are exempt from this check
@@ -859,15 +857,16 @@ impl<'a> Resolver<'a> {
859857

860858
match *ext {
861859
// If `ext` is a procedural macro, check if we've already warned about it
862-
AttrProcMacro(..) | ProcMacro(..) =>
860+
SyntaxExtension::AttrProcMacro(..) | SyntaxExtension::ProcMacro { .. } =>
863861
if !self.warned_proc_macros.insert(name) { return; },
864862
_ => return,
865863
}
866864

867865
let warn_msg = match *ext {
868-
AttrProcMacro(..) => "attribute procedural macros cannot be \
869-
imported with `#[macro_use]`",
870-
ProcMacro(..) => "procedural macros cannot be imported with `#[macro_use]`",
866+
SyntaxExtension::AttrProcMacro(..) =>
867+
"attribute procedural macros cannot be imported with `#[macro_use]`",
868+
SyntaxExtension::ProcMacro { .. } =>
869+
"procedural macros cannot be imported with `#[macro_use]`",
871870
_ => return,
872871
};
873872

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.
@@ -646,19 +646,21 @@ pub enum SyntaxExtension {
646646
BuiltinDerive(BuiltinDeriveFn),
647647

648648
/// A declarative macro, e.g. `macro m() {}`.
649-
///
650-
/// The second element is the definition site span.
651-
DeclMacro(Box<TTMacroExpander + sync::Sync + sync::Send>, Option<(ast::NodeId, Span)>, Edition),
649+
DeclMacro {
650+
expander: Box<TTMacroExpander + sync::Sync + sync::Send>,
651+
def_info: Option<(ast::NodeId, Span)>,
652+
edition: Edition,
653+
}
652654
}
653655

654656
impl SyntaxExtension {
655657
/// Return which kind of macro calls this syntax extension.
656658
pub fn kind(&self) -> MacroKind {
657659
match *self {
658-
SyntaxExtension::DeclMacro(..) |
660+
SyntaxExtension::DeclMacro { .. } |
659661
SyntaxExtension::NormalTT { .. } |
660662
SyntaxExtension::IdentTT(..) |
661-
SyntaxExtension::ProcMacro(..) =>
663+
SyntaxExtension::ProcMacro { .. } =>
662664
MacroKind::Bang,
663665
SyntaxExtension::MultiDecorator(..) |
664666
SyntaxExtension::MultiModifier(..) |
@@ -672,8 +674,8 @@ impl SyntaxExtension {
672674

673675
pub fn is_modern(&self) -> bool {
674676
match *self {
675-
SyntaxExtension::DeclMacro(..) |
676-
SyntaxExtension::ProcMacro(..) |
677+
SyntaxExtension::DeclMacro { .. } |
678+
SyntaxExtension::ProcMacro { .. } |
677679
SyntaxExtension::AttrProcMacro(..) |
678680
SyntaxExtension::ProcMacroDerive(..) => true,
679681
_ => false,
@@ -683,8 +685,8 @@ impl SyntaxExtension {
683685
pub fn edition(&self) -> Edition {
684686
match *self {
685687
SyntaxExtension::NormalTT { edition, .. } |
686-
SyntaxExtension::DeclMacro(.., edition) |
687-
SyntaxExtension::ProcMacro(.., edition) |
688+
SyntaxExtension::DeclMacro { edition, .. } |
689+
SyntaxExtension::ProcMacro { edition, .. } |
688690
SyntaxExtension::AttrProcMacro(.., edition) |
689691
SyntaxExtension::ProcMacroDerive(.., edition) => edition,
690692
// Unstable legacy stuff

src/libsyntax/ext/expand.rs

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

740740
let opt_expanded = match *ext {
741-
DeclMacro(ref expand, def_span, edition) => {
742-
if let Err(dummy_span) = validate_and_set_expn_info(self, def_span.map(|(_, s)| s),
741+
DeclMacro { ref expander, def_info, edition } => {
742+
if let Err(dummy_span) = validate_and_set_expn_info(self, def_info.map(|(_, s)| s),
743743
false, false, false, None,
744744
edition) {
745745
dummy_span
746746
} else {
747-
kind.make_from(expand.expand(self.cx, span, mac.node.stream()))
747+
kind.make_from(expander.expand(self.cx, span, mac.node.stream()))
748748
}
749749
}
750750

@@ -804,7 +804,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
804804
kind.dummy(span)
805805
}
806806

807-
ProcMacro(ref expandfun, allow_internal_unstable, edition) => {
807+
SyntaxExtension::ProcMacro { ref expander, allow_internal_unstable, edition } => {
808808
if ident.name != keywords::Invalid.name() {
809809
let msg =
810810
format!("macro {}! expects no ident argument, given '{}'", path, ident);
@@ -826,7 +826,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
826826
edition,
827827
});
828828

829-
let tok_result = expandfun.expand(self.cx, span, mac.node.stream());
829+
let tok_result = expander.expand(self.cx, span, mac.node.stream());
830830
let result = self.parse_ast_fragment(tok_result, kind, path, span);
831831
self.gate_proc_macro_expansion(span, &result);
832832
result

src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,11 @@ pub fn compile(sess: &ParseSess, features: &Features, def: &ast::Item, edition:
312312
edition,
313313
}
314314
} else {
315-
SyntaxExtension::DeclMacro(expander, Some((def.id, def.span)), edition)
315+
SyntaxExtension::DeclMacro {
316+
expander,
317+
def_info: Some((def.id, def.span)),
318+
edition,
319+
}
316320
}
317321
}
318322

0 commit comments

Comments
 (0)