Skip to content

Commit 9868352

Browse files
committed
Auto merge of rust-lang#43745 - kennytm:fix-43162, r=aturon
Type-check `break value;` even outside of `loop {}`. Fix rust-lang#43162, fix rust-lang#43727.
2 parents 73c3f55 + 3cb23a7 commit 9868352

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/librustc_typeck/check/mod.rs

+31
Original file line numberDiff line numberDiff line change
@@ -3652,6 +3652,20 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
36523652
// inside a loop at all, which is caught by the
36533653
// loop-checking pass.
36543654
assert!(self.tcx.sess.err_count() > 0);
3655+
3656+
// We still need to assign a type to the inner expression to
3657+
// prevent the ICE in #43162.
3658+
if let Some(ref e) = *expr_opt {
3659+
self.check_expr_with_hint(e, tcx.types.err);
3660+
3661+
// ... except when we try to 'break rust;'.
3662+
// ICE this expression in particular (see #43162).
3663+
if let hir::ExprPath(hir::QPath::Resolved(_, ref path)) = e.node {
3664+
if path.segments.len() == 1 && path.segments[0].name == "rust" {
3665+
fatally_break_rust(self.tcx.sess);
3666+
}
3667+
}
3668+
}
36553669
}
36563670

36573671
// the type of a `break` is always `!`, since it diverges
@@ -4880,3 +4894,20 @@ pub fn check_bounds_are_used<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
48804894
}
48814895
}
48824896
}
4897+
4898+
fn fatally_break_rust(sess: &Session) {
4899+
let handler = sess.diagnostic();
4900+
handler.span_bug_no_panic(
4901+
MultiSpan::new(),
4902+
"It looks like you're trying to break rust; would you like some ICE?",
4903+
);
4904+
handler.note_without_error("the compiler expectedly panicked. this is a feature.");
4905+
handler.note_without_error(
4906+
"we would appreciate a joke overview: \
4907+
https://github.com/rust-lang/rust/issues/43162#issuecomment-320764675"
4908+
);
4909+
handler.note_without_error(&format!("rustc {} running on {}",
4910+
option_env!("CFG_VERSION").unwrap_or("unknown_version"),
4911+
::session::config::host_triple(),
4912+
));
4913+
}

src/test/compile-fail/issue-43162.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn foo() -> bool {
12+
break true; //~ ERROR E0268
13+
}
14+
15+
fn main() {
16+
break {}; //~ ERROR E0268
17+
}

src/test/run-pass/loop-break-value.rs

+6
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,10 @@ pub fn main() {
137137
panic!("from outer");
138138
};
139139
assert_eq!(break_from_while_to_outer, 567);
140+
141+
let rust = true;
142+
let value = loop {
143+
break rust;
144+
};
145+
assert!(value);
140146
}

0 commit comments

Comments
 (0)