diff options
| author | varkor <github@varkor.com> | 2020-09-29 01:03:02 +0100 |
|---|---|---|
| committer | varkor <github@varkor.com> | 2020-10-21 21:46:03 +0100 |
| commit | 5a440f1bc29c06fdaa93ce8e05da70fc081f74ba (patch) | |
| tree | 96024f642dc57e56ea16f6c5ad69d2fa4eb952b7 | |
| parent | 1d2726726f8f3128e98191e4c6cb94bd76d0ddd4 (diff) | |
| download | rust-5a440f1bc29c06fdaa93ce8e05da70fc081f74ba.tar.gz rust-5a440f1bc29c06fdaa93ce8e05da70fc081f74ba.zip | |
Fix control flow check for breaking with diverging values
| -rw-r--r-- | compiler/rustc_typeck/src/check/expr.rs | 5 | ||||
| -rw-r--r-- | src/test/ui/break-diverging-value.rs | 13 | ||||
| -rw-r--r-- | src/test/ui/break-diverging-value.stderr | 11 |
3 files changed, 28 insertions, 1 deletions
diff --git a/compiler/rustc_typeck/src/check/expr.rs b/compiler/rustc_typeck/src/check/expr.rs index 9990f86a36b..15820d367c0 100644 --- a/compiler/rustc_typeck/src/check/expr.rs +++ b/compiler/rustc_typeck/src/check/expr.rs @@ -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 diff --git a/src/test/ui/break-diverging-value.rs b/src/test/ui/break-diverging-value.rs new file mode 100644 index 00000000000..0498580c7bd --- /dev/null +++ b/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() {} diff --git a/src/test/ui/break-diverging-value.stderr b/src/test/ui/break-diverging-value.stderr new file mode 100644 index 00000000000..78a8d78ed34 --- /dev/null +++ b/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`. |
