]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix validation of constant items
authorOwen Avery <powerboat9.gamer@gmail.com>
Tue, 25 Mar 2025 22:18:21 +0000 (18:18 -0400)
committerArthur Cohen <arthur.cohen@embecosm.com>
Mon, 31 Mar 2025 19:07:16 +0000 (21:07 +0200)
gcc/rust/ChangeLog:

* checks/errors/rust-ast-validation.cc
(ASTValidation::visit): Allow constant items lacking expressions
if and only if they're associated with a trait definition, not a
trait implementation.

gcc/testsuite/ChangeLog:

* rust/compile/issue-3541-1.rs: New test.
* rust/compile/issue-3541-2.rs: Likewise.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
gcc/rust/checks/errors/rust-ast-validation.cc
gcc/testsuite/rust/compile/issue-3541-1.rs [new file with mode: 0644]
gcc/testsuite/rust/compile/issue-3541-2.rs [new file with mode: 0644]

index 59b28057bab5300e69550b87774d08ed5f53a802..0f4bdeb6935fb22ca5e13f6f4a5bf98edda658a7 100644 (file)
@@ -56,7 +56,7 @@ ASTValidation::visit (AST::LoopLabel &label)
 void
 ASTValidation::visit (AST::ConstantItem &const_item)
 {
-  if (!const_item.has_expr () && ctx.peek () != Kind::TRAIT_IMPL)
+  if (!const_item.has_expr () && ctx.peek () != Kind::TRAIT)
     {
       rust_error_at (const_item.get_locus (),
                     "associated constant in %<impl%> without body");
diff --git a/gcc/testsuite/rust/compile/issue-3541-1.rs b/gcc/testsuite/rust/compile/issue-3541-1.rs
new file mode 100644 (file)
index 0000000..6b47b7e
--- /dev/null
@@ -0,0 +1,5 @@
+impl B for u32 {
+    const BAR: i32; // { dg-error "associated constant in .impl." }
+}
+
+trait B {}
diff --git a/gcc/testsuite/rust/compile/issue-3541-2.rs b/gcc/testsuite/rust/compile/issue-3541-2.rs
new file mode 100644 (file)
index 0000000..9f17eed
--- /dev/null
@@ -0,0 +1,3 @@
+trait B {
+    const BAR: i32;
+}