Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2374,15 +2374,28 @@ impl<'a> Parser<'a> {
let pat = self.parse_pat_no_top_alt(Some(Expected::ArgumentName), None)?;
self.expect(exp!(Colon))?;
let ty = self.parse_ty()?;
self.dcx().emit_err(PatternMethodParamWithoutBody {
span: pat.span,
target: match context {
FnContext::Trait => "methods without bodies",
FnContext::FunctionPtrType => "function pointer types",
FnContext::Free => unreachable!("This method is not called in free functions, as patterns are always allowed there"),
FnContext::Impl => unreachable!("This method is not called in impls, as patterns are always allowed there"),
},
});
match context {
FnContext::Trait | FnContext::FunctionPtrType => {
self.dcx().emit_err(PatternMethodParamWithoutBody {
span: pat.span,
target: if context == FnContext::Trait {
"methods without bodies"
} else {
"function pointer types"
},
});
}
FnContext::Free | FnContext::Impl => {
self.dcx().span_delayed_bug(
pat.span,
if context == FnContext::Free {
"This method is not called in free functions, as patterns are always allowed there"
} else {
"This method is not called in impls, as patterns are always allowed there"
},
);
}
}

// Pretend the pattern is `_`, to avoid duplicate errors from AST validation.
let pat = Box::new(Pat { kind: PatKind::Wild, span: pat.span, id: ast::DUMMY_NODE_ID });
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/parser/recover-invalid-fn-trait-bound-pattern.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Regression test for https://github.com/rust-lang/rust/issues/160337.

struct Baz where U : fn(() : bool)
//~^ ERROR expected identifier, found keyword `fn`
//~| ERROR expected `{` after struct name, found `<eof>`
20 changes: 20 additions & 0 deletions tests/ui/parser/recover-invalid-fn-trait-bound-pattern.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: expected identifier, found keyword `fn`
--> $DIR/recover-invalid-fn-trait-bound-pattern.rs:3:22
|
LL | struct Baz where U : fn(() : bool)
| ^^
|
help: use `Fn` to refer to the trait (notice the capitalization)
|
LL - struct Baz where U : fn(() : bool)
LL + struct Baz where U : Fn(() : bool)
|

error: expected `{` after struct name, found `<eof>`
--> $DIR/recover-invalid-fn-trait-bound-pattern.rs:3:34
|
LL | struct Baz where U : fn(() : bool)
| ^ expected `{` after struct name

error: aborting due to 2 previous errors

6 changes: 6 additions & 0 deletions tests/ui/parser/recover-invalid-function-parameter-pattern.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Regression test for https://github.com/rust-lang/rust/issues/160337.

fn main(... : ...)
//~^ ERROR unexpected `...`
//~| ERROR unexpected `...`
//~| ERROR expected one of `->`, `where`, or `{`, found `<eof>`
28 changes: 28 additions & 0 deletions tests/ui/parser/recover-invalid-function-parameter-pattern.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
error: unexpected `...`
--> $DIR/recover-invalid-function-parameter-pattern.rs:3:9
|
LL | fn main(... : ...)
| ^^^ not a valid pattern
|
help: for a rest pattern, use `..` instead of `...`
|
LL - fn main(... : ...)
LL + fn main(.. : ...)
|

error: unexpected `...`
--> $DIR/recover-invalid-function-parameter-pattern.rs:3:15
|
LL | fn main(... : ...)
| ^^^
|
= note: only `extern "C"` and `extern "C-unwind"` functions may have a C variable argument list

error: expected one of `->`, `where`, or `{`, found `<eof>`
--> $DIR/recover-invalid-function-parameter-pattern.rs:3:18
|
LL | fn main(... : ...)
| ^ expected one of `->`, `where`, or `{`

error: aborting due to 3 previous errors

Loading