Skip to content

Commit 49225b8

Browse files
committed
Delay specific span_bug() call until abort_if_errors()
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 rust-lang#23827, closes rust-lang#24356, closes rust-lang#23041, closes rust-lang#22897, closes rust-lang#23966, closes rust-lang#24013, and closes rust-lang#23729
1 parent 37cb1d4 commit 49225b8

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ fn visit_expr(rcx: &mut Rcx, expr: &ast::Expr) {
544544
if tcx.sess.has_errors() {
545545
// cannot run dropck; okay b/c in error state anyway.
546546
} else {
547-
tcx.sess.span_bug(expr.span, "cat_expr_unadjusted Errd");
547+
tcx.sess.delay_span_bug(expr.span, "cat_expr Errd");
548548
}
549549
}
550550
}
@@ -565,7 +565,7 @@ fn visit_expr(rcx: &mut Rcx, expr: &ast::Expr) {
565565
if tcx.sess.has_errors() {
566566
// cannot run dropck; okay b/c in error state anyway.
567567
} else {
568-
tcx.sess.span_bug(expr.span, "cat_expr Errd");
568+
tcx.sess.delay_span_bug(expr.span, "cat_expr Errd");
569569
}
570570
}
571571
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}
20+
21+
fn invalid_impl_within_scope() {
22+
{
23+
use std::ops::Deref;
24+
struct Something;
25+
26+
impl Deref for Something {}
27+
//~^ ERROR not all trait items implemented, missing: `Target`, `deref`
28+
29+
*Something
30+
};
31+
}

0 commit comments

Comments
 (0)