From 6db87f77d6849e5590298b4f2968367bbbf5dbe2 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Mon, 23 Oct 2023 17:06:43 +0200 Subject: [PATCH 1/2] Parse const with no value expression Const with no value expression may exist either in trait or in disabled blocks. This means we should be able to parse those correctly. gcc/rust/ChangeLog: * ast/rust-item.h: Add a new constructor for const with no value expression. * parse/rust-parse-impl.h (Parser::parse_const_item): Allow const with no expression value. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/ast/rust-item.h | 7 +++++++ gcc/rust/parse/rust-parse-impl.h | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index aae054969633..8b7e5f58bd12 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -2618,6 +2618,13 @@ class ConstantItem : public VisItem, const_expr (std::move (const_expr)), locus (locus) {} + ConstantItem (std::string ident, Visibility vis, std::unique_ptr type, + std::vector outer_attrs, location_t locus) + : VisItem (std::move (vis), std::move (outer_attrs)), + identifier (std::move (ident)), type (std::move (type)), + const_expr (nullptr), locus (locus) + {} + ConstantItem (ConstantItem const &other) : VisItem (other), identifier (other.identifier), locus (other.locus) { diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 2b2ba6286657..1a1db724ab99 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -4783,6 +4783,16 @@ Parser::parse_const_item (AST::Visibility vis, // parse constant type (required) std::unique_ptr type = parse_type (); + // A const with no given expression value + if (lexer.peek_token ()->get_id () == SEMICOLON) + { + lexer.skip_token (); + return std::unique_ptr ( + new AST::ConstantItem (std::move (ident), std::move (vis), + std::move (type), std::move (outer_attrs), + locus)); + } + if (!skip_token (EQUAL)) { skip_after_semicolon (); From 090990e48c4787f3de89d80534faa0155862744b Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Mon, 23 Oct 2023 17:16:25 +0200 Subject: [PATCH 2/2] Add a new regression test for issue 2665 Highlight issue 2665's fix for const with no value expression. gcc/testsuite/ChangeLog: * rust/compile/issue-2665.rs: New test. Signed-off-by: Pierre-Emmanuel Patry --- gcc/testsuite/rust/compile/issue-2665.rs | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 gcc/testsuite/rust/compile/issue-2665.rs diff --git a/gcc/testsuite/rust/compile/issue-2665.rs b/gcc/testsuite/rust/compile/issue-2665.rs new file mode 100644 index 000000000000..3ee8e7b22ca0 --- /dev/null +++ b/gcc/testsuite/rust/compile/issue-2665.rs @@ -0,0 +1,6 @@ +fn main() {} + +#[cfg(FALSE)] +impl X { + const Y: u8; +}