Skip to content

Commit 45567f9

Browse files
committed
gccrs: Fix crash on break outside of loop context
We need to add a guard to catch the case when there is no loop context for break outside of loop. Fixes #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 <[email protected]>
1 parent 654d800 commit 45567f9

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,10 @@ CompileExpr::visit (HIR::BreakExpr &expr)
828828
{
829829
tree compiled_expr = CompileExpr::Compile (expr.get_expr (), ctx);
830830

831+
translated = error_mark_node;
832+
if (!ctx->have_loop_context ())
833+
return;
834+
831835
Bvariable *loop_result_holder = ctx->peek_loop_context ();
832836
tree result_reference
833837
= Backend::var_expression (loop_result_holder,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#[lang = "sized"]
2+
pub trait Sized {
3+
// Empty.
4+
}
5+
6+
#[lang = "fn_once"]
7+
pub trait FnOnce<Args> {
8+
#[lang = "fn_once_output"]
9+
type Output;
10+
11+
extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
12+
}
13+
14+
fn main() {
15+
[(); {
16+
while true {
17+
// { dg-error ".constexpr. loop iteration count exceeds limit" "" { target *-*-* } .-1 }
18+
break 9;
19+
// { dg-error "can only .break. with a value inside a .loop. block .E0571." "" { target *-*-* } .-1 }
20+
}
21+
51
22+
}];
23+
24+
while true {
25+
break (|| {
26+
// { dg-error "can only .break. with a value inside a .loop. block .E0571." "" { target *-*-* } .-1 }
27+
let while_true = 9;
28+
});
29+
}
30+
}

0 commit comments

Comments
 (0)