From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gnu.wildebeest.org (wildebeest.demon.nl [212.238.236.112]) by sourceware.org (Postfix) with ESMTPS id 616AD388983A for ; Wed, 23 Jun 2021 20:15:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 616AD388983A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from reform (deer0x10.wildebeest.org [172.31.17.146]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 23A483000731 for ; Wed, 23 Jun 2021 22:15:37 +0200 (CEST) Received: by reform (Postfix, from userid 1000) id EFF482E8022C; Wed, 23 Jun 2021 22:15:36 +0200 (CEST) Date: Wed, 23 Jun 2021 22:15:36 +0200 From: Mark Wielaard To: gcc-rust@gcc.gnu.org Subject: Re: tuple indexes Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="qvPwzgjqH9vGhGdI" Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-rust@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: gcc-rust mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 23 Jun 2021 20:15:42 -0000 --qvPwzgjqH9vGhGdI Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Jun 23, 2021 at 12:51:34AM +0200, Mark Wielaard wrote: > Finally, the The Rust Reference says "A tuple index is used to refer > to the fields of tuples, tuple structs, and tuple variants." I don't > understand how this would work for tuple variants. Does anybody have > an example of how to refer to a tuple variant so a tuple index can be > used on it? Tom pointed out on irc that it doesn't seem possible to access enum variant types except through a matching expression. The Rust Reference also seems to be inconsistent. As mentioned above it mentions you can use a tuple index to refer to a field of a enum tuple variant. But it also says "A tuple indexing expression accesses fields of tuples and tuple structs." So it probably really isn't possible to use a tuple index on enum tuple variants. I did notice the same issue as for unit tuple struct types. The empty tuple wasn't accepted in the parser. The attached patch, also at https://code.wildebeest.org/git/user/mjw/gccrs/commit/?h=tuple_enum_variant_unit fixes this. It does include a test case, but most of it is commented out because actually resolving enum types isn't implemented yet. If you uncomment the rest of the testcase you get: tuple_enum_variants.rs:3:31: error: failed to resolve TypePath: E 3 | fn f(e0: E, e1: E, e2: E) -> (E,E,E,()) | ^ tuple_enum_variants.rs:3:31: error: unresolved type tuple_enum_variants.rs:3:33: error: failed to resolve TypePath: E 3 | fn f(e0: E, e1: E, e2: E) -> (E,E,E,()) | ^ tuple_enum_variants.rs:3:33: error: unresolved type tuple_enum_variants.rs:3:35: error: failed to resolve TypePath: E 3 | fn f(e0: E, e1: E, e2: E) -> (E,E,E,()) | ^ tuple_enum_variants.rs:3:35: error: unresolved type tuple_enum_variants.rs:3:10: error: failed to resolve TypePath: E 3 | fn f(e0: E, e1: E, e2: E) -> (E,E,E,()) | ^ tuple_enum_variants.rs:3:10: error: unresolved type tuple_enum_variants.rs:3:17: error: failed to resolve TypePath: E 3 | fn f(e0: E, e1: E, e2: E) -> (E,E,E,()) | ^ tuple_enum_variants.rs:3:17: error: unresolved type tuple_enum_variants.rs:3:24: error: failed to resolve TypePath: E 3 | fn f(e0: E, e1: E, e2: E) -> (E,E,E,()) | ^ tuple_enum_variants.rs:3:24: error: unresolved type tuple_enum_variants.rs:13:12: error: unknown root segment in path E::T0 lookup E 13 | let e0 = E::T0(); | ^ tuple_enum_variants.rs:14:12: error: unknown root segment in path E::T1 lookup E 14 | let e1 = E::T1(0); | ^ tuple_enum_variants.rs:15:12: error: unknown root segment in path E::T2 lookup E 15 | let e2 = E::T2(0,1); | ^ Cheers, Mark --qvPwzgjqH9vGhGdI Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-Handle-empty-unit-tuple-enum-variants-in-the-parser.patch" >From ee1d3b6ab4d508caea35efef67ec89f54178781c Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Wed, 23 Jun 2021 21:54:16 +0200 Subject: [PATCH] Handle empty/unit tuple enum variants in the parser. A tuple enum variant can be empty, in which case it is a unit enum variant. Handle this in Parser::parse_enum_item by creating a empty tuple_field vector instead of calling parse_tuple_fields. Add a testcase to show empty tuple enum variant types are now accepted. But note some part of the test is commented out because using the enum type isn't actually possible right now. --- gcc/rust/parse/rust-parse-impl.h | 7 ++++++- .../compile/torture/tuple_enum_variants.rs | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/rust/compile/torture/tuple_enum_variants.rs diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index dfac00e3dbc..9a28f6cdb66 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -4415,7 +4415,12 @@ Parser::parse_enum_item () // tuple enum item lexer.skip_token (); - std::vector tuple_fields = parse_tuple_fields (); + std::vector tuple_fields; + // Might be empty tuple for unit tuple enum variant. + if (lexer.peek_token ()->get_id () == RIGHT_PAREN) + tuple_fields = std::vector (); + else + tuple_fields = parse_tuple_fields (); if (!skip_token (RIGHT_PAREN)) { diff --git a/gcc/testsuite/rust/compile/torture/tuple_enum_variants.rs b/gcc/testsuite/rust/compile/torture/tuple_enum_variants.rs new file mode 100644 index 00000000000..26e3e5d0a71 --- /dev/null +++ b/gcc/testsuite/rust/compile/torture/tuple_enum_variants.rs @@ -0,0 +1,19 @@ +enum E { T0(), T1(i32), T2(i32,u32) } + +/* The following doesn't parse yet... +fn f(e0: E, e1: E, e2: E) -> (E,E,E,()) +{ + let e = e0; + let f = e1; + let g = e2; + (e,f,g,()) +} + +fn main() +{ + let e0 = E::T0(); + let e1 = E::T1(0); + let e2 = E::T2(0,1); + f(e0, e1, e2).3 +} +*/ -- 2.32.0 --qvPwzgjqH9vGhGdI--