Skip to content

Commit 3cb23a7

Browse files
committed
Type-check break value; even outside of loop {}.
Fix rust-lang#43162, fix rust-lang#43727.
1 parent 6d84a35 commit 3cb23a7

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
@@ -4857,3 +4871,20 @@ pub fn check_bounds_are_used<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
48574871
}
48584872
}
48594873
}
4874+
4875+
fn fatally_break_rust(sess: &Session) {
4876+
let handler = sess.diagnostic();
4877+
handler.span_bug_no_panic(
4878+
MultiSpan::new(),
4879+
"It looks like you're trying to break rust; would you like some ICE?",
4880+
);
4881+
handler.note_without_error("the compiler expectedly panicked. this is a feature.");
4882+
handler.note_without_error(
4883+
"we would appreciate a joke overview: \
4884+
https://github.com/rust-lang/rust/issues/43162#issuecomment-320764675"
4885+
);
4886+
handler.note_without_error(&format!("rustc {} running on {}",
4887+
option_env!("CFG_VERSION").unwrap_or("unknown_version"),
4888+
::session::config::host_triple(),
4889+
));
4890+
}

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)