Hi Philip,

On Wed, 2021-06-23 at 10:55 +0100, Philip Herron wrote:
> Small update, I think part of this issue is that the support for
> unit-structs is not there yet in the compiler, so if you remove the unit
> tuple struct and move the other struct definitions outside of the block
> the test case works: https://godbolt.org/z/nb84sEaE4
> 
> It might be enough to help with testing your tuple index fixes.

Very nice. That workaround helps me get unblocked, except for testing
empty struct tuples (unit-structs). But I have a patch for that, and
even included a testcase to proof the parser now handles them. See
attached.

Cheers,

Mark
From a1d03b5182f7e2b16b5201459c98b33a12775888 Mon Sep 17 00:00:00 2001
From: Mark Wielaard <m...@klomp.org>
Date: Wed, 23 Jun 2021 17:59:30 +0200
Subject: [PATCH] Handle empty/unit tuple structs in the parser.

A tuple struct can be empty, in which case it is a unit tuple struct.
Handle this in Parser<ManagedTokenSource>::parse_struct by creating
a empty tuple_field vector instead of calling parse_tuple_fields.

Add a testcase to show empty tuple structs are now accepted.
---
 gcc/rust/parse/rust-parse-impl.h                      |  7 ++++++-
 .../rust/compile/torture/tuple_struct_unit.rs         | 11 +++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/rust/compile/torture/tuple_struct_unit.rs

diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index b4f264e0261..dfac00e3dbc 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -3980,7 +3980,12 @@ Parser<ManagedTokenSource>::parse_struct (AST::Visibility vis,
       lexer.skip_token ();
 
       // parse tuple fields
-      std::vector<AST::TupleField> tuple_fields = parse_tuple_fields ();
+      std::vector<AST::TupleField> tuple_fields;
+      // Might be empty tuple for unit tuple struct.
+      if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
+	tuple_fields = std::vector<AST::TupleField> ();
+      else
+	tuple_fields = parse_tuple_fields ();
 
       // tuple parameters must have closing parenthesis
       if (!skip_token (RIGHT_PAREN))
diff --git a/gcc/testsuite/rust/compile/torture/tuple_struct_unit.rs b/gcc/testsuite/rust/compile/torture/tuple_struct_unit.rs
new file mode 100644
index 00000000000..cda19d2af0b
--- /dev/null
+++ b/gcc/testsuite/rust/compile/torture/tuple_struct_unit.rs
@@ -0,0 +1,11 @@
+struct E();
+struct T(E,E,());
+
+fn main()
+{
+  let z0 = E();
+  let z1 = E();
+  let t = T(z0,z1,());
+  let z = t.2;
+  z
+}
-- 
2.31.1

-- 
Gcc-rust mailing list
Gcc-rust@gcc.gnu.org
https://gcc.gnu.org/mailman/listinfo/gcc-rust

Reply via email to