diff options
| author | bors <bors@rust-lang.org> | 2020-10-27 07:10:55 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2020-10-27 07:10:55 +0000 |
| commit | 824f900a96d752da2d882863c65f9736e5f2b347 (patch) | |
| tree | 3a41011b5e1f023cc93c8a54468be0c59514b9d0 | |
| parent | c9b606ed679dd94f3f920ad339c0d2be0952b16c (diff) | |
| parent | d1c2815d6aa131182aa93603de215d63c52d0c53 (diff) | |
| download | rust-824f900a96d752da2d882863c65f9736e5f2b347.tar.gz rust-824f900a96d752da2d882863c65f9736e5f2b347.zip | |
Auto merge of #77317 - varkor:break-diverging-value, r=nikomatsakis
Fix control flow check for breaking with diverging values Fixes https://github.com/rust-lang/rust/issues/77156.
| -rw-r--r-- | compiler/rustc_typeck/src/check/expr.rs | 5 | ||||
| -rw-r--r-- | src/test/ui/break-diverging-value.rs | 37 | ||||
| -rw-r--r-- | src/test/ui/break-diverging-value.stderr | 19 |
3 files changed, 60 insertions, 1 deletions
diff --git a/compiler/rustc_typeck/src/check/expr.rs b/compiler/rustc_typeck/src/check/expr.rs index 03e448a00cc..b0417fca253 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 |= !self.diverges.get().is_always(); // 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..d070fddaffc --- /dev/null +++ b/src/test/ui/break-diverging-value.rs @@ -0,0 +1,37 @@ +#![feature(never_type)] + +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 loop_break_return_2() -> i32 { + let loop_value = loop { break { return 0; () } }; // ok +} + +enum Void {} + +fn get_void() -> Void { + panic!() +} + +fn loop_break_void() -> i32 { //~ ERROR mismatched types + let loop_value = loop { break get_void() }; +} + +fn get_never() -> ! { + panic!() +} + +fn loop_break_never() -> i32 { + let loop_value = loop { break get_never() }; // ok +} + +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..69edcd24080 --- /dev/null +++ b/src/test/ui/break-diverging-value.stderr @@ -0,0 +1,19 @@ +error[E0308]: mismatched types + --> $DIR/break-diverging-value.rs:11:26 + | +LL | fn loop_break_break() -> i32 { + | ---------------- ^^^ expected `i32`, found `()` + | | + | implicitly returns `()` as its body has no tail or `return` expression + +error[E0308]: mismatched types + --> $DIR/break-diverging-value.rs:25:25 + | +LL | fn loop_break_void() -> i32 { + | --------------- ^^^ expected `i32`, found `()` + | | + | implicitly returns `()` as its body has no tail or `return` expression + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`. |
