Skip to content

Commit 824f900

Browse files
committedOct 27, 2020
Auto merge of rust-lang#77317 - varkor:break-diverging-value, r=nikomatsakis
Fix control flow check for breaking with diverging values Fixes rust-lang#77156.
2 parents c9b606e + d1c2815 commit 824f900

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed
 

‎compiler/rustc_typeck/src/check/expr.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
627627
assert!(expr_opt.is_none() || self.tcx.sess.has_errors());
628628
}
629629

630-
ctxt.may_break = true;
630+
// If we encountered a `break`, then (no surprise) it may be possible to break from the
631+
// loop... unless the value being returned from the loop diverges itself, e.g.
632+
// `break return 5` or `break loop {}`.
633+
ctxt.may_break |= !self.diverges.get().is_always();
631634

632635
// the type of a `break` is always `!`, since it diverges
633636
tcx.types.never

‎src/test/ui/break-diverging-value.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#![feature(never_type)]
2+
3+
fn loop_break_return() -> i32 {
4+
let loop_value = loop { break return 0 }; // ok
5+
}
6+
7+
fn loop_break_loop() -> i32 {
8+
let loop_value = loop { break loop {} }; // ok
9+
}
10+
11+
fn loop_break_break() -> i32 { //~ ERROR mismatched types
12+
let loop_value = loop { break break };
13+
}
14+
15+
fn loop_break_return_2() -> i32 {
16+
let loop_value = loop { break { return 0; () } }; // ok
17+
}
18+
19+
enum Void {}
20+
21+
fn get_void() -> Void {
22+
panic!()
23+
}
24+
25+
fn loop_break_void() -> i32 { //~ ERROR mismatched types
26+
let loop_value = loop { break get_void() };
27+
}
28+
29+
fn get_never() -> ! {
30+
panic!()
31+
}
32+
33+
fn loop_break_never() -> i32 {
34+
let loop_value = loop { break get_never() }; // ok
35+
}
36+
37+
fn main() {}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/break-diverging-value.rs:11:26
3+
|
4+
LL | fn loop_break_break() -> i32 {
5+
| ---------------- ^^^ expected `i32`, found `()`
6+
| |
7+
| implicitly returns `()` as its body has no tail or `return` expression
8+
9+
error[E0308]: mismatched types
10+
--> $DIR/break-diverging-value.rs:25:25
11+
|
12+
LL | fn loop_break_void() -> i32 {
13+
| --------------- ^^^ expected `i32`, found `()`
14+
| |
15+
| implicitly returns `()` as its body has no tail or `return` expression
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)
Please sign in to comment.