From: lishin Date: Wed, 16 Jul 2025 00:39:48 +0000 (+0100) Subject: gccrs: Reject loop in const/static context X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=ecb3582903c87c6d2bcedae7b75e57c4f13540b3;p=thirdparty%2Fgcc.git 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 --- diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index a93e848304e..25da59d982d 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, + "% 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 97286135e3a..3bf2c7efd94 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 00000000000..ed0782b91a6 --- /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