]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Reject loop in const/static context
authorlishin <lishin1008@gmail.com>
Wed, 16 Jul 2025 00:39:48 +0000 (01:39 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 5 Aug 2025 14:36:56 +0000 (16:36 +0200)
gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::visit): Add a catch for const/static.

gcc/testsuite/ChangeLog:

* rust/compile/loop_constant_context.rs: New test.
* rust/compile/issue-3618.rs:

Signed-off-by: lishin <lishin1008@gmail.com>
gcc/rust/backend/rust-compile-expr.cc
gcc/testsuite/rust/compile/issue-3618.rs
gcc/testsuite/rust/compile/loop_constant_context.rs [new file with mode: 0644]

index a93e848304e60956c1a347936f8f6fc5b6fe0c7a..25da59d982d223ce572a20809626e2b3e8d9b659 100644 (file)
@@ -682,6 +682,15 @@ void
 CompileExpr::visit (HIR::LoopExpr &expr)
 {
   TyTy::BaseType *block_tyty = nullptr;
+  fncontext fnctx = ctx->peek_fn ();
+  if (ctx->const_context_p () && !DECL_DECLARED_CONSTEXPR_P (fnctx.fndecl))
+    {
+      rich_location r (line_table, expr.get_locus ());
+      rust_error_at (r, ErrorCode::E0658,
+                    "%<loop%> is not allowed in const context");
+      return;
+    }
+
   if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
                                       &block_tyty))
     {
@@ -689,7 +698,6 @@ CompileExpr::visit (HIR::LoopExpr &expr)
       return;
     }
 
-  fncontext fnctx = ctx->peek_fn ();
   tree enclosing_scope = ctx->peek_enclosing_scope ();
   tree block_type = TyTyResolveCompile::compile (ctx, block_tyty);
 
index 97286135e3a515b879b709e80b1059c3d2e83f4c..3bf2c7efd94d107da7d865a72854568dde08a891 100644 (file)
@@ -1 +1,2 @@
-static _X: () = loop {}; // { dg-error "loop iteration count exceeds limit" }
+static _X : ()
+           = loop{}; // { dg-error "'loop' is not allowed in const context" }
diff --git a/gcc/testsuite/rust/compile/loop_constant_context.rs b/gcc/testsuite/rust/compile/loop_constant_context.rs
new file mode 100644 (file)
index 0000000..ed0782b
--- /dev/null
@@ -0,0 +1,5 @@
+// { dg-error "'loop' is not allowed in const context" "" { target *-*-* } .+1 }
+const CONST_LOOP : () = loop{};
+
+// { dg-error "'loop' is not allowed in const context" "" { target *-*-* } .+1 }
+static STATIC_LOOP : () = loop{};
\ No newline at end of file