]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fix validation of constant items
authorOwen Avery <powerboat9.gamer@gmail.com>
Tue, 25 Mar 2025 22:18:21 +0000 (18:18 -0400)
committerP-E-P <32375388+P-E-P@users.noreply.github.com>
Wed, 26 Mar 2025 14:59:58 +0000 (14:59 +0000)
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 48577b98c0cb129d5cc30f82c7743a9e2322cbf1..b1de1e6e664af2f3fb11c0045e0895853dc4d338 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;
+}