Skip to content

Commit

Permalink
Reserve the keyword 'macro'
Browse files Browse the repository at this point in the history
  • Loading branch information
Keegan McAllister committed Jan 6, 2015
1 parent aa69cbd commit c9f0ff3
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 39 deletions.
12 changes: 6 additions & 6 deletions src/doc/reference.md
Expand Up @@ -193,12 +193,12 @@ grammar as double-quoted strings. Other tokens have exact rules given.
| break | const | continue | crate | do |
| else | enum | extern | false | final |
| fn | for | if | impl | in |
| let | loop | match | mod | move |
| mut | offsetof | override | priv | pub |
| pure | ref | return | sizeof | static |
| self | struct | super | true | trait |
| type | typeof | unsafe | unsized | use |
| virtual | where | while | yield |
| let | loop | macro | match | mod |
| move | mut | offsetof | override | priv |
| pub | pure | ref | return | sizeof |
| static | self | struct | super | true |
| trait | type | typeof | unsafe | unsized |
| use | virtual | where | while | yield |


Each of these keywords has special meaning in its grammar, and all of them are
Expand Down
2 changes: 1 addition & 1 deletion src/etc/vim/syntax/rust.vim
Expand Up @@ -56,7 +56,7 @@ syn match rustMacroRepeatCount ".\?[*+]" contained
syn match rustMacroVariable "$\w\+"

" Reserved (but not yet used) keywords {{{2
syn keyword rustReservedKeyword alignof be do offsetof priv pure sizeof typeof unsized yield abstract final override
syn keyword rustReservedKeyword alignof be do offsetof priv pure sizeof typeof unsized yield abstract final override macro

