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 DCD743838037 for ; Thu, 22 Jul 2021 23:19:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DCD743838037 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 (deer0x18.wildebeest.org [172.31.17.154]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 5BF9C300066F; Fri, 23 Jul 2021 01:19:40 +0200 (CEST) Received: by reform (Postfix, from userid 1000) id E75FE2E80C66; Fri, 23 Jul 2021 01:19:39 +0200 (CEST) From: Mark Wielaard To: gcc-rust@gcc.gnu.org Cc: Mark Wielaard Subject: [PATCH 1/2] Better union support in the parser Date: Fri, 23 Jul 2021 01:19:01 +0200 Message-Id: <20210722231902.7401-2-mark@klomp.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210722231902.7401-1-mark@klomp.org> References: <20210722231902.7401-1-mark@klomp.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.8 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Thu, 22 Jul 2021 23:19:43 -0000 union is a weak keyword which means it isn't reserved and can be used as a generic identifier. When we see an identifier where a union could be declared we check whether the identifier is "union", but only when the next token is also an identifier. In parse_union we shouldn't skip the first identifier token, because it is already skipped when we call expect_token. --- gcc/rust/parse/rust-parse-impl.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index bdf1e09a029..3996ef21672 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -1083,7 +1083,8 @@ Parser::parse_item (bool called_from_statement) // crappy hack to do union "keyword" case IDENTIFIER: // TODO: ensure std::string and literal comparison works - if (t->get_str () == "union") + if (t->get_str () == "union" + && lexer.peek_token (1)->get_id () == IDENTIFIER) { return parse_vis_item (std::move (outer_attrs)); // or should this go straight to parsing union? @@ -1274,8 +1275,8 @@ Parser::parse_vis_item (AST::AttrVec outer_attrs) // TODO: implement union keyword but not really because of // context-dependence case UNION: crappy hack to do union "keyword" case IDENTIFIER: - // TODO: ensure std::string and literal comparison works - if (t->get_str () == "union") + if (t->get_str () == "union" + && lexer.peek_token (1)->get_id () == IDENTIFIER) { return parse_union (std::move (vis), std::move (outer_attrs)); // or should item switch go straight to parsing union? @@ -4524,7 +4525,6 @@ Parser::parse_union (AST::Visibility vis, const_TokenPtr union_keyword = expect_token (IDENTIFIER); rust_assert (union_keyword->get_str () == "union"); Location locus = union_keyword->get_locus (); - lexer.skip_token (); // parse actual union name const_TokenPtr union_name_tok = expect_token (IDENTIFIER); @@ -6054,8 +6054,8 @@ Parser::parse_stmt () break; // crappy hack to do union "keyword" case IDENTIFIER: - // TODO: ensure std::string and literal comparison works - if (t->get_str () == "union") + if (t->get_str () == "union" + && lexer.peek_token (1)->get_id () == IDENTIFIER) { return parse_vis_item (std::move (outer_attrs)); // or should this go straight to parsing union? @@ -11674,8 +11674,8 @@ Parser::parse_stmt_or_expr_without_block () } // crappy hack to do union "keyword" case IDENTIFIER: - // TODO: ensure std::string and literal comparison works - if (t->get_str () == "union") + if (t->get_str () == "union" + && lexer.peek_token (1)->get_id () == IDENTIFIER) { std::unique_ptr item ( parse_vis_item (std::move (outer_attrs))); -- 2.32.0