about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2023-06-01 08:40:50 +0200
committerLukas Wirth <lukastw97@gmail.com>2023-06-01 08:40:50 +0200
commit7d1bf7023d7c11c20bfc35c9b9f4bfa027858d65 (patch)
tree9e37a929cc91ec47884ebaef28bad7114d7cc94a
parent42450d2511f8f174dc7448d0e9839d4b76d64482 (diff)
downloadrust-7d1bf7023d7c11c20bfc35c9b9f4bfa027858d65.tar.gz
rust-7d1bf7023d7c11c20bfc35c9b9f4bfa027858d65.zip
Recover from leading comma in tuple pat and expr
-rw-r--r--crates/ide/src/signature_help.rs17
-rw-r--r--crates/parser/src/grammar/expressions/atom.rs10
-rw-r--r--crates/parser/src/grammar/patterns.rs10
-rw-r--r--crates/parser/test_data/parser/inline/err/0019_tuple_expr_leading_comma.rast24
-rw-r--r--crates/parser/test_data/parser/inline/err/0019_tuple_expr_leading_comma.rs3
-rw-r--r--crates/parser/test_data/parser/inline/err/0020_tuple_pat_leading_comma.rast26
-rw-r--r--crates/parser/test_data/parser/inline/err/0020_tuple_pat_leading_comma.rs3
7 files changed, 89 insertions, 4 deletions
diff --git a/crates/ide/src/signature_help.rs b/crates/ide/src/signature_help.rs
index 455b519f80b..9bd9a948e7f 100644
--- a/crates/ide/src/signature_help.rs
+++ b/crates/ide/src/signature_help.rs
@@ -2003,7 +2003,10 @@ fn main() {
     let _: (&str, u32, u32)= ($0, 1, 3);
 }
 "#,
-            expect![""],
+            expect![[r#"
+                (&str, u32)
+                 ^^^^  ---
+            "#]],
         );
         check(
             r#"
@@ -2011,7 +2014,10 @@ fn main() {
     let _: (&str, u32, u32, u32)= ($0, 1, 3);
 }
 "#,
-            expect![""],
+            expect![[r#"
+                (&str, u32)
+                 ^^^^  ---
+            "#]],
         );
         check(
             r#"
@@ -2019,7 +2025,10 @@ fn main() {
     let _: (&str, u32, u32)= ($0, 1, 3, 5);
 }
 "#,
-            expect![""],
+            expect![[r#"
+                (&str, u32, u32)
+                 ^^^^  ---  ---
+            "#]],
         );
     }
 
@@ -2111,7 +2120,7 @@ fn main() {
         check(
             r#"
 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
diff --git a/crates/parser/src/grammar/expressions/atom.rs b/crates/parser/src/grammar/expressions/atom.rs
index 3cf9c4dd4b0..d8553d3f953 100644
--- a/crates/parser/src/grammar/expressions/atom.rs
+++ b/crates/parser/src/grammar/expressions/atom.rs
@@ -184,6 +184,16 @@ 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![')']) {
         saw_expr = true;
 
diff --git a/crates/parser/src/grammar/patterns.rs b/crates/parser/src/grammar/patterns.rs
index 4801732101f..39ded41bb24 100644
--- a/crates/parser/src/grammar/patterns.rs
+++ b/crates/parser/src/grammar/patterns.rs
@@ -413,6 +413,16 @@ fn tuple_pat(p: &mut Parser<'_>) -> CompletedMarker {
     let mut has_comma = false;
     let mut has_pat = 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![')']) {
         has_pat = true;
         if !p.at_ts(PAT_TOP_FIRST) {
diff --git a/crates/parser/test_data/parser/inline/err/0019_tuple_expr_leading_comma.rast b/crates/parser/test_data/parser/inline/err/0019_tuple_expr_leading_comma.rast
new file mode 100644
index 00000000000..3fbc0da4002
--- /dev/null
+++ b/crates/parser/test_data/parser/inline/err/0019_tuple_expr_leading_comma.rast
@@ -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
diff --git a/crates/parser/test_data/parser/inline/err/0019_tuple_expr_leading_comma.rs b/crates/parser/test_data/parser/inline/err/0019_tuple_expr_leading_comma.rs
new file mode 100644
index 00000000000..12fab59a776
--- /dev/null
+++ b/crates/parser/test_data/parser/inline/err/0019_tuple_expr_leading_comma.rs
@@ -0,0 +1,3 @@
+fn foo() {
+    (,);
+}
diff --git a/crates/parser/test_data/parser/inline/err/0020_tuple_pat_leading_comma.rast b/crates/parser/test_data/parser/inline/err/0020_tuple_pat_leading_comma.rast
new file mode 100644
index 00000000000..9c8837292d2
--- /dev/null
+++ b/crates/parser/test_data/parser/inline/err/0020_tuple_pat_leading_comma.rast
@@ -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
diff --git a/crates/parser/test_data/parser/inline/err/0020_tuple_pat_leading_comma.rs b/crates/parser/test_data/parser/inline/err/0020_tuple_pat_leading_comma.rs
new file mode 100644
index 00000000000..de168521e1d
--- /dev/null
+++ b/crates/parser/test_data/parser/inline/err/0020_tuple_pat_leading_comma.rs
@@ -0,0 +1,3 @@
+fn foo() {
+    let (,);
+}