Skip to content

Commit 398ae3e

Browse files
bors[bot]matklad
andauthored
Merge #9622
9622: feat: improve parser error recovery for function parameters r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 610ecc9 + 15f11dc commit 398ae3e

File tree

10 files changed

+106
-21
lines changed

10 files changed

+106
-21
lines changed

crates/ide_completion/src/completions/fn_param.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,20 @@ fn baz(file$0) {}
101101
);
102102
}
103103

104+
#[test]
105+
fn test_param_completion_first_param() {
106+
check(
107+
r#"
108+
fn foo(file_id: FileId) {}
109+
fn bar(file_id: FileId) {}
110+
fn baz(file$0 id: u32) {}
111+
"#,
112+
expect![[r#"
113+
bn file_id: FileId
114+
"#]],
115+
);
116+
}
117+
104118
#[test]
105119
fn test_param_completion_nth_param() {
106120
check(

crates/parser/src/grammar/items/consts.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ fn const_or_static(p: &mut Parser, m: Marker, kw: SyntaxKind, def: SyntaxKind) {
2121

2222
// test_err static_underscore
2323
// static _: i32 = 5;
24-
25-
types::ascription(p);
24+
if p.at(T![:]) {
25+
types::ascription(p);
26+
} else {
27+
p.error("missing type for `const` or `static`")
28+
}
2629
if p.eat(T![=]) {
2730
expressions::expr(p);
2831
}

crates/parser/src/grammar/params.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,13 @@ fn param(p: &mut Parser, m: Marker, flavor: Flavor) -> Variadic {
106106
if variadic_param(p) {
107107
res = Variadic(true)
108108
} else {
109-
types::ascription(p);
109+
if p.at(T![:]) {
110+
types::ascription(p)
111+
} else {
112+
// test_err missing_fn_param_type
113+
// fn f(x y: i32, z, t: i32) {}
114+
p.error("missing type for function parameter")
115+
}
110116
}
111117
}
112118
// test value_parameters_no_patterns
@@ -126,7 +132,11 @@ fn param(p: &mut Parser, m: Marker, flavor: Flavor) -> Variadic {
126132
if variadic_param(p) {
127133
res = Variadic(true)
128134
} else {
129-
types::ascription(p);
135+
if p.at(T![:]) {
136+
types::ascription(p)
137+
} else {
138+
p.error("missing type for function parameter")
139+
}
130140
}
131141
} else {
132142
types::type_(p);

crates/parser/src/grammar/type_params.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ fn const_param(p: &mut Parser, m: Marker) {
6767
assert!(p.at(T![const]));
6868
p.bump(T![const]);
6969
name(p);
70-
types::ascription(p);
70+
if p.at(T![:]) {
71+
types::ascription(p);
72+
} else {
73+
p.error("missing type for const parameter");
74+
}
7175

7276
// test const_param_defaults
7377
// struct A<const N: i32 = -1>;

crates/parser/src/grammar/types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ fn type_with_bounds_cond(p: &mut Parser, allow_bounds: bool) {
5555
}
5656

5757
pub(super) fn ascription(p: &mut Parser) {
58-
p.expect(T![:]);
58+
assert!(p.at(T![:]));
59+
p.bump(T![:]);
5960
type_(p)
6061
}
6162

crates/syntax/test_data/parser/err/0018_incomplete_fn.rast

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ [email protected]
127127
128128
129129
error 34..34: expected pattern
130-
error 34..34: expected COLON
131-
error 34..34: expected type
130+
error 34..34: missing type for function parameter
132131
error 180..180: expected function arguments
133132
error 180..180: expected a block

crates/syntax/test_data/parser/err/0021_incomplete_param.rast

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,4 @@ [email protected]
3030
3131
3232
33-
error 16..16: expected COLON
34-
error 16..16: expected type
33+
error 16..16: missing type for function parameter

crates/syntax/test_data/parser/err/0045_item_modifiers.rast

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,31 @@ [email protected]
1717
1818
1919
20-
CONST@25..46
20+
CONST@25..40
2121
2222
2323
2424
2525
2626
27-
28-
PATH_TYPE@41..46
29-
PATH@41..46
30-
PATH_SEGMENT@41..46
31-
32-
33-
PARAM_LIST@44..46
34-
35-
27+
28+
MACRO_CALL@41..46
29+
PATH@41..44
30+
PATH_SEGMENT@41..44
31+
32+
33+
TOKEN_TREE@44..46
34+
35+
3636
3737
3838
3939
4040
4141
error 6..6: expected existential, fn, trait or impl
4242
error 38..38: expected a name
43-
error 40..40: expected COLON
43+
error 40..40: missing type for `const` or `static`
44+
error 40..40: expected SEMICOLON
45+
error 44..44: expected BANG
4446
error 46..46: expected SEMICOLON
4547
error 47..47: expected an item
Lines changed: 52 additions & 0 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn f(x y: i32, z, t: i32) {}

0 commit comments

Comments
 (0)