Skip to content

Commit 7fa2c6c

Browse files
committed
Auto merge of #30011 - jonas-schievink:macro-context, r=nrc
Fixes #22425 Also fixes #30007, since it's just a change from `true` to `false`.
2 parents 5faed4c + 5cb5d20 commit 7fa2c6c

File tree

4 files changed

+63
-11
lines changed

4 files changed

+63
-11
lines changed

src/libsyntax/ext/tt/macro_rules.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl<'a> ParserAnyMacro<'a> {
4444
/// about e.g. the semicolon in `macro_rules! kapow { () => {
4545
/// panic!(); } }` doesn't get picked up by .parse_expr(), but it's
4646
/// allowed to be there.
47-
fn ensure_complete_parse(&self, allow_semi: bool) {
47+
fn ensure_complete_parse(&self, allow_semi: bool, context: &str) {
4848
let mut parser = self.parser.borrow_mut();
4949
if allow_semi && parser.token == token::Semi {
5050
panictry!(parser.bump())
@@ -58,8 +58,8 @@ impl<'a> ParserAnyMacro<'a> {
5858
parser.span_err(span, &msg[..]);
5959

6060
let msg = format!("caused by the macro expansion here; the usage \
61-
of `{}` is likely invalid in this context",
62-
self.macro_ident);
61+
of `{}!` is likely invalid in {} context",
62+
self.macro_ident, context);
6363
parser.span_note(self.site_span, &msg[..]);
6464
}
6565
}
@@ -68,20 +68,20 @@ impl<'a> ParserAnyMacro<'a> {
6868
impl<'a> MacResult for ParserAnyMacro<'a> {
6969
fn make_expr(self: Box<ParserAnyMacro<'a>>) -> Option<P<ast::Expr>> {
7070
let ret = panictry!(self.parser.borrow_mut().parse_expr());
71-
self.ensure_complete_parse(true);
71+
self.ensure_complete_parse(true, "expression");
7272
Some(ret)
7373
}
7474
fn make_pat(self: Box<ParserAnyMacro<'a>>) -> Option<P<ast::Pat>> {
7575
let ret = panictry!(self.parser.borrow_mut().parse_pat());
76-
self.ensure_complete_parse(false);
76+
self.ensure_complete_parse(false, "pattern");
7777
Some(ret)
7878
}
7979
fn make_items(self: Box<ParserAnyMacro<'a>>) -> Option<SmallVector<P<ast::Item>>> {
8080
let mut ret = SmallVector::zero();
8181
while let Some(item) = panictry!(self.parser.borrow_mut().parse_item()) {
8282
ret.push(item);
8383
}
84-
self.ensure_complete_parse(false);
84+
self.ensure_complete_parse(false, "item");
8585
Some(ret)
8686
}
8787

@@ -95,7 +95,7 @@ impl<'a> MacResult for ParserAnyMacro<'a> {
9595
_ => ret.push(panictry!(parser.parse_impl_item()))
9696
}
9797
}
98-
self.ensure_complete_parse(false);
98+
self.ensure_complete_parse(false, "item");
9999
Some(ret)
100100
}
101101

@@ -115,13 +115,13 @@ impl<'a> MacResult for ParserAnyMacro<'a> {
115115
}
116116
}
117117
}
118-
self.ensure_complete_parse(false);
118+
self.ensure_complete_parse(false, "statement");
119119
Some(ret)
120120
}
121121

122122
fn make_ty(self: Box<ParserAnyMacro<'a>>) -> Option<P<ast::Ty>> {
123123
let ret = panictry!(self.parser.borrow_mut().parse_ty());
124-
self.ensure_complete_parse(true);
124+
self.ensure_complete_parse(false, "type");
125125
Some(ret)
126126
}
127127
}
@@ -327,7 +327,7 @@ fn check_lhs_nt_follows(cx: &mut ExtCtxt, lhs: &TokenTree, sp: Span) {
327327
tt @ &TokenTree::Sequence(..) => {
328328
check_matcher(cx, Some(tt).into_iter(), &Eof);
329329
},
330-
_ => cx.span_err(sp, "Invalid macro matcher; matchers must be contained \
330+
_ => cx.span_err(sp, "invalid macro matcher; matchers must be contained \
331331
in balanced delimiters or a repetition indicator")
332332
};
333333
// we don't abort on errors on rejection, the driver will do that for us

src/test/compile-fail/invalid-macro-matcher.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
macro_rules! invalid {
12-
_ => (); //~^ ERROR Invalid macro matcher
12+
_ => (); //~^ ERROR invalid macro matcher
1313
}
1414

1515
fn main() {

src/test/compile-fail/issue-30007.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2015 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+
#![feature(type_macros)]
12+
13+
macro_rules! t {
14+
() => ( String ; ); //~ ERROR macro expansion ignores token `;`
15+
}
16+
17+
fn main() {
18+
let i: Vec<t!()>; //~ NOTE caused by the macro expansion here
19+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2015 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+
#![feature(type_macros)]
12+
13+
// (typeof used because it's surprisingly hard to find an unparsed token after a stmt)
14+
macro_rules! m {
15+
() => ( i ; typeof ); //~ ERROR `typeof` is a reserved keyword
16+
//~| ERROR macro expansion ignores token `typeof`
17+
//~| ERROR macro expansion ignores token `typeof`
18+
//~| ERROR macro expansion ignores token `;`
19+
//~| ERROR macro expansion ignores token `;`
20+
//~| ERROR macro expansion ignores token `i`
21+
}
22+
23+
m!(); //~ NOTE the usage of `m!` is likely invalid in item context
24+
25+
fn main() {
26+
let a: m!(); //~ NOTE the usage of `m!` is likely invalid in type context
27+
let i = m!(); //~ NOTE the usage of `m!` is likely invalid in expression context
28+
match 0 {
29+
m!() => {} //~ NOTE the usage of `m!` is likely invalid in pattern context
30+
}
31+
32+
m!(); //~ NOTE the usage of `m!` is likely invalid in statement context
33+
}

0 commit comments

Comments
 (0)