]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Evaluate the enum's discriminant in a const context
authorRyutaro Okada <1015ryu88@gmail.com>
Sun, 30 Mar 2025 16:28:41 +0000 (09:28 -0700)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 8 Apr 2025 08:17:11 +0000 (10:17 +0200)
gcc/rust/ChangeLog:

* backend/rust-compile-resolve-path.cc: Evaluate the enum's discriminant in a const context

gcc/testsuite/ChangeLog:

* rust/compile/enum_discriminant1.rs: New test.

Signed-off-by: Ryutaro Okada <1015ryu88@gmail.com>
gcc/rust/backend/rust-compile-resolve-path.cc
gcc/testsuite/rust/compile/enum_discriminant1.rs [new file with mode: 0644]

index 115dd0464657061ac04affeb4ab3944b0973cfca..3cb1eb6faa50f41d0d820e5f48a86bd092998f7e 100644 (file)
@@ -105,7 +105,9 @@ ResolvePathRef::attempt_constructor_expression_lookup (
 
   // make the ctor for the union
   HIR::Expr &discrim_expr = variant->get_discriminant ();
+  ctx->push_const_context ();
   tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
+  ctx->pop_const_context ();
   tree folded_discrim_expr = fold_expr (discrim_expr_node);
   tree qualifier = folded_discrim_expr;
 
diff --git a/gcc/testsuite/rust/compile/enum_discriminant1.rs b/gcc/testsuite/rust/compile/enum_discriminant1.rs
new file mode 100644 (file)
index 0000000..32092b2
--- /dev/null
@@ -0,0 +1,7 @@
+enum Foo {
+    Bar = 3 + 12,
+}
+
+fn test() -> Foo { // { dg-warning "function is never used" }
+    return Foo::Bar;
+}
\ No newline at end of file