Skip to content

Commit

Permalink
Fix control flow check for breaking with diverging values
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed Oct 21, 2020
1 parent 1d27267 commit 5a440f1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion compiler/rustc_typeck/src/check/expr.rs
Expand Up @@ -627,7 +627,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
assert!(expr_opt.is_none() || self.tcx.sess.has_errors());
}

ctxt.may_break = true;
// If we encountered a `break`, then (no surprise) it may be possible to break from the
// loop... unless the value being returned from the loop diverges itself, e.g.
// `break return 5` or `break loop {}`.
ctxt.may_break |= !e_ty.is_never();

// the type of a `break` is always `!`, since it diverges
tcx.types.never
Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/break-diverging-value.rs
@@ -0,0 +1,13 @@
fn loop_break_return() -> i32 {
let loop_value = loop { break return 0 }; // ok
}

fn loop_break_loop() -> i32 {
let loop_value = loop { break loop {} }; // ok
}

fn loop_break_break() -> i32 { //~ ERROR mismatched types
let loop_value = loop { break break };
}

fn main() {}
11 changes: 11 additions & 0 deletions src/test/ui/break-diverging-value.stderr
@@ -0,0 +1,11 @@
error[E0308]: mismatched types
--> $DIR/break-diverging-value.rs:9:26
|
LL | fn loop_break_break() -> i32 {
| ---------------- ^^^ expected `i32`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

0 comments on commit 5a440f1

Please sign in to comment.