Skip to content

Commit 690addc

Browse files
committed
parser: fix ICE with invalid variable declaration in macro call
Fix ICE on parsing an invalid variable declaration as a statement like: ``` macro_rules! m { ($s:stmt) => {} } m! { var x } ```
1 parent 703d95e commit 690addc

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

compiler/rustc_parse/src/parser/stmt.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ impl<'a> Parser<'a> {
7272

7373
Ok(Some(if self.token.is_keyword(kw::Let) {
7474
self.parse_local_mk(lo, attrs, capture_semi, force_collect)?
75-
} else if self.is_kw_followed_by_ident(kw::Mut) {
75+
} else if self.is_kw_followed_by_ident(kw::Mut) && self.may_recover() {
7676
self.recover_stmt_local(lo, attrs, InvalidVariableDeclarationSub::MissingLet)?
77-
} else if self.is_kw_followed_by_ident(kw::Auto) {
77+
} else if self.is_kw_followed_by_ident(kw::Auto) && self.may_recover() {
7878
self.bump(); // `auto`
7979
self.recover_stmt_local(lo, attrs, InvalidVariableDeclarationSub::UseLetNotAuto)?
80-
} else if self.is_kw_followed_by_ident(sym::var) {
80+
} else if self.is_kw_followed_by_ident(sym::var) && self.may_recover() {
8181
self.bump(); // `var`
8282
self.recover_stmt_local(lo, attrs, InvalidVariableDeclarationSub::UseLetNotVar)?
8383
} else if self.check_path() && !self.token.is_qpath_start() && !self.is_path_start_item() {
@@ -244,7 +244,7 @@ impl<'a> Parser<'a> {
244244
}
245245

246246
fn recover_local_after_let(&mut self, lo: Span, attrs: AttrWrapper) -> PResult<'a, Stmt> {
247-
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
247+
self.collect_tokens_trailing_token(attrs, ForceCollect::Yes, |this, attrs| {
248248
let local = this.parse_local(attrs)?;
249249
// FIXME - maybe capture semicolon in recovery?
250250
Ok((

src/test/ui/macros/issue-103529.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
macro_rules! m {
2+
($s:stmt) => {}
3+
}
4+
5+
m! { mut x }
6+
//~^ ERROR expected expression, found keyword `mut`
7+
//~| ERROR expected a statement
8+
m! { auto x }
9+
//~^ ERROR invalid variable declaration
10+
m! { var x }
11+
//~^ ERROR invalid variable declaration
12+
13+
fn main() {}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error: expected expression, found keyword `mut`
2+
--> $DIR/issue-103529.rs:5:6
3+
|
4+
LL | m! { mut x }
5+
| ^^^ expected expression
6+
7+
error: expected a statement
8+
--> $DIR/issue-103529.rs:5:10
9+
|
10+
LL | ($s:stmt) => {}
11+
| ------- while parsing argument for this `stmt` macro fragment
12+
...
13+
LL | m! { mut x }
14+
| ^
15+
16+
error: invalid variable declaration
17+
--> $DIR/issue-103529.rs:8:6
18+
|
19+
LL | m! { auto x }
20+
| ^^^^
21+
|
22+
help: write `let` instead of `auto` to introduce a new variable
23+
|
24+
LL | m! { let x }
25+
| ~~~
26+
27+
error: invalid variable declaration
28+
--> $DIR/issue-103529.rs:10:6
29+
|
30+
LL | m! { var x }
31+
| ^^^
32+
|
33+
help: write `let` instead of `var` to introduce a new variable
34+
|
35+
LL | m! { let x }
36+
| ~~~
37+
38+
error: aborting due to 4 previous errors
39+

0 commit comments

Comments
 (0)