Skip to content

Commit b181683

Browse files
authored
Rollup merge of #105141 - ohno418:fix-ice-on-invalid-var-decl-in-macro-call, r=compiler-errors
Fix ICE on invalid variable declarations in macro calls This fixes ICE that happens with invalid variable declarations in macro calls like: ```rust macro_rules! m { ($s:stmt) => {} } m! { var x } m! { auto x } m! { mut x } ``` Found this is because of not collecting tokens on recovery, so I changed to force collect them. Fixes #103529.
2 parents 7dbd160 + e481258 commit b181683

File tree

3 files changed

+76
-19
lines changed

3 files changed

+76
-19
lines changed

compiler/rustc_parse/src/parser/stmt.rs

+24-19
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,22 @@ 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) {
76-
self.recover_stmt_local(lo, attrs, InvalidVariableDeclarationSub::MissingLet)?
77-
} else if self.is_kw_followed_by_ident(kw::Auto) {
75+
} else if self.is_kw_followed_by_ident(kw::Mut) && self.may_recover() {
76+
self.recover_stmt_local_after_let(lo, attrs, InvalidVariableDeclarationSub::MissingLet)?
77+
} else if self.is_kw_followed_by_ident(kw::Auto) && self.may_recover() {
7878
self.bump(); // `auto`
79-
self.recover_stmt_local(lo, attrs, InvalidVariableDeclarationSub::UseLetNotAuto)?
80-
} else if self.is_kw_followed_by_ident(sym::var) {
79+
self.recover_stmt_local_after_let(
80+
lo,
81+
attrs,
82+
InvalidVariableDeclarationSub::UseLetNotAuto,
83+
)?
84+
} else if self.is_kw_followed_by_ident(sym::var) && self.may_recover() {
8185
self.bump(); // `var`
82-
self.recover_stmt_local(lo, attrs, InvalidVariableDeclarationSub::UseLetNotVar)?
86+
self.recover_stmt_local_after_let(
87+
lo,
88+
attrs,
89+
InvalidVariableDeclarationSub::UseLetNotVar,
90+
)?
8391
} else if self.check_path() && !self.token.is_qpath_start() && !self.is_path_start_item() {
8492
// We have avoided contextual keywords like `union`, items with `crate` visibility,
8593
// or `auto trait` items. We aim to parse an arbitrary path `a::b` but not something
@@ -213,13 +221,21 @@ impl<'a> Parser<'a> {
213221
}
214222
}
215223

216-
fn recover_stmt_local(
224+
fn recover_stmt_local_after_let(
217225
&mut self,
218226
lo: Span,
219227
attrs: AttrWrapper,
220228
subdiagnostic: fn(Span) -> InvalidVariableDeclarationSub,
221229
) -> PResult<'a, Stmt> {
222-
let stmt = self.recover_local_after_let(lo, attrs)?;
230+
let stmt =
231+
self.collect_tokens_trailing_token(attrs, ForceCollect::Yes, |this, attrs| {
232+
let local = this.parse_local(attrs)?;
233+
// FIXME - maybe capture semicolon in recovery?
234+
Ok((
235+
this.mk_stmt(lo.to(this.prev_token.span), StmtKind::Local(local)),
236+
TrailingToken::None,
237+
))
238+
})?;
223239
self.sess.emit_err(InvalidVariableDeclaration { span: lo, sub: subdiagnostic(lo) });
224240
Ok(stmt)
225241
}
@@ -243,17 +259,6 @@ impl<'a> Parser<'a> {
243259
})
244260
}
245261

246-
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| {
248-
let local = this.parse_local(attrs)?;
249-
// FIXME - maybe capture semicolon in recovery?
250-
Ok((
251-
this.mk_stmt(lo.to(this.prev_token.span), StmtKind::Local(local)),
252-
TrailingToken::None,
253-
))
254-
})
255-
}
256-
257262
/// Parses a local variable declaration.
258263
fn parse_local(&mut self, attrs: AttrVec) -> PResult<'a, P<Local>> {
259264
let lo = self.prev_token.span;

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

+13
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() {}
+39
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)