]> git.lizzy.rs Git - rust.git/commitdiff
Handle pub tuple fields in tuple structs
authorAdam Bratschi-Kaye <ark.email@gmail.com>
Wed, 3 Nov 2021 22:57:46 +0000 (23:57 +0100)
committerAdam Bratschi-Kaye <ark.email@gmail.com>
Wed, 10 Nov 2021 20:29:50 +0000 (21:29 +0100)
The current implementation will throw a parser error for tuple structs
that contain a pub tuple field. For example,
```rust
struct Foo(pub (u32, u32));
```
is valid Rust, but rust-analyzer will throw a parser error.  This is
because the parens after `pub` is treated as a visibility context.
Allowing a tuple type to follow `pub` in the special case when we are
defining fields in a tuple struct can fix the issue.

crates/parser/src/grammar.rs
crates/parser/src/grammar/items.rs
crates/parser/src/grammar/items/adt.rs
crates/syntax/test_data/parser/inline/ok/0196_pub_tuple_field.rast [new file with mode: 0644]
crates/syntax/test_data/parser/inline/ok/0196_pub_tuple_field.rs [new file with mode: 0644]

index 991955fca6d79606d0eda2e88c94c01e48688735..25178ddd775ac301ff056e08c269804650a19f32 100644 (file)
@@ -75,7 +75,7 @@ pub(crate) fn stmt_optional_semi(p: &mut Parser) {
     }
 
     pub(crate) fn visibility(p: &mut Parser) {
-        let _ = opt_visibility(p);
+        let _ = opt_visibility(p, false);
     }
 
     // Parse a meta item , which excluded [], e.g : #[ MetaItem ]
@@ -149,7 +149,7 @@ fn is_block(self) -> bool {
     }
 }
 
-fn opt_visibility(p: &mut Parser) -> bool {
+fn opt_visibility(p: &mut Parser, in_tuple_field: bool) -> bool {
     match p.current() {
         T![pub] => {
             let m = p.start();
@@ -165,9 +165,17 @@ fn opt_visibility(p: &mut Parser) -> bool {
                     // struct B(pub (super::A));
                     // struct B(pub (crate::A,));
                     T![crate] | T![self] | T![super] | T![ident] if p.nth(2) != T![:] => {
-                        p.bump(T!['(']);
-                        paths::use_path(p);
-                        p.expect(T![')']);
+                        // If we are in a tuple struct, then the parens following `pub`
+                        // might be an tuple field, not part of the visibility. So in that
+                        // case we don't want to consume an identifier.
+
+                        // test pub_tuple_field
+                        // struct MyStruct(pub (u32, u32));
+                        if !(in_tuple_field && matches!(p.nth(1), T![ident])) {
+                            p.bump(T!['(']);
+                            paths::use_path(p);
+                            p.expect(T![')']);
+                        }
                     }
                     // test crate_visibility_in
                     // pub(in super::A) struct S;
index 9de9afde5d362464980d648f3c7ff5124bd89717..39be0e1a1926e40c6059fbe94e4134a3b3fa0fd6 100644 (file)
@@ -87,7 +87,7 @@ pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool) {
 pub(super) fn opt_item(p: &mut Parser, m: Marker) -> Result<(), Marker> {
     // test_err pub_expr
     // fn foo() { pub 92; }
-    let has_visibility = opt_visibility(p);
+    let has_visibility = opt_visibility(p, false);
 
     let m = match opt_item_without_modifiers(p, m) {
         Ok(()) => return Ok(()),
index 42ebecc6d6c8a341601f53d1113e2063feb11b47..c5bd5b14baed39f7e86ff6488750917bbba9200f 100644 (file)
@@ -128,7 +128,7 @@ fn record_field(p: &mut Parser) {
         // test record_field_attrs
         // struct S { #[attr] f: f32 }
         attributes::outer_attrs(p);
-        opt_visibility(p);
+        opt_visibility(p, false);
         if p.at(IDENT) {
             name(p);
             p.expect(T![:]);
@@ -150,7 +150,7 @@ fn tuple_field_list(p: &mut Parser) {
         // test tuple_field_attrs
         // struct S (#[attr] f32);
         attributes::outer_attrs(p);
-        opt_visibility(p);
+        opt_visibility(p, true);
         if !p.at_ts(types::TYPE_FIRST) {
             p.error("expected a type");
             m.complete(p, ERROR);
diff --git a/crates/syntax/test_data/parser/inline/ok/0196_pub_tuple_field.rast b/crates/syntax/test_data/parser/inline/ok/0196_pub_tuple_field.rast
new file mode 100644 (file)
index 0000000..8a1f590
--- /dev/null
@@ -0,0 +1,30 @@
+SOURCE_FILE@0..33
+  STRUCT@0..32
+    STRUCT_KW@0..6 "struct"
+    WHITESPACE@6..7 " "
+    NAME@7..15
+      IDENT@7..15 "MyStruct"
+    TUPLE_FIELD_LIST@15..31
+      L_PAREN@15..16 "("
+      TUPLE_FIELD@16..30
+        VISIBILITY@16..19
+          PUB_KW@16..19 "pub"
+        WHITESPACE@19..20 " "
+        TUPLE_TYPE@20..30
+          L_PAREN@20..21 "("
+          PATH_TYPE@21..24
+            PATH@21..24
+              PATH_SEGMENT@21..24
+                NAME_REF@21..24
+                  IDENT@21..24 "u32"
+          COMMA@24..25 ","
+          WHITESPACE@25..26 " "
+          PATH_TYPE@26..29
+            PATH@26..29
+              PATH_SEGMENT@26..29
+                NAME_REF@26..29
+                  IDENT@26..29 "u32"
+          R_PAREN@29..30 ")"
+      R_PAREN@30..31 ")"
+    SEMICOLON@31..32 ";"
+  WHITESPACE@32..33 "\n"
diff --git a/crates/syntax/test_data/parser/inline/ok/0196_pub_tuple_field.rs b/crates/syntax/test_data/parser/inline/ok/0196_pub_tuple_field.rs
new file mode 100644 (file)
index 0000000..00d8feb
--- /dev/null
@@ -0,0 +1 @@
+struct MyStruct(pub (u32, u32));