Recover from leading comma in tuple pat and expr

This commit is contained in:
Lukas Wirth 2023-06-01 08:40:50 +02:00
parent 42450d2511
commit 7d1bf7023d
7 changed files with 89 additions and 4 deletions

View File

@ -2003,7 +2003,10 @@ fn main() {
let _: (&str, u32, u32)= ($0, 1, 3); let _: (&str, u32, u32)= ($0, 1, 3);
} }
"#, "#,
expect![""], expect![[r#"
(&str, u32)
^^^^ ---
"#]],
); );
check( check(
r#" r#"
@ -2011,7 +2014,10 @@ fn main() {
let _: (&str, u32, u32, u32)= ($0, 1, 3); let _: (&str, u32, u32, u32)= ($0, 1, 3);
} }
"#, "#,
expect![""], expect![[r#"
(&str, u32)
^^^^ ---
"#]],
); );
check( check(
r#" r#"
@ -2019,7 +2025,10 @@ fn main() {
let _: (&str, u32, u32)= ($0, 1, 3, 5); let _: (&str, u32, u32)= ($0, 1, 3, 5);
} }
"#, "#,
expect![""], expect![[r#"
(&str, u32, u32)
^^^^ --- ---
"#]],
); );
} }
@ -2111,7 +2120,7 @@ fn main() {
check( check(
r#" r#"
fn main() { fn main() {
let ($0 1, 3): (i32, i32, i32); let ($0, 1, 3): (i32, i32, i32);
} }
"#, "#,
// FIXME: tuple pat should be of size 3 ideally // FIXME: tuple pat should be of size 3 ideally

View File

@ -184,6 +184,16 @@ fn tuple_expr(p: &mut Parser<'_>) -> CompletedMarker {
let mut saw_comma = false; let mut saw_comma = false;
let mut saw_expr = false; let mut saw_expr = false;
// test_err tuple_expr_leading_comma
// fn foo() {
// (,);
// }
if p.eat(T![,]) {
p.error("expected expression");
saw_comma = true;
}
while !p.at(EOF) && !p.at(T![')']) { while !p.at(EOF) && !p.at(T![')']) {
saw_expr = true; saw_expr = true;

View File

@ -413,6 +413,16 @@ fn tuple_pat(p: &mut Parser<'_>) -> CompletedMarker {
let mut has_comma = false; let mut has_comma = false;
let mut has_pat = false; let mut has_pat = false;
let mut has_rest = false; let mut has_rest = false;
// test_err tuple_pat_leading_comma
// fn foo() {
// let (,);
// }
if p.eat(T![,]) {
p.error("expected pattern");
has_comma = true;
}
while !p.at(EOF) && !p.at(T![')']) { while !p.at(EOF) && !p.at(T![')']) {
has_pat = true; has_pat = true;
if !p.at_ts(PAT_TOP_FIRST) { if !p.at_ts(PAT_TOP_FIRST) {

View File

@ -0,0 +1,24 @@
SOURCE_FILE
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "foo"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
EXPR_STMT
TUPLE_EXPR
L_PAREN "("
COMMA ","
R_PAREN ")"
SEMICOLON ";"
WHITESPACE "\n"
R_CURLY "}"
WHITESPACE "\n"
error 17: expected expression

View File

@ -0,0 +1,3 @@
fn foo() {
(,);
}

View File

@ -0,0 +1,26 @@
SOURCE_FILE
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "foo"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
LET_STMT
LET_KW "let"
WHITESPACE " "
TUPLE_PAT
L_PAREN "("
COMMA ","
R_PAREN ")"
SEMICOLON ";"
WHITESPACE "\n"
R_CURLY "}"
WHITESPACE "\n"
error 21: expected pattern

View File

@ -0,0 +1,3 @@
fn foo() {
let (,);
}