Skip to content

Commit fa48a02

Browse files
committed
Remove SyntaxExtension::DeclMacro
It's a less powerful duplicate of `SyntaxExtension::NormalTT`
1 parent cc17dbb commit fa48a02

File tree

9 files changed

+79
-104
lines changed

9 files changed

+79
-104
lines changed

src/librustc_plugin/registry.rs

+7-24
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc::util::nodemap::FxHashMap;
66

77
use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT};
88
use syntax::ext::base::MacroExpanderFn;
9+
use syntax::ext::hygiene::Transparency;
910
use syntax::symbol::{Symbol, sym};
1011
use syntax::ast;
1112
use syntax::feature_gate::AttributeType;
@@ -84,33 +85,14 @@ impl<'a> Registry<'a> {
8485
/// Register a syntax extension of any kind.
8586
///
8687
/// This is the most general hook into `libsyntax`'s expansion behavior.
87-
pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) {
88+
pub fn register_syntax_extension(&mut self, name: ast::Name, mut extension: SyntaxExtension) {
8889
if name == sym::macro_rules {
8990
panic!("user-defined macros may not be named `macro_rules`");
9091
}
91-
self.syntax_exts.push((name, match extension {
92-
NormalTT {
93-
expander,
94-
def_info: _,
95-
allow_internal_unstable,
96-
allow_internal_unsafe,
97-
local_inner_macros,
98-
unstable_feature,
99-
edition,
100-
} => {
101-
let nid = ast::CRATE_NODE_ID;
102-
NormalTT {
103-
expander,
104-
def_info: Some((nid, self.krate_span)),
105-
allow_internal_unstable,
106-
allow_internal_unsafe,
107-
local_inner_macros,
108-
unstable_feature,
109-
edition,
110-
}
111-
}
112-
_ => extension,
113-
}));
92+
if let NormalTT { def_info: ref mut def_info @ None, .. } = extension {
93+
*def_info = Some((ast::CRATE_NODE_ID, self.krate_span));
94+
}
95+
self.syntax_exts.push((name, extension));
11496
}
11597

11698
/// Register a macro of the usual kind.
@@ -122,6 +104,7 @@ impl<'a> Registry<'a> {
122104
self.register_syntax_extension(Symbol::intern(name), NormalTT {
123105
expander: Box::new(expander),
124106
def_info: None,
107+
transparency: Transparency::SemiTransparent,
125108
allow_internal_unstable: None,
126109
allow_internal_unsafe: false,
127110
local_inner_macros: false,

src/librustc_resolve/macros.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,7 @@ impl<'a> base::Resolver for Resolver<'a> {
242242
fn check_unused_macros(&self) {
243243
for did in self.unused_macros.iter() {
244244
let id_span = match *self.macro_map[did] {
245-
SyntaxExtension::NormalTT { def_info, .. } |
246-
SyntaxExtension::DeclMacro { def_info, .. } => def_info,
245+
SyntaxExtension::NormalTT { def_info, .. } => def_info,
247246
_ => None,
248247
};
249248
if let Some((id, span)) = id_span {

src/librustdoc/passes/collect_intra_doc_links.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ fn macro_resolve(cx: &DocContext<'_>, path_str: &str) -> Option<Res> {
433433
if let Res::Def(DefKind::Macro(MacroKind::ProcMacroStub), _) = res {
434434
// skip proc-macro stubs, they'll cause `get_macro` to crash
435435
} else {
436-
if let SyntaxExtension::DeclMacro { .. } = *resolver.get_macro(res) {
436+
if let SyntaxExtension::NormalTT { .. } = *resolver.get_macro(res) {
437437
return Some(res.map_id(|_| panic!("unexpected id")));
438438
}
439439
}

src/libsyntax/ext/base.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ pub enum SyntaxExtension {
576576
NormalTT {
577577
expander: Box<dyn TTMacroExpander + sync::Sync + sync::Send>,
578578
def_info: Option<(ast::NodeId, Span)>,
579+
transparency: Transparency,
579580
/// Whether the contents of the macro can
580581
/// directly use `#[unstable]` things.
581582
///
@@ -602,21 +603,12 @@ pub enum SyntaxExtension {
602603

603604
/// An attribute-like procedural macro that derives a builtin trait.
604605
BuiltinDerive(Box<dyn MultiItemModifier + sync::Sync + sync::Send>),
605-
606-
/// A declarative macro, e.g., `macro m() {}`.
607-
DeclMacro {
608-
expander: Box<dyn TTMacroExpander + sync::Sync + sync::Send>,
609-
def_info: Option<(ast::NodeId, Span)>,
610-
is_transparent: bool,
611-
edition: Edition,
612-
}
613606
}
614607

615608
impl SyntaxExtension {
616609
/// Returns which kind of macro calls this syntax extension.
617610
pub fn kind(&self) -> MacroKind {
618611
match *self {
619-
SyntaxExtension::DeclMacro { .. } |
620612
SyntaxExtension::NormalTT { .. } |
621613
SyntaxExtension::ProcMacro { .. } =>
622614
MacroKind::Bang,
@@ -632,19 +624,19 @@ impl SyntaxExtension {
632624

633625
pub fn default_transparency(&self) -> Transparency {
634626
match *self {
627+
SyntaxExtension::NormalTT { transparency, .. } => transparency,
635628
SyntaxExtension::ProcMacro { .. } |
636629
SyntaxExtension::AttrProcMacro(..) |
637630
SyntaxExtension::ProcMacroDerive(..) |
638-
SyntaxExtension::DeclMacro { is_transparent: false, .. } => Transparency::Opaque,
639-
SyntaxExtension::DeclMacro { is_transparent: true, .. } => Transparency::Transparent,
640-
_ => Transparency::SemiTransparent,
631+
SyntaxExtension::NonMacroAttr { .. } => Transparency::Opaque,
632+
SyntaxExtension::MultiModifier(..) |
633+
SyntaxExtension::BuiltinDerive(..) => Transparency::SemiTransparent,
641634
}
642635
}
643636

644637
pub fn edition(&self, default_edition: Edition) -> Edition {
645638
match *self {
646639
SyntaxExtension::NormalTT { edition, .. } |
647-
SyntaxExtension::DeclMacro { edition, .. } |
648640
SyntaxExtension::ProcMacro { edition, .. } |
649641
SyntaxExtension::AttrProcMacro(.., edition) |
650642
SyntaxExtension::ProcMacroDerive(.., edition) => edition,

src/libsyntax/ext/expand.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -747,16 +747,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
747747
};
748748

749749
let opt_expanded = match *ext {
750-
DeclMacro { ref expander, def_info, edition, .. } => {
751-
if let Err(dummy_span) = validate_and_set_expn_info(self, def_info.map(|(_, s)| s),
752-
None, false, false, None,
753-
edition) {
754-
dummy_span
755-
} else {
756-
kind.make_from(expander.expand(self.cx, span, mac.node.stream(), None))
757-
}
758-
}
759-
760750
NormalTT {
761751
ref expander,
762752
def_info,
@@ -765,6 +755,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
765755
local_inner_macros,
766756
unstable_feature,
767757
edition,
758+
..
768759
} => {
769760
if let Err(dummy_span) = validate_and_set_expn_info(self, def_info.map(|(_, s)| s),
770761
allow_internal_unstable.clone(),

src/libsyntax/ext/tt/macro_rules.rs

+55-54
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::edition::Edition;
33
use crate::ext::base::{DummyResult, ExtCtxt, MacResult, SyntaxExtension};
44
use crate::ext::base::{NormalTT, TTMacroExpander};
55
use crate::ext::expand::{AstFragment, AstFragmentKind};
6+
use crate::ext::hygiene::Transparency;
67
use crate::ext::tt::macro_parser::{Success, Error, Failure};
78
use crate::ext::tt::macro_parser::{MatchedSeq, MatchedNonterminal};
89
use crate::ext::tt::macro_parser::{parse, parse_failure_msg};
@@ -375,65 +376,65 @@ pub fn compile(
375376
valid,
376377
});
377378

378-
if body.legacy {
379-
let allow_internal_unstable = attr::find_by_name(&def.attrs, sym::allow_internal_unstable)
380-
.map(|attr| attr
381-
.meta_item_list()
382-
.map(|list| list.iter()
383-
.filter_map(|it| {
384-
let name = it.ident().map(|ident| ident.name);
385-
if name.is_none() {
386-
sess.span_diagnostic.span_err(it.span(),
387-
"allow internal unstable expects feature names")
388-
}
389-
name
390-
})
391-
.collect::<Vec<Symbol>>().into()
392-
)
393-
.unwrap_or_else(|| {
394-
sess.span_diagnostic.span_warn(
395-
attr.span, "allow_internal_unstable expects list of feature names. In the \
396-
future this will become a hard error. Please use `allow_internal_unstable(\
397-
foo, bar)` to only allow the `foo` and `bar` features",
398-
);
399-
vec![sym::allow_internal_unstable_backcompat_hack].into()
379+
let transparency = if attr::contains_name(&def.attrs, sym::rustc_transparent_macro) {
380+
Transparency::Transparent
381+
} else if body.legacy {
382+
Transparency::SemiTransparent
383+
} else {
384+
Transparency::Opaque
385+
};
386+
387+
let allow_internal_unstable = attr::find_by_name(&def.attrs, sym::allow_internal_unstable)
388+
.map(|attr| attr
389+
.meta_item_list()
390+
.map(|list| list.iter()
391+
.filter_map(|it| {
392+
let name = it.ident().map(|ident| ident.name);
393+
if name.is_none() {
394+
sess.span_diagnostic.span_err(it.span(),
395+
"allow internal unstable expects feature names")
396+
}
397+
name
400398
})
401-
);
402-
let allow_internal_unsafe = attr::contains_name(&def.attrs, sym::allow_internal_unsafe);
403-
let mut local_inner_macros = false;
404-
if let Some(macro_export) = attr::find_by_name(&def.attrs, sym::macro_export) {
405-
if let Some(l) = macro_export.meta_item_list() {
406-
local_inner_macros = attr::list_contains_name(&l, sym::local_inner_macros);
407-
}
408-
}
399+
.collect::<Vec<Symbol>>().into()
400+
)
401+
.unwrap_or_else(|| {
402+
sess.span_diagnostic.span_warn(
403+
attr.span, "allow_internal_unstable expects list of feature names. In the \
404+
future this will become a hard error. Please use `allow_internal_unstable(\
405+
foo, bar)` to only allow the `foo` and `bar` features",
406+
);
407+
vec![sym::allow_internal_unstable_backcompat_hack].into()
408+
})
409+
);
409410

410-
let unstable_feature = attr::find_stability(&sess,
411-
&def.attrs, def.span).and_then(|stability| {
412-
if let attr::StabilityLevel::Unstable { issue, .. } = stability.level {
413-
Some((stability.feature, issue))
414-
} else {
415-
None
416-
}
417-
});
418-
419-
NormalTT {
420-
expander,
421-
def_info: Some((def.id, def.span)),
422-
allow_internal_unstable,
423-
allow_internal_unsafe,
424-
local_inner_macros,
425-
unstable_feature,
426-
edition,
411+
let allow_internal_unsafe = attr::contains_name(&def.attrs, sym::allow_internal_unsafe);
412+
413+
let mut local_inner_macros = false;
414+
if let Some(macro_export) = attr::find_by_name(&def.attrs, sym::macro_export) {
415+
if let Some(l) = macro_export.meta_item_list() {
416+
local_inner_macros = attr::list_contains_name(&l, sym::local_inner_macros);
427417
}
428-
} else {
429-
let is_transparent = attr::contains_name(&def.attrs, sym::rustc_transparent_macro);
418+
}
430419

431-
SyntaxExtension::DeclMacro {
432-
expander,
433-
def_info: Some((def.id, def.span)),
434-
is_transparent,
435-
edition,
420+
let unstable_feature = attr::find_stability(&sess,
421+
&def.attrs, def.span).and_then(|stability| {
422+
if let attr::StabilityLevel::Unstable { issue, .. } = stability.level {
423+
Some((stability.feature, issue))
424+
} else {
425+
None
436426
}
427+
});
428+
429+
NormalTT {
430+
expander,
431+
def_info: Some((def.id, def.span)),
432+
transparency,
433+
allow_internal_unstable,
434+
allow_internal_unsafe,
435+
local_inner_macros,
436+
unstable_feature,
437+
edition,
437438
}
438439
}
439440

src/libsyntax_ext/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub mod proc_macro_impl;
4242
use rustc_data_structures::sync::Lrc;
4343
use syntax::ast;
4444
use syntax::ext::base::{MacroExpanderFn, NormalTT, NamedSyntaxExtension, MultiModifier};
45+
use syntax::ext::hygiene::Transparency;
4546
use syntax::edition::Edition;
4647
use syntax::symbol::{sym, Symbol};
4748

@@ -59,6 +60,7 @@ pub fn register_builtins(resolver: &mut dyn syntax::ext::base::Resolver,
5960
NormalTT {
6061
expander: Box::new($f as MacroExpanderFn),
6162
def_info: None,
63+
transparency: Transparency::SemiTransparent,
6264
allow_internal_unstable: None,
6365
allow_internal_unsafe: false,
6466
local_inner_macros: false,
@@ -102,6 +104,7 @@ pub fn register_builtins(resolver: &mut dyn syntax::ext::base::Resolver,
102104
NormalTT {
103105
expander: Box::new(format::expand_format_args),
104106
def_info: None,
107+
transparency: Transparency::SemiTransparent,
105108
allow_internal_unstable: Some(vec![sym::fmt_internals].into()),
106109
allow_internal_unsafe: false,
107110
local_inner_macros: false,
@@ -112,6 +115,7 @@ pub fn register_builtins(resolver: &mut dyn syntax::ext::base::Resolver,
112115
NormalTT {
113116
expander: Box::new(format::expand_format_args_nl),
114117
def_info: None,
118+
transparency: Transparency::SemiTransparent,
115119
allow_internal_unstable: Some(vec![sym::fmt_internals].into()),
116120
allow_internal_unsafe: false,
117121
local_inner_macros: false,

src/test/run-pass-fulldeps/auxiliary/plugin-args.rs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use syntax::ast;
1313
use syntax::ext::hygiene;
1414
use syntax::ext::build::AstBuilder;
1515
use syntax::ext::base::{TTMacroExpander, ExtCtxt, MacResult, MacEager, NormalTT};
16+
use syntax::ext::hygiene::Transparency;
1617
use syntax::print::pprust;
1718
use syntax::ptr::P;
1819
use syntax::symbol::Symbol;
@@ -43,6 +44,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
4344
NormalTT {
4445
expander: Box::new(Expander { args: args, }),
4546
def_info: None,
47+
transparency: Transparency::SemiTransparent,
4648
allow_internal_unstable: None,
4749
allow_internal_unsafe: false,
4850
local_inner_macros: false,

src/test/ui/macros/nonterminal-matching.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error: no rules expected the token `enum E { }`
22
--> $DIR/nonterminal-matching.rs:19:10
33
|
4+
LL | macro n(a $nt_item b) {
5+
| --------------------- when calling this macro
6+
...
47
LL | n!(a $nt_item b);
58
| ^^^^^^^^ no rules expected this token in macro call
69
...

0 commit comments

Comments
 (0)