diff options
| author | kennytm <kennytm@gmail.com> | 2017-08-08 23:28:09 +0800 |
|---|---|---|
| committer | kennytm <kennytm@gmail.com> | 2017-08-09 00:30:26 +0800 |
| commit | 3cb23a714f2b7adbf1eee697fb6471c4dd733f04 (patch) | |
| tree | 46e53a69ca276d053f07ad4ee854ce921993db07 | |
| parent | 6d84a355c34fc7dfc1a201478f99890046081398 (diff) | |
| download | rust-3cb23a714f2b7adbf1eee697fb6471c4dd733f04.tar.gz rust-3cb23a714f2b7adbf1eee697fb6471c4dd733f04.zip | |
Type-check `break value;` even outside of `loop {}`.
Fix #43162, fix #43727.
| -rw-r--r-- | src/librustc_typeck/check/mod.rs | 31 | ||||
| -rw-r--r-- | src/test/compile-fail/issue-43162.rs | 17 | ||||
| -rw-r--r-- | src/test/run-pass/loop-break-value.rs | 6 |
3 files changed, 54 insertions, 0 deletions
diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index e53e5e7b08c..f146742f5ae 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3652,6 +3652,20 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { // inside a loop at all, which is caught by the // loop-checking pass. assert!(self.tcx.sess.err_count() > 0); + + // We still need to assign a type to the inner expression to + // prevent the ICE in #43162. + if let Some(ref e) = *expr_opt { + self.check_expr_with_hint(e, tcx.types.err); + + // ... except when we try to 'break rust;'. + // ICE this expression in particular (see #43162). + if let hir::ExprPath(hir::QPath::Resolved(_, ref path)) = e.node { + if path.segments.len() == 1 && path.segments[0].name == "rust" { + fatally_break_rust(self.tcx.sess); + } + } + } } // the type of a `break` is always `!`, since it diverges @@ -4857,3 +4871,20 @@ pub fn check_bounds_are_used<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, } } } + +fn fatally_break_rust(sess: &Session) { + let handler = sess.diagnostic(); + handler.span_bug_no_panic( + MultiSpan::new(), + "It looks like you're trying to break rust; would you like some ICE?", + ); + handler.note_without_error("the compiler expectedly panicked. this is a feature."); + handler.note_without_error( + "we would appreciate a joke overview: \ + https://github.com/rust-lang/rust/issues/43162#issuecomment-320764675" + ); + handler.note_without_error(&format!("rustc {} running on {}", + option_env!("CFG_VERSION").unwrap_or("unknown_version"), + ::session::config::host_triple(), + )); +} diff --git a/src/test/compile-fail/issue-43162.rs b/src/test/compile-fail/issue-43162.rs new file mode 100644 index 00000000000..8f4661299e9 --- /dev/null +++ b/src/test/compile-fail/issue-43162.rs @@ -0,0 +1,17 @@ +// Copyright 2017 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 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn foo() -> bool { + break true; //~ ERROR E0268 +} + +fn main() { + break {}; //~ ERROR E0268 +} diff --git a/src/test/run-pass/loop-break-value.rs b/src/test/run-pass/loop-break-value.rs index 1d5c83bc20d..39053769b24 100644 --- a/src/test/run-pass/loop-break-value.rs +++ b/src/test/run-pass/loop-break-value.rs @@ -137,4 +137,10 @@ pub fn main() { panic!("from outer"); }; assert_eq!(break_from_while_to_outer, 567); + + let rust = true; + let value = loop { + break rust; + }; + assert!(value); } |
