Skip to content

Commit 8284df9

Browse files
committed
syntax: indicate an error when a macro ignores trailing tokens.
That is, only a single expression or item gets parsed, so if there are any extra tokens (e.g. the start of another item/expression) the user should be told, rather than silently dropping them. An example: macro_rules! foo { () => { println("hi"); println("bye); } } would expand to just `println("hi")`, which is almost certainly not what the programmer wanted. Fixes #8012.
1 parent bbbafc4 commit 8284df9

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

src/libsyntax/ext/deriving/cmp/eq.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn expand_deriving_eq(cx: @ExtCtxt,
3939
ret_ty: Literal(Path::new(~["bool"])),
4040
const_nonmatching: true,
4141
combine_substructure: $f
42-
},
42+
}
4343
}
4444
);
4545

src/libsyntax/ext/tt/macro_rules.rs

+29-4
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,47 @@ use ext::tt::macro_parser::{parse, parse_or_else, success, failure};
2121
use parse::lexer::{new_tt_reader, reader};
2222
use parse::parser::Parser;
2323
use parse::token::{get_ident_interner, special_idents, gensym_ident, ident_to_str};
24-
use parse::token::{FAT_ARROW, SEMI, nt_matchers, nt_tt};
24+
use parse::token::{FAT_ARROW, SEMI, nt_matchers, nt_tt, EOF};
2525
use print;
2626

2727
struct ParserAnyMacro {
2828
parser: @Parser,
2929
}
3030

31+
impl ParserAnyMacro {
32+
/// Make sure we don't have any tokens left to parse, so we don't
33+
/// silently drop anything. `allow_semi` is so that "optional"
34+
/// semilons at the end of normal expressions aren't complained
35+
/// about e.g. the semicolon in `macro_rules! kapow( () => {
36+
/// fail!(); } )` doesn't get picked up by .parse_expr(), but it's
37+
/// allowed to be there.
38+
fn ensure_complete_parse(&self, allow_semi: bool) {
39+
if allow_semi && *self.parser.token == SEMI {
40+
self.parser.bump()
41+
}
42+
if *self.parser.token != EOF {
43+
let msg = format!("macro expansion ignores token `{}` and any following",
44+
self.parser.this_token_to_str());
45+
self.parser.span_err(*self.parser.span, msg);
46+
}
47+
}
48+
}
49+
3150
impl AnyMacro for ParserAnyMacro {
3251
fn make_expr(&self) -> @ast::Expr {
33-
self.parser.parse_expr()
52+
let ret = self.parser.parse_expr();
53+
self.ensure_complete_parse(true);
54+
ret
3455
}
3556
fn make_item(&self) -> Option<@ast::item> {
36-
self.parser.parse_item(~[]) // no attrs
57+
let ret = self.parser.parse_item(~[]); // no attrs
58+
self.ensure_complete_parse(false);
59+
ret
3760
}
3861
fn make_stmt(&self) -> @ast::Stmt {
39-
self.parser.parse_stmt(~[]) // no attrs
62+
let ret = self.parser.parse_stmt(~[]); // no attrs
63+
self.ensure_complete_parse(true);
64+
ret
4065
}
4166
}
4267

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
macro_rules! ignored_item {
12+
() => {
13+
fn foo() {}
14+
fn bar() {} //~ ERROR macro expansion ignores token `fn`
15+
}
16+
}
17+
18+
macro_rules! ignored_expr {
19+
() => ( 1, 2 ) //~ ERROR macro expansion ignores token `,`
20+
}
21+
22+
ignored_item!()
23+
24+
fn main() {
25+
ignored_expr!()
26+
}

0 commit comments

Comments
 (0)