Skip to content

Commit 28c3348

Browse files
Lishin1215philberty
authored andcommitted
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 <[email protected]>
1 parent 98ddf74 commit 28c3348

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

gcc/rust/backend/rust-compile-expr.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,14 +682,22 @@ void
682682
CompileExpr::visit (HIR::LoopExpr &expr)
683683
{
684684
TyTy::BaseType *block_tyty = nullptr;
685+
fncontext fnctx = ctx->peek_fn ();
686+
if (ctx->const_context_p () && !DECL_DECLARED_CONSTEXPR_P (fnctx.fndecl))
687+
{
688+
rich_location r (line_table, expr.get_locus ());
689+
rust_error_at (r, ErrorCode::E0658,
690+
"%<loop%> is not allowed in const context");
691+
return;
692+
}
693+
685694
if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
686695
&block_tyty))
687696
{
688697
rust_error_at (expr.get_locus (), "failed to lookup type of BlockExpr");
689698
return;
690699
}
691700

692-
fncontext fnctx = ctx->peek_fn ();
693701
tree enclosing_scope = ctx->peek_enclosing_scope ();
694702
tree block_type = TyTyResolveCompile::compile (ctx, block_tyty);
695703

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
static _X: () = loop {}; // { dg-error "loop iteration count exceeds limit" }
1+
static _X : ()
2+
= loop{}; // { dg-error "'loop' is not allowed in const context" }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// { dg-error "'loop' is not allowed in const context" "" { target *-*-* } .+1 }
2+
const CONST_LOOP : () = loop{};
3+
4+
// { dg-error "'loop' is not allowed in const context" "" { target *-*-* } .+1 }
5+
static STATIC_LOOP : () = loop{};

0 commit comments

Comments
 (0)