" Built-in types {{{2
syn keyword rustType int uint float char bool u8 u16 u32 u64 f32
Expand Down
14 changes: 7 additions & 7 deletions src/librustc_back/svh.rs
Expand Up @@ -327,11 +327,11 @@ mod svh_visitor {

impl<'a, 'v> Visitor<'v> for StrictVersionHashVisitor<'a> {

fn visit_mac(&mut self, macro: &Mac) {
fn visit_mac(&mut self, mac: &Mac) {
// macro invocations, namely macro_rules definitions,
// *can* appear as items, even in the expanded crate AST.

if macro_name(macro).get() == "macro_rules" {
if macro_name(mac).get() == "macro_rules" {
// Pretty-printing definition to a string strips out
// surface artifacts (currently), such as the span
// information, yielding a content-based hash.
Expand All @@ -341,22 +341,22 @@ mod svh_visitor {
// trees might be faster. Implementing this is far
// easier in short term.
let macro_defn_as_string = pprust::to_string(|pp_state| {
pp_state.print_mac(macro, token::Paren)
pp_state.print_mac(mac, token::Paren)
});
macro_defn_as_string.hash(self.st);
} else {
// It is not possible to observe any kind of macro
// invocation at this stage except `macro_rules!`.
panic!("reached macro somehow: {}",
pprust::to_string(|pp_state| {
pp_state.print_mac(macro, token::Paren)
pp_state.print_mac(mac, token::Paren)
}));
}

visit::walk_mac(self, macro);
visit::walk_mac(self, mac);

fn macro_name(macro: &Mac) -> token::InternedString {
match &macro.node {
fn macro_name(mac: &Mac) -> token::InternedString {
match &mac.node {
&MacInvocTT(ref path, ref _tts, ref _stx_ctxt) => {
let s = path.segments[];
assert_eq!(s.len(), 1);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_driver/pretty.rs
Expand Up @@ -484,8 +484,8 @@ impl fold::Folder for ReplaceBodyWithLoop {

// in general the pretty printer processes unexpanded code, so
// we override the default `fold_mac` method which panics.
fn fold_mac(&mut self, _macro: ast::Mac) -> ast::Mac {
fold::noop_fold_mac(_macro, self)
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
fold::noop_fold_mac(mac, self)
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/libsyntax/ext/expand.rs
Expand Up @@ -986,8 +986,8 @@ impl<'a> Folder for IdentRenamer<'a> {
ctxt: mtwt::apply_renames(self.renames, id.ctxt),
}
}
fn fold_mac(&mut self, macro: ast::Mac) -> ast::Mac {
fold::noop_fold_mac(macro, self)
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
fold::noop_fold_mac(mac, self)
}
}

Expand Down Expand Up @@ -1023,8 +1023,8 @@ impl<'a> Folder for PatIdentRenamer<'a> {
_ => unreachable!()
})
}
fn fold_mac(&mut self, macro: ast::Mac) -> ast::Mac {
fold::noop_fold_mac(macro, self)
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
fold::noop_fold_mac(mac, self)
}
}

Expand Down Expand Up @@ -1286,8 +1286,8 @@ struct MacroExterminator<'a>{
}

impl<'a, 'v> Visitor<'v> for MacroExterminator<'a> {
fn visit_mac(&mut self, macro: &ast::Mac) {
self.sess.span_diagnostic.span_bug(macro.span,
fn visit_mac(&mut self, mac: &ast::Mac) {
self.sess.span_diagnostic.span_bug(mac.span,
"macro exterminator: expected AST \
with no macro invocations");
}
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/feature_gate.rs
Expand Up @@ -165,8 +165,8 @@ struct MacroVisitor<'a> {
}

impl<'a, 'v> Visitor<'v> for MacroVisitor<'a> {
fn visit_mac(&mut self, macro: &ast::Mac) {
let ast::MacInvocTT(ref path, _, _) = macro.node;
fn visit_mac(&mut self, mac: &ast::Mac) {
let ast::MacInvocTT(ref path, _, _) = mac.node;
let id = path.segments.last().unwrap().identifier;

if id == token::str_to_ident("macro_rules") {
Expand Down
8 changes: 4 additions & 4 deletions src/libsyntax/fold.rs
Expand Up @@ -194,13 +194,13 @@ pub trait Folder : Sized {
noop_fold_local(l, self)
}

fn fold_mac(&mut self, _macro: Mac) -> Mac {
fn fold_mac(&mut self, _mac: Mac) -> Mac {
panic!("fold_mac disabled by default");
// NB: see note about macros above.
// if you really want a folder that
// works on macros, use this
// definition in your trait impl:
// fold::noop_fold_mac(_macro, self)
// fold::noop_fold_mac(_mac, self)
}

fn fold_explicit_self(&mut self, es: ExplicitSelf) -> ExplicitSelf {
Expand Down Expand Up @@ -1487,8 +1487,8 @@ mod test {
fn fold_ident(&mut self, _: ast::Ident) -> ast::Ident {
token::str_to_ident("zz")
}
fn fold_mac(&mut self, macro: ast::Mac) -> ast::Mac {
fold::noop_fold_mac(macro, self)
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
fold::noop_fold_mac(mac, self)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -3881,13 +3881,13 @@ impl<'a> Parser<'a> {
&mut stmts,
&mut expr);
}
StmtMac(macro, MacStmtWithoutBraces) => {
StmtMac(mac, MacStmtWithoutBraces) => {
// statement macro without braces; might be an
// expr depending on whether a semicolon follows
match self.token {
token::Semi => {
stmts.push(P(Spanned {
node: StmtMac(macro,
node: StmtMac(mac,
MacStmtWithSemicolon),
span: span,
}));
Expand All @@ -3896,7 +3896,7 @@ impl<'a> Parser<'a> {
_ => {
let e = self.mk_mac_expr(span.lo,
span.hi,
macro.and_then(|m| m.node));
mac.and_then(|m| m.node));
let e =
self.parse_dot_or_call_expr_with(e);
self.handle_expression_like_statement(
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/parse/token.rs
Expand Up @@ -574,6 +574,7 @@ declare_special_idents_and_keywords! {
(56, Abstract, "abstract");
(57, Final, "final");
(58, Override, "override");
(59, Macro, "macro");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/show_span.rs
Expand Up @@ -28,8 +28,8 @@ impl<'a, 'v> Visitor<'v> for ShowSpanVisitor<'a> {
visit::walk_expr(self, e);
}

fn visit_mac(&mut self, macro: &ast::Mac) {
visit::walk_mac(self, macro);
fn visit_mac(&mut self, mac: &ast::Mac) {
visit::walk_mac(self, mac);
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/libsyntax/visit.rs
Expand Up @@ -115,13 +115,13 @@ pub trait Visitor<'v> : Sized {
fn visit_explicit_self(&mut self, es: &'v ExplicitSelf) {
walk_explicit_self(self, es)
}
fn visit_mac(&mut self, _macro: &'v Mac) {
fn visit_mac(&mut self, _mac: &'v Mac) {
panic!("visit_mac disabled by default");
// NB: see note about macros above.
// if you really want a visitor that
// works on macros, use this
// definition in your trait impl:
// visit::walk_mac(self, _macro)
// visit::walk_mac(self, _mac)
}
fn visit_path(&mut self, path: &'v Path, _id: ast::NodeId) {
walk_path(self, path)
Expand Down Expand Up @@ -334,7 +334,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
visitor.visit_trait_item(method)
}
}
ItemMac(ref macro) => visitor.visit_mac(macro),
ItemMac(ref mac) => visitor.visit_mac(mac),
}
for attr in item.attrs.iter() {
visitor.visit_attribute(attr);
Expand Down Expand Up @@ -546,7 +546,7 @@ pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
visitor.visit_pat(&**postpattern)
}
}
PatMac(ref macro) => visitor.visit_mac(macro),
PatMac(ref mac) => visitor.visit_mac(mac),
}
}

Expand Down Expand Up @@ -746,7 +746,7 @@ pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
StmtExpr(ref expression, _) | StmtSemi(ref expression, _) => {
visitor.visit_expr(&**expression)
}
StmtMac(ref macro, _) => visitor.visit_mac(&**macro),
StmtMac(ref mac, _) => visitor.visit_mac(&**mac),
}
}

Expand Down Expand Up @@ -893,7 +893,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
ExprRet(ref optional_expression) => {
walk_expr_opt(visitor, optional_expression)
}
ExprMac(ref macro) => visitor.visit_mac(macro),
ExprMac(ref mac) => visitor.visit_mac(mac),
ExprParen(ref subexpression) => {
visitor.visit_expr(&**subexpression)
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/compile-fail/macro-keyword.rs
@@ -0,0 +1,15 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn macro() { //~ ERROR `macro` is a reserved keyword
}

pub fn main() {
}

0 comments on commit c9f0ff3

Please sign in to comment.