Skip to content

Commit 6857c93

Browse files
committed
Check break target availability when checking breaks with values
Fixes #66702
1 parent 4787e97 commit 6857c93

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/librustc_typeck/check/expr.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
582582
// If this is a break with a value, we need to type-check
583583
// the expression. Get an expected type from the loop context.
584584
let opt_coerce_to = {
585+
// We should release `enclosing_breakables` before the `check_expr_with_hint`
586+
// below, so can't move this block of code to the enclosing scope and share
587+
// `ctxt` with the second `encloding_breakables` borrow below.
585588
let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
586-
enclosing_breakables.find_breakable(target_id)
587-
.coerce
588-
.as_ref()
589-
.map(|coerce| coerce.expected_ty())
589+
match enclosing_breakables.opt_find_breakable(target_id) {
590+
Some(ctxt) =>
591+
ctxt.coerce.as_ref().map(|coerce| coerce.expected_ty()),
592+
None => { // Avoid ICE when `break` is inside a closure (#65383).
593+
self.tcx.sess.delay_span_bug(
594+
expr.span,
595+
"break was outside loop, but no error was emitted",
596+
);
597+
return tcx.types.err;
598+
}
599+
}
590600
};
591601

592602
// If the loop context is not a `loop { }`, then break with
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Breaks with values inside closures used to ICE (#66863)
2+
3+
fn main() {
4+
'some_label: loop {
5+
|| break 'some_label (); //~ ERROR: `break` inside of a closure
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0267]: `break` inside of a closure
2+
--> $DIR/issue-66702-break-outside-loop-val.rs:5:12
3+
|
4+
LL | || break 'some_label ();
5+
| -- ^^^^^^^^^^^^^^^^^^^^ cannot `break` inside of a closure
6+
| |
7+
| enclosing closure
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0267`.

0 commit comments

Comments
 (0)