From: lishin <lishin1...@gmail.com>

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 <lishin1...@gmail.com>
---
 gcc/rust/backend/rust-compile-expr.cc               | 10 +++++++++-
 gcc/testsuite/rust/compile/issue-3618.rs            |  3 ++-
 gcc/testsuite/rust/compile/loop_constant_context.rs |  5 +++++
 3 files changed, 16 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/loop_constant_context.rs

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,
+                    "%<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 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
-- 
2.49.0

Reply via email to