Skip to content

Commit f5e7857

Browse files
authored
Rollup merge of rust-lang#62258 - petrochenkov:idclean, r=Centril
syntax: Unsupport `foo! bar { ... }` macros in the parser Their support in expansion was removed in rust-lang#61606. Also un-reserve `macro_rules` as a macro name, there's no ambiguity between `macro_rules` definitions and macro calls (it also wasn't reserved correctly). cc rust-lang/wg-grammar#51
2 parents 7ec194a + d0dc41a commit f5e7857

File tree

11 files changed

+111
-230
lines changed

11 files changed

+111
-230
lines changed

src/librustc_plugin/registry.rs

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

77
use syntax::ext::base::{SyntaxExtension, SyntaxExtensionKind, NamedSyntaxExtension};
88
use syntax::ext::base::MacroExpanderFn;
9-
use syntax::symbol::{Symbol, sym};
9+
use syntax::symbol::Symbol;
1010
use syntax::ast;
1111
use syntax::feature_gate::AttributeType;
1212
use syntax_pos::Span;
@@ -85,9 +85,6 @@ impl<'a> Registry<'a> {
8585
///
8686
/// This is the most general hook into `libsyntax`'s expansion behavior.
8787
pub fn register_syntax_extension(&mut self, name: ast::Name, mut extension: SyntaxExtension) {
88-
if name == sym::macro_rules {
89-
panic!("user-defined macros may not be named `macro_rules`");
90-
}
9188
if extension.def_info.is_none() {
9289
extension.def_info = Some((ast::CRATE_NODE_ID, self.krate_span));
9390
}

src/librustc_resolve/macros.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1109,9 +1109,6 @@ impl<'a> Resolver<'a> {
11091109
current_legacy_scope: &mut LegacyScope<'a>) {
11101110
self.local_macro_def_scopes.insert(item.id, self.current_module);
11111111
let ident = item.ident;
1112-
if ident.name == sym::macro_rules {
1113-
self.session.span_err(item.span, "user-defined macros may not be named `macro_rules`");
1114-
}
11151112

11161113
let def_id = self.definitions.local_def_id(item.id);
11171114
let ext = Lrc::new(macro_rules::compile(&self.session.parse_sess,

src/libsyntax/ext/expand.rs

+12-33
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use crate::parse::{DirectoryOwnership, PResult, ParseSess};
1313
use crate::parse::token;
1414
use crate::parse::parser::Parser;
1515
use crate::ptr::P;
16-
use crate::symbol::Symbol;
17-
use crate::symbol::{kw, sym};
16+
use crate::symbol::{sym, Symbol};
1817
use crate::tokenstream::{TokenStream, TokenTree};
1918
use crate::visit::{self, Visitor};
2019
use crate::util::map_in_place::MapInPlace;
@@ -197,7 +196,6 @@ pub struct Invocation {
197196
pub enum InvocationKind {
198197
Bang {
199198
mac: ast::Mac,
200-
ident: Option<Ident>,
201199
span: Span,
202200
},
203201
Attr {
@@ -664,13 +662,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
664662
ext: &SyntaxExtension)
665663
-> Option<AstFragment> {
666664
let kind = invoc.fragment_kind;
667-
let (mac, ident, span) = match invoc.kind {
668-
InvocationKind::Bang { mac, ident, span } => (mac, ident, span),
665+
let (mac, span) = match invoc.kind {
666+
InvocationKind::Bang { mac, span } => (mac, span),
669667
_ => unreachable!(),
670668
};
671669
let path = &mac.node.path;
672670

673-
let ident = ident.unwrap_or_else(|| Ident::invalid());
674671
let validate = |this: &mut Self| {
675672
// feature-gate the macro invocation
676673
if let Some((feature, issue)) = ext.unstable_feature {
@@ -690,12 +687,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
690687
}
691688
}
692689

693-
if ident.name != kw::Invalid {
694-
let msg = format!("macro {}! expects no ident argument, given '{}'", path, ident);
695-
this.cx.span_err(path.span, &msg);
696-
this.cx.trace_macros_diag();
697-
return Err(kind.dummy(span));
698-
}
699690
Ok(())
700691
};
701692

@@ -729,19 +720,11 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
729720
}
730721

731722
SyntaxExtensionKind::Bang(expander) => {
732-
if ident.name != kw::Invalid {
733-
let msg =
734-
format!("macro {}! expects no ident argument, given '{}'", path, ident);
735-
self.cx.span_err(path.span, &msg);
736-
self.cx.trace_macros_diag();
737-
kind.dummy(span)
738-
} else {
739-
self.gate_proc_macro_expansion_kind(span, kind);
740-
let tok_result = expander.expand(self.cx, span, mac.node.stream());
741-
let result = self.parse_ast_fragment(tok_result, kind, path, span);
742-
self.gate_proc_macro_expansion(span, &result);
743-
result
744-
}
723+
self.gate_proc_macro_expansion_kind(span, kind);
724+
let tok_result = expander.expand(self.cx, span, mac.node.stream());
725+
let result = self.parse_ast_fragment(tok_result, kind, path, span);
726+
self.gate_proc_macro_expansion(span, &result);
727+
result
745728
}
746729
};
747730

@@ -944,7 +927,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
944927
}
945928

946929
fn collect_bang(&mut self, mac: ast::Mac, span: Span, kind: AstFragmentKind) -> AstFragment {
947-
self.collect(kind, InvocationKind::Bang { mac, ident: None, span })
930+
self.collect(kind, InvocationKind::Bang { mac, span })
948931
}
949932

950933
fn collect_attr(&mut self,
@@ -1179,13 +1162,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
11791162
ast::ItemKind::Mac(..) => {
11801163
self.check_attributes(&item.attrs);
11811164
item.and_then(|item| match item.node {
1182-
ItemKind::Mac(mac) => {
1183-
self.collect(AstFragmentKind::Items, InvocationKind::Bang {
1184-
mac,
1185-
ident: Some(item.ident),
1186-
span: item.span,
1187-
}).make_items()
1188-
}
1165+
ItemKind::Mac(mac) => self.collect(
1166+
AstFragmentKind::Items, InvocationKind::Bang { mac, span: item.span }
1167+
).make_items(),
11891168
_ => unreachable!(),
11901169
})
11911170
}

0 commit comments

Comments
 (0)