aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlishin <lishin1008@gmail.com>2025-07-16 01:39:48 +0100
committerArthur Cohen <arthur.cohen@embecosm.com>2025-08-05 16:36:56 +0200
commitecb3582903c87c6d2bcedae7b75e57c4f13540b3 (patch)
treece9e6c694cd670c882bf6969135ec6ed1b5a5b99
parent39aa96ce9382f455bc071a3aa36f866fef760af1 (diff)
downloadgcc-ecb3582903c87c6d2bcedae7b75e57c4f13540b3.zip
gcc-ecb3582903c87c6d2bcedae7b75e57c4f13540b3.tar.gz
gcc-ecb3582903c87c6d2bcedae7b75e57c4f13540b3.tar.bz2
gccrs: Reject loop in const/static context
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>
-rw-r--r--gcc/rust/backend/rust-compile-expr.cc10
-rw-r--r--gcc/testsuite/rust/compile/issue-3618.rs3
-rw-r--r--gcc/testsuite/rust/compile/loop_constant_context.rs5
3 files changed, 16 insertions, 2 deletions
diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc
index a93e848..25da59d 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -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);
diff --git a/gcc/testsuite/rust/compile/issue-3618.rs b/gcc/testsuite/rust/compile/issue-3618.rs
index 9728613..3bf2c7e 100644
--- a/gcc/testsuite/rust/compile/issue-3618.rs
+++ b/gcc/testsuite/rust/compile/issue-3618.rs
@@ -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
index 0000000..ed0782b
--- /dev/null
+++ b/gcc/testsuite/rust/compile/loop_constant_context.rs
@@ -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