Skip to content

Commit 2b2360a

Browse files
authored
Rollup merge of rust-lang#117298 - clubby789:fn-missing-params, r=petrochenkov
Recover from missing param list in function definitions Addresses the other issue mentioned in rust-lang#108109
2 parents b0a0759 + ca1bcb6 commit 2b2360a

10 files changed

+65
-19
lines changed

compiler/rustc_parse/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,9 @@ parse_missing_fn_for_function_definition = missing `fn` for function definition
526526
parse_missing_fn_for_method_definition = missing `fn` for method definition
527527
.suggestion = add `fn` here to parse `{$ident}` as a public method
528528
529+
parse_missing_fn_params = missing parameters for function definition
530+
.suggestion = add a parameter list
531+
529532
parse_missing_for_in_trait_impl = missing `for` in a trait impl
530533
.suggestion = add `for` here
531534

compiler/rustc_parse/src/errors.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,14 @@ pub(crate) enum AmbiguousMissingKwForItemSub {
15511551
HelpMacro,
15521552
}
15531553

1554+
#[derive(Diagnostic)]
1555+
#[diag(parse_missing_fn_params)]
1556+
pub(crate) struct MissingFnParams {
1557+
#[primary_span]
1558+
#[suggestion(code = "()", applicability = "machine-applicable", style = "short")]
1559+
pub span: Span,
1560+
}
1561+
15541562
#[derive(Diagnostic)]
15551563
#[diag(parse_missing_trait_in_trait_impl)]
15561564
pub(crate) struct MissingTraitInTraitImpl {

compiler/rustc_parse/src/parser/item.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2499,6 +2499,16 @@ impl<'a> Parser<'a> {
24992499
pub(super) fn parse_fn_params(&mut self, req_name: ReqName) -> PResult<'a, ThinVec<Param>> {
25002500
let mut first_param = true;
25012501
// Parse the arguments, starting out with `self` being allowed...
2502+
if self.token.kind != TokenKind::OpenDelim(Delimiter::Parenthesis)
2503+
// might be typo'd trait impl, handled elsewhere
2504+
&& !self.token.is_keyword(kw::For)
2505+
{
2506+
// recover from missing argument list, e.g. `fn main -> () {}`
2507+
self.sess
2508+
.emit_err(errors::MissingFnParams { span: self.prev_token.span.shrink_to_hi() });
2509+
return Ok(ThinVec::new());
2510+
}
2511+
25022512
let (mut params, _) = self.parse_paren_comma_seq(|p| {
25032513
p.recover_diff_marker();
25042514
let snapshot = p.create_snapshot_for_diagnostic();

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)