Unnamed repository; edit this file 'description' to name the repository.
fix: handle empty expr in tuple expr
Use logic similar to `arg_list` for parsing Example --- ```rust const _=(a,,b) ``` **Before this PR** ```text [email protected] [email protected] "const" [email protected] " " [email protected] "_" [email protected] "=" [email protected] [email protected] "(" [email protected] [email protected] [email protected] [email protected] [email protected] "a" [email protected] "," [email protected] [email protected] "," [email protected] [email protected] [email protected] [email protected] [email protected] "b" [email protected] [email protected] ")" [email protected] "\n" ``` **After this PR** ```text [email protected] [email protected] "const" [email protected] " " [email protected] "_" [email protected] "=" TUPLE_EXPR L_PAREN "(" PATH_EXPR PATH PATH_SEGMENT NAME_REF IDENT "a" COMMA "," COMMA "," PATH_EXPR PATH PATH_SEGMENT NAME_REF IDENT "b" R_PAREN ")" ```
A4-Tacks 3 weeks ago
parent 9006fee · commit 4854f38
-rw-r--r--crates/parser/src/grammar/expressions/atom.rs21
-rw-r--r--crates/parser/test_data/parser/inline/err/tuple_expr_leading_comma.rast23
-rw-r--r--crates/parser/test_data/parser/inline/err/tuple_expr_leading_comma.rs1
3 files changed, 35 insertions, 10 deletions
diff --git a/crates/parser/src/grammar/expressions/atom.rs b/crates/parser/src/grammar/expressions/atom.rs
index 3620a27c23..52dfb049aa 100644
--- a/crates/parser/src/grammar/expressions/atom.rs
+++ b/crates/parser/src/grammar/expressions/atom.rs
@@ -216,16 +216,19 @@ fn tuple_expr(p: &mut Parser<'_>) -> CompletedMarker {
let mut saw_comma = 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![')']) {
+ // test_err tuple_expr_leading_comma
+ // fn foo() {
+ // (,);
+ // (a, , b);
+ // }
+ if p.current() == T![,] {
+ p.error("expected expression");
+ p.bump(T![,]);
+ saw_comma = true;
+ continue;
+ }
+
saw_expr = true;
// test tuple_attrs
diff --git a/crates/parser/test_data/parser/inline/err/tuple_expr_leading_comma.rast b/crates/parser/test_data/parser/inline/err/tuple_expr_leading_comma.rast
index 3fbc0da400..2ccd04c321 100644
--- a/crates/parser/test_data/parser/inline/err/tuple_expr_leading_comma.rast
+++ b/crates/parser/test_data/parser/inline/err/tuple_expr_leading_comma.rast
@@ -18,7 +18,28 @@ SOURCE_FILE
COMMA ","
R_PAREN ")"
SEMICOLON ";"
+ WHITESPACE "\n "
+ EXPR_STMT
+ TUPLE_EXPR
+ L_PAREN "("
+ PATH_EXPR
+ PATH
+ PATH_SEGMENT
+ NAME_REF
+ IDENT "a"
+ COMMA ","
+ WHITESPACE " "
+ COMMA ","
+ WHITESPACE " "
+ PATH_EXPR
+ PATH
+ PATH_SEGMENT
+ NAME_REF
+ IDENT "b"
+ R_PAREN ")"
+ SEMICOLON ";"
WHITESPACE "\n"
R_CURLY "}"
WHITESPACE "\n"
-error 17: expected expression
+error 16: expected expression
+error 27: expected expression
diff --git a/crates/parser/test_data/parser/inline/err/tuple_expr_leading_comma.rs b/crates/parser/test_data/parser/inline/err/tuple_expr_leading_comma.rs
index 12fab59a77..37f756ffa6 100644
--- a/crates/parser/test_data/parser/inline/err/tuple_expr_leading_comma.rs
+++ b/crates/parser/test_data/parser/inline/err/tuple_expr_leading_comma.rs
@@ -1,3 +1,4 @@
fn foo() {
(,);
+ (a, , b);
}