Skip to content

Commit

Permalink
Rollup merge of rust-lang#62258 - petrochenkov:idclean, r=Centril
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Centril authored Jul 3, 2019
2 parents fbc771d + d0dc41a commit ece67e4
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 230 deletions.
5 changes: 1 addition & 4 deletions src/librustc_plugin/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc::util::nodemap::FxHashMap;

use syntax::ext::base::{SyntaxExtension, SyntaxExtensionKind, NamedSyntaxExtension};
use syntax::ext::base::MacroExpanderFn;
use syntax::symbol::{Symbol, sym};
use syntax::symbol::Symbol;
use syntax::ast;
use syntax::feature_gate::AttributeType;
use syntax_pos::Span;
Expand Down Expand Up @@ -85,9 +85,6 @@ impl<'a> Registry<'a> {
///
/// This is the most general hook into `libsyntax`'s expansion behavior.
pub fn register_syntax_extension(&mut self, name: ast::Name, mut extension: SyntaxExtension) {
if name == sym::macro_rules {
panic!("user-defined macros may not be named `macro_rules`");
}
if extension.def_info.is_none() {
extension.def_info = Some((ast::CRATE_NODE_ID, self.krate_span));
}
Expand Down
3 changes: 0 additions & 3 deletions src/librustc_resolve/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1109,9 +1109,6 @@ impl<'a> Resolver<'a> {
current_legacy_scope: &mut LegacyScope<'a>) {
self.local_macro_def_scopes.insert(item.id, self.current_module);
let ident = item.ident;
if ident.name == sym::macro_rules {
self.session.span_err(item.span, "user-defined macros may not be named `macro_rules`");
}

let def_id = self.definitions.local_def_id(item.id);
let ext = Lrc::new(macro_rules::compile(&self.session.parse_sess,
Expand Down
45 changes: 12 additions & 33 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ use crate::parse::{DirectoryOwnership, PResult, ParseSess};
use crate::parse::token;
use crate::parse::parser::Parser;
use crate::ptr::P;
use crate::symbol::Symbol;
use crate::symbol::{kw, sym};
use crate::symbol::{sym, Symbol};
use crate::tokenstream::{TokenStream, TokenTree};
use crate::visit::{self, Visitor};
use crate::util::map_in_place::MapInPlace;
Expand Down Expand Up @@ -197,7 +196,6 @@ pub struct Invocation {
pub enum InvocationKind {
Bang {
mac: ast::Mac,
ident: Option<Ident>,
span: Span,
},
Attr {
Expand Down Expand Up @@ -664,13 +662,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
ext: &SyntaxExtension)
-> Option<AstFragment> {
let kind = invoc.fragment_kind;
let (mac, ident, span) = match invoc.kind {
InvocationKind::Bang { mac, ident, span } => (mac, ident, span),
let (mac, span) = match invoc.kind {
InvocationKind::Bang { mac, span } => (mac, span),
_ => unreachable!(),
};
let path = &mac.node.path;

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

if ident.name != kw::Invalid {
let msg = format!("macro {}! expects no ident argument, given '{}'", path, ident);
this.cx.span_err(path.span, &msg);
this.cx.trace_macros_diag();
return Err(kind.dummy(span));
}
Ok(())
};

Expand Down Expand Up @@ -729,19 +720,11 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
}

SyntaxExtensionKind::Bang(expander) => {
if ident.name != kw::Invalid {
let msg =
format!("macro {}! expects no ident argument, given '{}'", path, ident);
self.cx.span_err(path.span, &msg);
self.cx.trace_macros_diag();
kind.dummy(span)
} else {
self.gate_proc_macro_expansion_kind(span, kind);
let tok_result = expander.expand(self.cx, span, mac.node.stream());
let result = self.parse_ast_fragment(tok_result, kind, path, span);
self.gate_proc_macro_expansion(span, &result);
result
}
self.gate_proc_macro_expansion_kind(span, kind);
let tok_result = expander.expand(self.cx, span, mac.node.stream());
let result = self.parse_ast_fragment(tok_result, kind, path, span);
self.gate_proc_macro_expansion(span, &result);
result
}
};

Expand Down Expand Up @@ -944,7 +927,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
}

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

fn collect_attr(&mut self,
Expand Down Expand Up @@ -1179,13 +1162,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
ast::ItemKind::Mac(..) => {
self.check_attributes(&item.attrs);
item.and_then(|item| match item.node {
ItemKind::Mac(mac) => {
self.collect(AstFragmentKind::Items, InvocationKind::Bang {
mac,
ident: Some(item.ident),
span: item.span,
}).make_items()
}
ItemKind::Mac(mac) => self.collect(
AstFragmentKind::Items, InvocationKind::Bang { mac, span: item.span }
).make_items(),
_ => unreachable!(),
})
}
Expand Down
Loading

0 comments on commit ece67e4

Please sign in to comment.