Skip to content

Commit ca1bcb6

Browse files
committed
Recover from missing param list in function definitions
1 parent 10143e7 commit ca1bcb6

10 files changed

+65
-19
lines changed

compiler/rustc_parse/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,9 @@ parse_missing_fn_for_function_definition = missing `fn` for function definition
523523
parse_missing_fn_for_method_definition = missing `fn` for method definition
524524
.suggestion = add `fn` here to parse `{$ident}` as a public method
525525
526+
parse_missing_fn_params = missing parameters for function definition
527+
.suggestion = add a parameter list
528+
526529
parse_missing_for_in_trait_impl = missing `for` in a trait impl
527530
.suggestion = add `for` here
528531

compiler/rustc_parse/src/errors.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,14 @@ pub(crate) enum AmbiguousMissingKwForItemSub {
15431543
HelpMacro,
15441544
}
15451545

1546+
#[derive(Diagnostic)]
1547+
#[diag(parse_missing_fn_params)]
1548+
pub(crate) struct MissingFnParams {
1549+
#[primary_span]
1550+
#[suggestion(code = "()", applicability = "machine-applicable", style = "short")]
1551+
pub span: Span,
1552+
}
1553+
15461554
#[derive(Diagnostic)]
15471555
#[diag(parse_missing_trait_in_trait_impl)]
15481556
pub(crate) struct MissingTraitInTraitImpl {

compiler/rustc_parse/src/parser/item.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2492,6 +2492,16 @@ impl<'a> Parser<'a> {
24922492
pub(super) fn parse_fn_params(&mut self, req_name: ReqName) -> PResult<'a, ThinVec<Param>> {
24932493
let mut first_param = true;
24942494
// Parse the arguments, starting out with `self` being allowed...
2495+
if self.token.kind != TokenKind::OpenDelim(Delimiter::Parenthesis)
2496+
// might be typo'd trait impl, handled elsewhere
2497+
&& !self.token.is_keyword(kw::For)
2498+
{
2499+
// recover from missing argument list, e.g. `fn main -> () {}`
2500+
self.sess
2501+
.emit_err(errors::MissingFnParams { span: self.prev_token.span.shrink_to_hi() });
2502+
return Ok(ThinVec::new());
2503+
}
2504+
24952505
let (mut params, _) = self.parse_paren_comma_seq(|p| {
24962506
p.recover_diff_marker();
24972507
let param = p.parse_param_general(req_name, first_param).or_else(|mut e| {

tests/ui/mismatched_types/recovered-block.rs

-6
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,4 @@ pub fn foo() -> Foo {
1212
}
1313
//~^^ ERROR missing `struct` for struct definition
1414

15-
pub fn bar() -> Foo {
16-
fn
17-
Foo { text: "".to_string() }
18-
}
19-
//~^^ ERROR expected one of `(` or `<`, found `{`
20-
2115
fn main() {}

tests/ui/mismatched_types/recovered-block.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,5 @@ help: add `struct` here to parse `Foo` as a public struct
99
LL | pub struct Foo { text }
1010
| ++++++
1111

12-
error: expected one of `(` or `<`, found `{`
13-
--> $DIR/recovered-block.rs:17:9
14-
|
15-
LL | Foo { text: "".to_string() }
16-
| ^ expected one of `(` or `<`
17-
18-
error: aborting due to 2 previous errors
12+
error: aborting due to previous error
1913

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// run-rustfix
2+
3+
pub fn missing() -> () {}
4+
//~^ ERROR missing parameters for function definition
5+
6+
pub fn missing2() {}
7+
//~^ ERROR missing parameters for function definition
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// run-rustfix
2+
3+
pub fn missing -> () {}
4+
//~^ ERROR missing parameters for function definition
5+
6+
pub fn missing2 {}
7+
//~^ ERROR missing parameters for function definition
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: missing parameters for function definition
2+
--> $DIR/issue-108109-fn-missing-params.rs:3:15
3+
|
4+
LL | pub fn missing -> () {}
5+
| ^ help: add a parameter list
6+
7+
error: missing parameters for function definition
8+
--> $DIR/issue-108109-fn-missing-params.rs:6:16
9+
|
10+
LL | pub fn missing2 {}
11+
| ^ help: add a parameter list
12+
13+
error: aborting due to 2 previous errors
14+
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
fn main() {
2-
let x: fn~() = || (); //~ ERROR expected `(`, found `~`
2+
let x: fn~() = || (); //~ ERROR missing parameters for function definition
3+
//~| ERROR expected one of `->`, `;`, or `=`, found `~`
34
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
error: expected `(`, found `~`
1+
error: missing parameters for function definition
22
--> $DIR/removed-syntax-fn-sigil.rs:2:14
33
|
44
LL | let x: fn~() = || ();
5-
| - ^ expected `(`
6-
| |
7-
| while parsing the type for `x`
5+
| ^ help: add a parameter list
86

9-
error: aborting due to previous error
7+
error: expected one of `->`, `;`, or `=`, found `~`
8+
--> $DIR/removed-syntax-fn-sigil.rs:2:14
9+
|
10+
LL | let x: fn~() = || ();
11+
| ^ expected one of `->`, `;`, or `=`
12+
13+
error: aborting due to 2 previous errors
1014

0 commit comments

Comments
 (0)