Skip to content

Commit 547f9aa

Browse files
authored
Rollup merge of #162498 - TaKO8Ki:fix/parser-arg-recovery-ice, r=fee1-dead
Avoid ICEs when recovering malformed function parameters Fixes #160337 Fixes #160490
2 parents 9d3cc46 + e2160bd commit 547f9aa

8 files changed

Lines changed: 136 additions & 12 deletions

‎compiler/rustc_parse/src/parser/diagnostics.rs‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,16 +2140,32 @@ impl<'a> Parser<'a> {
21402140
let pat = self.parse_pat_no_top_alt(Some(Expected::ArgumentName), None)?;
21412141
self.expect(exp!(Colon))?;
21422142
let ty = self.parse_ty()?;
2143-
self.dcx().emit_err(PatternMethodParamWithoutBody {
2144-
span: pat.span,
2145-
target: match context {
2146-
FnContext::Trait => "methods without bodies",
2147-
FnContext::FunctionPtrType => "function pointer types",
2148-
FnContext::ParenthesizedArgumentList => "parenthesized argument list",
2149-
FnContext::Free => unreachable!("This method is not called in free functions, as patterns are always allowed there"),
2150-
FnContext::Impl => unreachable!("This method is not called in impls, as patterns are always allowed there"),
2151-
},
2152-
});
2143+
match context {
2144+
FnContext::Trait
2145+
| FnContext::FunctionPtrType
2146+
| FnContext::ParenthesizedArgumentList => {
2147+
self.dcx().emit_err(PatternMethodParamWithoutBody {
2148+
span: pat.span,
2149+
target: if context == FnContext::Trait {
2150+
"methods without bodies"
2151+
} else if context == FnContext::FunctionPtrType {
2152+
"function pointer types"
2153+
} else {
2154+
"parenthesized argument list"
2155+
},
2156+
});
2157+
}
2158+
FnContext::Free | FnContext::Impl => {
2159+
self.dcx().span_delayed_bug(
2160+
pat.span,
2161+
if context == FnContext::Free {
2162+
"This method is not called in free functions, as patterns are always allowed there"
2163+
} else {
2164+
"This method is not called in impls, as patterns are always allowed there"
2165+
},
2166+
);
2167+
}
2168+
}
21532169

21542170
// Pretend the pattern is `_`, to avoid duplicate errors from AST validation.
21552171
let pat = Box::new(Pat { kind: PatKind::Wild, span: pat.span, id: ast::DUMMY_NODE_ID });

‎tests/crashes/160490.rs‎

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Regression test for https://github.com/rust-lang/rust/issues/160337.
2+
3+
struct Baz where U : fn(() : bool)
4+
//~^ ERROR expected identifier, found keyword `fn`
5+
//~| ERROR expected `{` after struct name, found `<eof>`
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: expected identifier, found keyword `fn`
2+
--> $DIR/recover-invalid-fn-trait-bound-pattern.rs:3:22
3+
|
4+
LL | struct Baz where U : fn(() : bool)
5+
| ^^
6+
|
7+
help: use `Fn` to refer to the trait (notice the capitalization)
8+
|
9+
LL - struct Baz where U : fn(() : bool)
10+
LL + struct Baz where U : Fn(() : bool)
11+
|
12+
13+
error: expected `{` after struct name, found `<eof>`
14+
--> $DIR/recover-invalid-fn-trait-bound-pattern.rs:3:34
15+
|
16+
LL | struct Baz where U : fn(() : bool)
17+
| ^ expected `{` after struct name
18+
19+
error: aborting due to 2 previous errors
20+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Regression test for https://github.com/rust-lang/rust/issues/160337.
2+
3+
fn main(... : ...)
4+
//~^ ERROR unexpected `...`
5+
//~| ERROR unexpected `...`
6+
//~| ERROR expected one of `->`, `where`, or `{`, found `<eof>`
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: unexpected `...`
2+
--> $DIR/recover-invalid-function-parameter-pattern-1.rs:3:9
3+
|
4+
LL | fn main(... : ...)
5+
| ^^^ not a valid pattern
6+
|
7+
help: for a rest pattern, use `..` instead of `...`
8+
|
9+
LL - fn main(... : ...)
10+
LL + fn main(.. : ...)
11+
|
12+
13+
error: unexpected `...`
14+
--> $DIR/recover-invalid-function-parameter-pattern-1.rs:3:15
15+
|
16+
LL | fn main(... : ...)
17+
| ^^^
18+
|
19+
= note: only `extern "C"` and `extern "C-unwind"` functions may have a C variable argument list
20+
21+
error: expected one of `->`, `where`, or `{`, found `<eof>`
22+
--> $DIR/recover-invalid-function-parameter-pattern-1.rs:3:18
23+
|
24+
LL | fn main(... : ...)
25+
| ^ expected one of `->`, `where`, or `{`
26+
27+
error: aborting due to 3 previous errors
28+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Regression test for https://github.com/rust-lang/rust/issues/160490.
2+
3+
fn f(...: u8) {}
4+
//~^ ERROR unexpected `...`
5+
//~| ERROR missing pattern for `...` argument
6+
//~| WARN this was previously accepted by the compiler
7+
8+
fn main() {}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error: unexpected `...`
2+
--> $DIR/recover-invalid-function-parameter-pattern-2.rs:3:6
3+
|
4+
LL | fn f(...: u8) {}
5+
| ^^^ not a valid pattern
6+
|
7+
help: for a rest pattern, use `..` instead of `...`
8+
|
9+
LL - fn f(...: u8) {}
10+
LL + fn f(..: u8) {}
11+
|
12+
13+
error: missing pattern for `...` argument
14+
--> $DIR/recover-invalid-function-parameter-pattern-2.rs:3:6
15+
|
16+
LL | fn f(...: u8) {}
17+
| ^^^
18+
|
19+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
20+
= note: for more information, see issue #145544 <https://github.com/rust-lang/rust/issues/145544>
21+
= note: `#[deny(varargs_without_pattern)]` (part of `#[deny(future_incompatible)]`) on by default
22+
help: name the argument, or use `_` to continue ignoring it
23+
|
24+
LL | fn f(_: ...: u8) {}
25+
| ++
26+
27+
error: aborting due to 2 previous errors
28+
29+
Future incompatibility report: Future breakage diagnostic:
30+
error: missing pattern for `...` argument
31+
--> $DIR/recover-invalid-function-parameter-pattern-2.rs:3:6
32+
|
33+
LL | fn f(...: u8) {}
34+
| ^^^
35+
|
36+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
37+
= note: for more information, see issue #145544 <https://github.com/rust-lang/rust/issues/145544>
38+
= note: `#[deny(varargs_without_pattern)]` (part of `#[deny(future_incompatible)]`) on by default
39+
help: name the argument, or use `_` to continue ignoring it
40+
|
41+
LL | fn f(_: ...: u8) {}
42+
| ++
43+

0 commit comments

Comments
 (0)