Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 7d1bf70

Browse files
committed
Recover from leading comma in tuple pat and expr
1 parent 42450d2 commit 7d1bf70

File tree

7 files changed

+89
-4
lines changed

7 files changed

+89
-4
lines changed

crates/ide/src/signature_help.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,23 +2003,32 @@ fn main() {
20032003
let _: (&str, u32, u32)= ($0, 1, 3);
20042004
}
20052005
"#,
2006-
expect![""],
2006+
expect![[r#"
2007+
(&str, u32)
2008+
^^^^ ---
2009+
"#]],
20072010
);
20082011
check(
20092012
r#"
20102013
fn main() {
20112014
let _: (&str, u32, u32, u32)= ($0, 1, 3);
20122015
}
20132016
"#,
2014-
expect![""],
2017+
expect![[r#"
2018+
(&str, u32)
2019+
^^^^ ---
2020+
"#]],
20152021
);
20162022
check(
20172023
r#"
20182024
fn main() {
20192025
let _: (&str, u32, u32)= ($0, 1, 3, 5);
20202026
}
20212027
"#,
2022-
expect![""],
2028+
expect![[r#"
2029+
(&str, u32, u32)
2030+
^^^^ --- ---
2031+
"#]],
20232032
);
20242033
}
20252034

@@ -2111,7 +2120,7 @@ fn main() {
21112120
check(
21122121
r#"
21132122
fn main() {
2114-
let ($0 1, 3): (i32, i32, i32);
2123+
let ($0, 1, 3): (i32, i32, i32);
21152124
}
21162125
"#,
21172126
// FIXME: tuple pat should be of size 3 ideally

crates/parser/src/grammar/expressions/atom.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@ fn tuple_expr(p: &mut Parser<'_>) -> CompletedMarker {
184184

185185
let mut saw_comma = false;
186186
let mut saw_expr = false;
187+
188+
// test_err tuple_expr_leading_comma
189+
// fn foo() {
190+
// (,);
191+
// }
192+
if p.eat(T![,]) {
193+
p.error("expected expression");
194+
saw_comma = true;
195+
}
196+
187197
while !p.at(EOF) && !p.at(T![')']) {
188198
saw_expr = true;
189199

crates/parser/src/grammar/patterns.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,16 @@ fn tuple_pat(p: &mut Parser<'_>) -> CompletedMarker {
413413
let mut has_comma = false;
414414
let mut has_pat = false;
415415
let mut has_rest = false;
416+
417+
// test_err tuple_pat_leading_comma
418+
// fn foo() {
419+
// let (,);
420+
// }
421+
if p.eat(T![,]) {
422+
p.error("expected pattern");
423+
has_comma = true;
424+
}
425+
416426
while !p.at(EOF) && !p.at(T![')']) {
417427
has_pat = true;
418428
if !p.at_ts(PAT_TOP_FIRST) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
SOURCE_FILE
2+
FN
3+
FN_KW "fn"
4+
WHITESPACE " "
5+
NAME
6+
IDENT "foo"
7+
PARAM_LIST
8+
L_PAREN "("
9+
R_PAREN ")"
10+
WHITESPACE " "
11+
BLOCK_EXPR
12+
STMT_LIST
13+
L_CURLY "{"
14+
WHITESPACE "\n "
15+
EXPR_STMT
16+
TUPLE_EXPR
17+
L_PAREN "("
18+
COMMA ","
19+
R_PAREN ")"
20+
SEMICOLON ";"
21+
WHITESPACE "\n"
22+
R_CURLY "}"
23+
WHITESPACE "\n"
24+
error 17: expected expression
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn foo() {
2+
(,);
3+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
SOURCE_FILE
2+
FN
3+
FN_KW "fn"
4+
WHITESPACE " "
5+
NAME
6+
IDENT "foo"
7+
PARAM_LIST
8+
L_PAREN "("
9+
R_PAREN ")"
10+
WHITESPACE " "
11+
BLOCK_EXPR
12+
STMT_LIST
13+
L_CURLY "{"
14+
WHITESPACE "\n "
15+
LET_STMT
16+
LET_KW "let"
17+
WHITESPACE " "
18+
TUPLE_PAT
19+
L_PAREN "("
20+
COMMA ","
21+
R_PAREN ")"
22+
SEMICOLON ";"
23+
WHITESPACE "\n"
24+
R_CURLY "}"
25+
WHITESPACE "\n"
26+
error 21: expected pattern
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn foo() {
2+
let (,);
3+
}

0 commit comments

Comments
 (0)