From a38a1ce29c72e9afea5bdb84e3a46d4c2bf19718 Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 24 May 2018 23:27:36 +0100 Subject: [PATCH 1/3] Fix behaviour of divergence in while loop conditions This fixes `'a: while break 'a {};` being treated as diverging, by tracking break expressions in the same way as in `loop` expressions. --- src/librustc_typeck/check/mod.rs | 14 ++++++++++---- src/test/ui/break-while-condition.rs | 17 +++++++++++++++++ src/test/ui/break-while-condition.stderr | 15 +++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 src/test/ui/break-while-condition.rs create mode 100644 src/test/ui/break-while-condition.stderr diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 70d299437a6e9..b0766f0591301 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3841,10 +3841,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { let ctxt = BreakableCtxt { // cannot use break with a value from a while loop coerce: None, - may_break: true, + may_break: false, // Will get updated if/when we find a `break`. }; - self.with_breakable_ctxt(expr.id, ctxt, || { + let (ctxt, ()) = self.with_breakable_ctxt(expr.id, ctxt, || { self.check_expr_has_type_or_error(&cond, tcx.types.bool); let cond_diverging = self.diverges.get(); self.check_block_no_value(&body); @@ -3853,6 +3853,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { self.diverges.set(cond_diverging); }); + if ctxt.may_break { + // No way to know whether it's diverging because + // of a `break` or an outer `break` or `return`. + self.diverges.set(Diverges::Maybe); + } + self.tcx.mk_nil() } hir::ExprLoop(ref body, _, source) => { @@ -3871,7 +3877,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { let ctxt = BreakableCtxt { coerce, - may_break: false, // will get updated if/when we find a `break` + may_break: false, // Will get updated if/when we find a `break`. }; let (ctxt, ()) = self.with_breakable_ctxt(expr.id, ctxt, || { @@ -3880,7 +3886,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { if ctxt.may_break { // No way to know whether it's diverging because - // of a `break` or an outer `break` or `return. + // of a `break` or an outer `break` or `return`. self.diverges.set(Diverges::Maybe); } diff --git a/src/test/ui/break-while-condition.rs b/src/test/ui/break-while-condition.rs new file mode 100644 index 0000000000000..cc5d17e42d52b --- /dev/null +++ b/src/test/ui/break-while-condition.rs @@ -0,0 +1,17 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(never_type)] + +fn main() { + let _: ! = { //~ ERROR mismatched types + 'a: while break 'a {}; + }; +} diff --git a/src/test/ui/break-while-condition.stderr b/src/test/ui/break-while-condition.stderr new file mode 100644 index 0000000000000..5abf60c86d306 --- /dev/null +++ b/src/test/ui/break-while-condition.stderr @@ -0,0 +1,15 @@ +error[E0308]: mismatched types + --> $DIR/break-while-condition.rs:14:16 + | +LL | let _: ! = { //~ ERROR mismatched types + | ________________^ +LL | | 'a: while break 'a {}; +LL | | }; + | |_____^ expected !, found () + | + = note: expected type `!` + found type `()` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. From 03d816fd934a19d3fdb27080add61b1dddbaad86 Mon Sep 17 00:00:00 2001 From: varkor Date: Fri, 25 May 2018 15:31:13 +0100 Subject: [PATCH 2/3] Add a test for returning inside a while loop --- src/test/ui/break-while-condition.rs | 28 ++++++++++++++-- src/test/ui/break-while-condition.stderr | 42 ++++++++++++++++++++---- 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/src/test/ui/break-while-condition.rs b/src/test/ui/break-while-condition.rs index cc5d17e42d52b..5949b29989324 100644 --- a/src/test/ui/break-while-condition.rs +++ b/src/test/ui/break-while-condition.rs @@ -11,7 +11,29 @@ #![feature(never_type)] fn main() { - let _: ! = { //~ ERROR mismatched types - 'a: while break 'a {}; - }; + // The `if false` expressions are simply to + // make sure we don't avoid checking everything + // simply because a few expressions are unreachable. + + if false { + let _: ! = { //~ ERROR mismatched types + 'a: while break 'a {}; + }; + } + + if false { + let _: ! = { //~ ERROR mismatched types + while false { + break + } + }; + } + + if false { + let _: ! = { //~ ERROR mismatched types + while false { + return + } + }; + } } diff --git a/src/test/ui/break-while-condition.stderr b/src/test/ui/break-while-condition.stderr index 5abf60c86d306..e3564297bf2ef 100644 --- a/src/test/ui/break-while-condition.stderr +++ b/src/test/ui/break-while-condition.stderr @@ -1,15 +1,43 @@ error[E0308]: mismatched types - --> $DIR/break-while-condition.rs:14:16 + --> $DIR/break-while-condition.rs:19:20 | -LL | let _: ! = { //~ ERROR mismatched types - | ________________^ -LL | | 'a: while break 'a {}; -LL | | }; - | |_____^ expected !, found () +LL | let _: ! = { //~ ERROR mismatched types + | ____________________^ +LL | | 'a: while break 'a {}; +LL | | }; + | |_________^ expected !, found () | = note: expected type `!` found type `()` -error: aborting due to previous error +error[E0308]: mismatched types + --> $DIR/break-while-condition.rs:26:13 + | +LL | fn main() { + | - expected `()` because of default return type +... +LL | / while false { +LL | | break +LL | | } + | |_____________^ expected !, found () + | + = note: expected type `!` + found type `()` + +error[E0308]: mismatched types + --> $DIR/break-while-condition.rs:34:13 + | +LL | fn main() { + | - expected `()` because of default return type +... +LL | / while false { +LL | | return +LL | | } + | |_____________^ expected !, found () + | + = note: expected type `!` + found type `()` + +error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0308`. From d5bf4de0e4c86d5cacd5ca738e1ba65afc5586ca Mon Sep 17 00:00:00 2001 From: varkor Date: Fri, 25 May 2018 17:31:45 +0100 Subject: [PATCH 3/3] --bless the tests --- src/test/ui/break-while-condition.rs | 8 ++++---- src/test/ui/break-while-condition.stderr | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/ui/break-while-condition.rs b/src/test/ui/break-while-condition.rs index 5949b29989324..050b479d485f6 100644 --- a/src/test/ui/break-while-condition.rs +++ b/src/test/ui/break-while-condition.rs @@ -22,16 +22,16 @@ fn main() { } if false { - let _: ! = { //~ ERROR mismatched types - while false { + let _: ! = { + while false { //~ ERROR mismatched types break } }; } if false { - let _: ! = { //~ ERROR mismatched types - while false { + let _: ! = { + while false { //~ ERROR mismatched types return } }; diff --git a/src/test/ui/break-while-condition.stderr b/src/test/ui/break-while-condition.stderr index e3564297bf2ef..c8f06db960392 100644 --- a/src/test/ui/break-while-condition.stderr +++ b/src/test/ui/break-while-condition.stderr @@ -16,7 +16,7 @@ error[E0308]: mismatched types LL | fn main() { | - expected `()` because of default return type ... -LL | / while false { +LL | / while false { //~ ERROR mismatched types LL | | break LL | | } | |_____________^ expected !, found () @@ -30,7 +30,7 @@ error[E0308]: mismatched types LL | fn main() { | - expected `()` because of default return type ... -LL | / while false { +LL | / while false { //~ ERROR mismatched types LL | | return LL | | } | |_____________^ expected !, found ()