Skip to content

Commit d616603

Browse files
committed
auto merge of #9673 : huonw/rust/macros, r=catamorphism
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.
2 parents d00c926 + 8284df9 commit d616603

File tree

3 files changed

+56
-78
lines changed

3 files changed

+56
-78
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-77
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

@@ -185,79 +210,6 @@ pub fn add_new_extension(cx: @ExtCtxt,
185210
_ => cx.span_bug(sp, "wrong-structured rhs")
186211
};
187212

188-
// Given `lhses` and `rhses`, this is the new macro we create
189-
fn generic_extension(cx: @ExtCtxt,
190-
sp: Span,
191-
name: Ident,
192-
arg: &[ast::token_tree],
193-
lhses: &[@named_match],
194-
rhses: &[@named_match])
195-
-> MacResult {
196-
if cx.trace_macros() {
197-
println!("{}! \\{ {} \\}",
198-
cx.str_of(name),
199-
print::pprust::tt_to_str(
200-
&ast::tt_delim(@mut arg.to_owned()),
201-
get_ident_interner()));
202-
}
203-
204-
// Which arm's failure should we report? (the one furthest along)
205-
let mut best_fail_spot = dummy_sp();
206-
let mut best_fail_msg = ~"internal error: ran no matchers";
207-
208-
let s_d = cx.parse_sess().span_diagnostic;
209-
210-
for (i, lhs) in lhses.iter().enumerate() { // try each arm's matchers
211-
match *lhs {
212-
@matched_nonterminal(nt_matchers(ref mtcs)) => {
213-
// `none` is because we're not interpolating
214-
let arg_rdr = new_tt_reader(
215-
s_d,
216-
None,
217-
arg.to_owned()
218-
) as @mut reader;
219-
match parse(cx.parse_sess(), cx.cfg(), arg_rdr, *mtcs) {
220-
success(named_matches) => {
221-
let rhs = match rhses[i] {
222-
// okay, what's your transcriber?
223-
@matched_nonterminal(nt_tt(@ref tt)) => {
224-
match (*tt) {
225-
// cut off delimiters; don't parse 'em
226-
tt_delim(ref tts) => {
227-
(*tts).slice(1u,(*tts).len()-1u).to_owned()
228-
}
229-
_ => cx.span_fatal(
230-
sp, "macro rhs must be delimited")
231-
}
232-
},
233-
_ => cx.span_bug(sp, "bad thing in rhs")
234-
};
235-
// rhs has holes ( `$id` and `$(...)` that need filled)
236-
let trncbr = new_tt_reader(s_d, Some(named_matches),
237-
rhs);
238-
let p = @Parser(cx.parse_sess(),
239-
cx.cfg(),
240-
trncbr as @mut reader);
241-
242-
// Let the context choose how to interpret the result.
243-
// Weird, but useful for X-macros.
244-
return MRAny(@ParserAnyMacro {
245-
parser: p
246-
} as @AnyMacro);
247-
}
248-
failure(sp, ref msg) => if sp.lo >= best_fail_spot.lo {
249-
best_fail_spot = sp;
250-
best_fail_msg = (*msg).clone();
251-
},
252-
error(sp, ref msg) => cx.span_fatal(sp, (*msg))
253-
}
254-
}
255-
_ => cx.bug("non-matcher found in parsed lhses")
256-
}
257-
}
258-
cx.span_fatal(best_fail_spot, best_fail_msg);
259-
}
260-
261213
let exp = @MacroRulesSyntaxExpanderTTFun {
262214
name: name,
263215
lhses: lhses,
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)