]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix crash on break outside of loop context
authorPhilip Herron <herron.philip@googlemail.com>
Sun, 31 Aug 2025 22:20:03 +0000 (23:20 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 30 Oct 2025 19:59:08 +0000 (20:59 +0100)
We need to add a guard to catch the case when there is no loop context
for break outside of loop.

Fixes Rust-GCC#3969

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::visit): add guard

gcc/testsuite/ChangeLog:

* rust/compile/issue-3969.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/backend/rust-compile-expr.cc
gcc/testsuite/rust/compile/issue-3969.rs [new file with mode: 0644]

index 946bbb1234f7c4b0743bf673a4e4604281c71dea..2d910e3e0a4567a0c3ec4d1006867a6d525ab43f 100644 (file)
@@ -828,6 +828,10 @@ CompileExpr::visit (HIR::BreakExpr &expr)
     {
       tree compiled_expr = CompileExpr::Compile (expr.get_expr (), ctx);
 
+      translated = error_mark_node;
+      if (!ctx->have_loop_context ())
+       return;
+
       Bvariable *loop_result_holder = ctx->peek_loop_context ();
       tree result_reference
        = Backend::var_expression (loop_result_holder,
diff --git a/gcc/testsuite/rust/compile/issue-3969.rs b/gcc/testsuite/rust/compile/issue-3969.rs
new file mode 100644 (file)
index 0000000..9608589
--- /dev/null
@@ -0,0 +1,30 @@
+#[lang = "sized"]
+pub trait Sized {
+    // Empty.
+}
+
+#[lang = "fn_once"]
+pub trait FnOnce<Args> {
+    #[lang = "fn_once_output"]
+    type Output;
+
+    extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
+}
+
+fn main() {
+    [(); {
+        while true {
+            // { dg-error ".constexpr. loop iteration count exceeds limit" "" { target *-*-* } .-1 }
+            break 9;
+            // { dg-error "can only .break. with a value inside a .loop. block .E0571." "" { target *-*-* } .-1 }
+        }
+        51
+    }];
+
+    while true {
+        break (|| {
+            // { dg-error "can only .break. with a value inside a .loop. block .E0571." "" { target *-*-* } .-1 }
+            let while_true = 9;
+        });
+    }
+}