Skip to content

Commit b0043db

Browse files
committed
Auto merge of #24367 - ebfull:fix_ice_cat_expr, r=pnkfelix
An actual typeck error is the cause of many failed compilations but an unrelated bug is being reported instead. It is triggered because a typeck error is presumably not yet identified during compiler execution, which would normally bypass an invariant in the presence of other errors. In this particular situation, we delay the reporting of the bug until abort_if_errors(). Closes #23827, closes #24356, closes #23041, closes #22897, closes #23966, closes #24013, and closes #23729 **There is at least one situation where this bug may still be genuinely triggered (#23437).**
2 parents 61a5e46 + be11171 commit b0043db

File tree

6 files changed

+87
-11
lines changed

6 files changed

+87
-11
lines changed

src/librustc/session/mod.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ pub struct Session {
5757
pub crate_metadata: RefCell<Vec<String>>,
5858
pub features: RefCell<feature_gate::Features>,
5959

60+
pub delayed_span_bug: RefCell<Option<(codemap::Span, String)>>,
61+
6062
/// The maximum recursion limit for potentially infinitely recursive
6163
/// operations such as auto-dereference and monomorphization.
6264
pub recursion_limit: Cell<usize>,
@@ -114,7 +116,15 @@ impl Session {
114116
self.diagnostic().handler().has_errors()
115117
}
116118
pub fn abort_if_errors(&self) {
117-
self.diagnostic().handler().abort_if_errors()
119+
self.diagnostic().handler().abort_if_errors();
120+
121+
let delayed_bug = self.delayed_span_bug.borrow();
122+
match *delayed_bug {
123+
Some((span, ref errmsg)) => {
124+
self.diagnostic().span_bug(span, errmsg);
125+
},
126+
_ => {}
127+
}
118128
}
119129
pub fn span_warn(&self, sp: Span, msg: &str) {
120130
if self.can_print_warnings {
@@ -171,6 +181,11 @@ impl Session {
171181
None => self.bug(msg),
172182
}
173183
}
184+
/// Delay a span_bug() call until abort_if_errors()
185+
pub fn delay_span_bug(&self, sp: Span, msg: &str) {
186+
let mut delayed = self.delayed_span_bug.borrow_mut();
187+
*delayed = Some((sp, msg.to_string()));
188+
}
174189
pub fn span_bug(&self, sp: Span, msg: &str) -> ! {
175190
self.diagnostic().span_bug(sp, msg)
176191
}
@@ -402,6 +417,7 @@ pub fn build_session_(sopts: config::Options,
402417
plugin_llvm_passes: RefCell::new(Vec::new()),
403418
crate_types: RefCell::new(Vec::new()),
404419
crate_metadata: RefCell::new(Vec::new()),
420+
delayed_span_bug: RefCell::new(None),
405421
features: RefCell::new(feature_gate::Features::new()),
406422
recursion_limit: Cell::new(64),
407423
can_print_warnings: can_print_warnings

src/librustc_typeck/check/regionck.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -562,11 +562,7 @@ fn visit_expr(rcx: &mut Rcx, expr: &ast::Expr) {
562562
}
563563
Err(..) => {
564564
let tcx = rcx.fcx.tcx();
565-
if tcx.sess.has_errors() {
566-
// cannot run dropck; okay b/c in error state anyway.
567-
} else {
568-
tcx.sess.span_bug(expr.span, "cat_expr_unadjusted Errd");
569-
}
565+
tcx.sess.delay_span_bug(expr.span, "cat_expr_unadjusted Errd");
570566
}
571567
}
572568
}
@@ -583,11 +579,7 @@ fn visit_expr(rcx: &mut Rcx, expr: &ast::Expr) {
583579
}
584580
Err(..) => {
585581
let tcx = rcx.fcx.tcx();
586-
if tcx.sess.has_errors() {
587-
// cannot run dropck; okay b/c in error state anyway.
588-
} else {
589-
tcx.sess.span_bug(expr.span, "cat_expr Errd");
590-
}
582+
tcx.sess.delay_span_bug(expr.span, "cat_expr Errd");
591583
}
592584
}
593585

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2015 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 main() { }
12+
13+
// Before these errors would ICE as "cat_expr Errd" because the errors
14+
// were unknown when the bug was triggered.
15+
16+
fn unconstrained_type() {
17+
[];
18+
//~^ ERROR cannot determine a type for this expression: unconstrained type
19+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2015 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+
use std::any::Any;
12+
fn main()
13+
{
14+
fn bar(x:i32) ->i32 { 3*x };
15+
let b:Box<Any> = Box::new(bar as fn(_)->_);
16+
b.downcast_ref::<fn(_)->_>();
17+
//~^ ERROR cannot determine a type for this expression: unconstrained type
18+
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2015 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 main() {
12+
"".chars().fold(|_, _| (), ());
13+
//~^ ERROR cannot determine a type for this expression: unconstrained type
14+
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2015 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 main() {
12+
use std::mem::{transmute, swap};
13+
let a = 1;
14+
let b = 2;
15+
unsafe {swap::<&mut _>(transmute(&a), transmute(&b))};
16+
//~^ ERROR cannot determine a type for this expression: unconstrained type
17+
}

0 commit comments

Comments
 (0)