From 5a440f1bc29c06fdaa93ce8e05da70fc081f74ba Mon Sep 17 00:00:00 2001 From: varkor Date: Tue, 29 Sep 2020 01:03:02 +0100 Subject: [PATCH 1/3] Fix control flow check for breaking with diverging values --- compiler/rustc_typeck/src/check/expr.rs | 5 ++++- src/test/ui/break-diverging-value.rs | 13 +++++++++++++ src/test/ui/break-diverging-value.stderr | 11 +++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/break-diverging-value.rs create mode 100644 src/test/ui/break-diverging-value.stderr diff --git a/compiler/rustc_typeck/src/check/expr.rs b/compiler/rustc_typeck/src/check/expr.rs index 9990f86a36b13..15820d367c0a6 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 0000000000000..0498580c7bd8a --- /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 0000000000000..78a8d78ed3400 --- /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`. From d415fae8b76cddbcd7825cb6642186a833453b3c Mon Sep 17 00:00:00 2001 From: varkor Date: Wed, 21 Oct 2020 22:52:41 +0100 Subject: [PATCH 2/3] Add tests for uninhabited types --- src/test/ui/break-diverging-value.rs | 25 ++++++++++++++++++++ src/test/ui/break-diverging-value.stderr | 29 +++++++++++++++++++++--- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/test/ui/break-diverging-value.rs b/src/test/ui/break-diverging-value.rs index 0498580c7bd8a..0117abeb105cf 100644 --- a/src/test/ui/break-diverging-value.rs +++ b/src/test/ui/break-diverging-value.rs @@ -1,3 +1,5 @@ +#![feature(never_type)] + fn loop_break_return() -> i32 { let loop_value = loop { break return 0 }; // ok } @@ -10,4 +12,27 @@ fn loop_break_break() -> i32 { //~ ERROR mismatched types let loop_value = loop { break break }; } +fn loop_break_return_2() -> i32 { //~ ERROR mismatched types + let loop_value = loop { break { return; () } }; + //~^ ERROR `return;` in a function whose return type is not `()` +} + +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 index 78a8d78ed3400..de3f9bd778a9d 100644 --- a/src/test/ui/break-diverging-value.stderr +++ b/src/test/ui/break-diverging-value.stderr @@ -1,11 +1,34 @@ error[E0308]: mismatched types - --> $DIR/break-diverging-value.rs:9:26 + --> $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: aborting due to previous error +error[E0069]: `return;` in a function whose return type is not `()` + --> $DIR/break-diverging-value.rs:16:37 + | +LL | let loop_value = loop { break { return; () } }; + | ^^^^^^ return type is not `()` + +error[E0308]: mismatched types + --> $DIR/break-diverging-value.rs:15:29 + | +LL | fn loop_break_return_2() -> i32 { + | ------------------- ^^^ expected `i32`, found `()` + | | + | implicitly returns `()` as its body has no tail or `return` expression + +error[E0308]: mismatched types + --> $DIR/break-diverging-value.rs:26: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 4 previous errors -For more information about this error, try `rustc --explain E0308`. +Some errors have detailed explanations: E0069, E0308. +For more information about an error, try `rustc --explain E0069`. From d1c2815d6aa131182aa93603de215d63c52d0c53 Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 22 Oct 2020 23:12:46 +0100 Subject: [PATCH 3/3] Use `diverges` instead of `!`-type --- compiler/rustc_typeck/src/check/expr.rs | 2 +- src/test/ui/break-diverging-value.rs | 5 ++--- src/test/ui/break-diverging-value.stderr | 21 +++------------------ 3 files changed, 6 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_typeck/src/check/expr.rs b/compiler/rustc_typeck/src/check/expr.rs index 15820d367c0a6..dce3be9399dd6 100644 --- a/compiler/rustc_typeck/src/check/expr.rs +++ b/compiler/rustc_typeck/src/check/expr.rs @@ -630,7 +630,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // 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(); + 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 index 0117abeb105cf..d070fddaffc19 100644 --- a/src/test/ui/break-diverging-value.rs +++ b/src/test/ui/break-diverging-value.rs @@ -12,9 +12,8 @@ fn loop_break_break() -> i32 { //~ ERROR mismatched types let loop_value = loop { break break }; } -fn loop_break_return_2() -> i32 { //~ ERROR mismatched types - let loop_value = loop { break { return; () } }; - //~^ ERROR `return;` in a function whose return type is not `()` +fn loop_break_return_2() -> i32 { + let loop_value = loop { break { return 0; () } }; // ok } enum Void {} diff --git a/src/test/ui/break-diverging-value.stderr b/src/test/ui/break-diverging-value.stderr index de3f9bd778a9d..69edcd2408002 100644 --- a/src/test/ui/break-diverging-value.stderr +++ b/src/test/ui/break-diverging-value.stderr @@ -6,29 +6,14 @@ LL | fn loop_break_break() -> i32 { | | | implicitly returns `()` as its body has no tail or `return` expression -error[E0069]: `return;` in a function whose return type is not `()` - --> $DIR/break-diverging-value.rs:16:37 - | -LL | let loop_value = loop { break { return; () } }; - | ^^^^^^ return type is not `()` - -error[E0308]: mismatched types - --> $DIR/break-diverging-value.rs:15:29 - | -LL | fn loop_break_return_2() -> i32 { - | ------------------- ^^^ expected `i32`, found `()` - | | - | implicitly returns `()` as its body has no tail or `return` expression - error[E0308]: mismatched types - --> $DIR/break-diverging-value.rs:26:25 + --> $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 4 previous errors +error: aborting due to 2 previous errors -Some errors have detailed explanations: E0069, E0308. -For more information about an error, try `rustc --explain E0069`. +For more information about this error, try `rustc --explain E0308`.