From 49225b8fbc8e171e7788571dac82977a3b3f227c Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Mon, 13 Apr 2015 01:13:09 -0600 Subject: [PATCH 1/4] 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 #23827, closes #24356, closes #23041, closes #22897, closes #23966, closes #24013, and closes #23729 --- src/librustc/session/mod.rs | 18 ++++++++++- src/librustc_typeck/check/regionck.rs | 4 +-- .../compile-fail/typeck-before-dropck-bug.rs | 31 +++++++++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/test/compile-fail/typeck-before-dropck-bug.rs diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index 500af5fc772f5..14bc19dffd5d0 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -57,6 +57,8 @@ pub struct Session { pub crate_metadata: RefCell>, pub features: RefCell, + pub delayed_span_bug: RefCell>, + /// The maximum recursion limit for potentially infinitely recursive /// operations such as auto-dereference and monomorphization. pub recursion_limit: Cell, @@ -114,7 +116,15 @@ impl Session { self.diagnostic().handler().has_errors() } pub fn abort_if_errors(&self) { - self.diagnostic().handler().abort_if_errors() + self.diagnostic().handler().abort_if_errors(); + + let delayed_bug = self.delayed_span_bug.borrow(); + match *delayed_bug { + Some((span, ref errmsg)) => { + self.diagnostic().span_bug(span, errmsg); + }, + _ => {} + } } pub fn span_warn(&self, sp: Span, msg: &str) { if self.can_print_warnings { @@ -171,6 +181,11 @@ impl Session { None => self.bug(msg), } } + /// Delay a span_bug() call until abort_if_errors() + pub fn delay_span_bug(&self, sp: Span, msg: &str) { + let mut delayed = self.delayed_span_bug.borrow_mut(); + *delayed = Some((sp, msg.to_string())); + } pub fn span_bug(&self, sp: Span, msg: &str) -> ! { self.diagnostic().span_bug(sp, msg) } @@ -402,6 +417,7 @@ pub fn build_session_(sopts: config::Options, plugin_llvm_passes: RefCell::new(Vec::new()), crate_types: RefCell::new(Vec::new()), crate_metadata: RefCell::new(Vec::new()), + delayed_span_bug: RefCell::new(None), features: RefCell::new(feature_gate::Features::new()), recursion_limit: Cell::new(64), can_print_warnings: can_print_warnings diff --git a/src/librustc_typeck/check/regionck.rs b/src/librustc_typeck/check/regionck.rs index 9554e6ad8aad3..55ad52dab952b 100644 --- a/src/librustc_typeck/check/regionck.rs +++ b/src/librustc_typeck/check/regionck.rs @@ -544,7 +544,7 @@ fn visit_expr(rcx: &mut Rcx, expr: &ast::Expr) { if tcx.sess.has_errors() { // cannot run dropck; okay b/c in error state anyway. } else { - tcx.sess.span_bug(expr.span, "cat_expr_unadjusted Errd"); + tcx.sess.delay_span_bug(expr.span, "cat_expr Errd"); } } } @@ -565,7 +565,7 @@ fn visit_expr(rcx: &mut Rcx, expr: &ast::Expr) { if tcx.sess.has_errors() { // cannot run dropck; okay b/c in error state anyway. } else { - tcx.sess.span_bug(expr.span, "cat_expr Errd"); + tcx.sess.delay_span_bug(expr.span, "cat_expr Errd"); } } } diff --git a/src/test/compile-fail/typeck-before-dropck-bug.rs b/src/test/compile-fail/typeck-before-dropck-bug.rs new file mode 100644 index 0000000000000..04ee993c04076 --- /dev/null +++ b/src/test/compile-fail/typeck-before-dropck-bug.rs @@ -0,0 +1,31 @@ +// Copyright 2015 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { } + +// Before these errors would ICE as "cat_expr Errd" because the errors +// were unknown when the bug was triggered. + +fn unconstrained_type() { + []; + //~^ ERROR cannot determine a type for this expression: unconstrained type +} + +fn invalid_impl_within_scope() { + { + use std::ops::Deref; + struct Something; + + impl Deref for Something {} + //~^ ERROR not all trait items implemented, missing: `Target`, `deref` + + *Something + }; +} From c6606091902daedcea77c1f68198f6c9b071a90b Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Mon, 13 Apr 2015 09:01:55 -0600 Subject: [PATCH 2/4] Leave error message intact --- src/librustc_typeck/check/regionck.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_typeck/check/regionck.rs b/src/librustc_typeck/check/regionck.rs index 55ad52dab952b..682aa6e6ee7d4 100644 --- a/src/librustc_typeck/check/regionck.rs +++ b/src/librustc_typeck/check/regionck.rs @@ -544,7 +544,7 @@ fn visit_expr(rcx: &mut Rcx, expr: &ast::Expr) { if tcx.sess.has_errors() { // cannot run dropck; okay b/c in error state anyway. } else { - tcx.sess.delay_span_bug(expr.span, "cat_expr Errd"); + tcx.sess.delay_span_bug(expr.span, "cat_expr_unadjusted Errd"); } } } From 8f41b74dba4c440dd55e6871407448a9478d5e24 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Mon, 13 Apr 2015 14:19:26 -0600 Subject: [PATCH 3/4] Completely delay this error checking --- src/librustc_typeck/check/regionck.rs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/librustc_typeck/check/regionck.rs b/src/librustc_typeck/check/regionck.rs index 682aa6e6ee7d4..3c8ad36138524 100644 --- a/src/librustc_typeck/check/regionck.rs +++ b/src/librustc_typeck/check/regionck.rs @@ -541,11 +541,7 @@ fn visit_expr(rcx: &mut Rcx, expr: &ast::Expr) { } Err(..) => { let tcx = rcx.fcx.tcx(); - if tcx.sess.has_errors() { - // cannot run dropck; okay b/c in error state anyway. - } else { - tcx.sess.delay_span_bug(expr.span, "cat_expr_unadjusted Errd"); - } + tcx.sess.delay_span_bug(expr.span, "cat_expr_unadjusted Errd"); } } } @@ -562,11 +558,7 @@ fn visit_expr(rcx: &mut Rcx, expr: &ast::Expr) { } Err(..) => { let tcx = rcx.fcx.tcx(); - if tcx.sess.has_errors() { - // cannot run dropck; okay b/c in error state anyway. - } else { - tcx.sess.delay_span_bug(expr.span, "cat_expr Errd"); - } + tcx.sess.delay_span_bug(expr.span, "cat_expr Errd"); } } From be1117113f60b8afedf90039b0dcb87148666567 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Fri, 24 Apr 2015 12:34:27 -0600 Subject: [PATCH 4/4] Split up tests, reduce coverage --- ...eck-before-dropck-bug.rs => issue-22897.rs} | 12 ------------ src/test/compile-fail/issue-23041.rs | 18 ++++++++++++++++++ src/test/compile-fail/issue-23966.rs | 14 ++++++++++++++ src/test/compile-fail/issue-24013.rs | 17 +++++++++++++++++ 4 files changed, 49 insertions(+), 12 deletions(-) rename src/test/compile-fail/{typeck-before-dropck-bug.rs => issue-22897.rs} (74%) create mode 100644 src/test/compile-fail/issue-23041.rs create mode 100644 src/test/compile-fail/issue-23966.rs create mode 100644 src/test/compile-fail/issue-24013.rs diff --git a/src/test/compile-fail/typeck-before-dropck-bug.rs b/src/test/compile-fail/issue-22897.rs similarity index 74% rename from src/test/compile-fail/typeck-before-dropck-bug.rs rename to src/test/compile-fail/issue-22897.rs index 04ee993c04076..c6bbf2d4abcc0 100644 --- a/src/test/compile-fail/typeck-before-dropck-bug.rs +++ b/src/test/compile-fail/issue-22897.rs @@ -17,15 +17,3 @@ fn unconstrained_type() { []; //~^ ERROR cannot determine a type for this expression: unconstrained type } - -fn invalid_impl_within_scope() { - { - use std::ops::Deref; - struct Something; - - impl Deref for Something {} - //~^ ERROR not all trait items implemented, missing: `Target`, `deref` - - *Something - }; -} diff --git a/src/test/compile-fail/issue-23041.rs b/src/test/compile-fail/issue-23041.rs new file mode 100644 index 0000000000000..68895759c5c2a --- /dev/null +++ b/src/test/compile-fail/issue-23041.rs @@ -0,0 +1,18 @@ +// Copyright 2015 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::any::Any; +fn main() +{ + fn bar(x:i32) ->i32 { 3*x }; + let b:Box = Box::new(bar as fn(_)->_); + b.downcast_ref::_>(); + //~^ ERROR cannot determine a type for this expression: unconstrained type +} diff --git a/src/test/compile-fail/issue-23966.rs b/src/test/compile-fail/issue-23966.rs new file mode 100644 index 0000000000000..18b5136866521 --- /dev/null +++ b/src/test/compile-fail/issue-23966.rs @@ -0,0 +1,14 @@ +// Copyright 2015 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + "".chars().fold(|_, _| (), ()); + //~^ ERROR cannot determine a type for this expression: unconstrained type +} diff --git a/src/test/compile-fail/issue-24013.rs b/src/test/compile-fail/issue-24013.rs new file mode 100644 index 0000000000000..0adad8a88cbc9 --- /dev/null +++ b/src/test/compile-fail/issue-24013.rs @@ -0,0 +1,17 @@ +// Copyright 2015 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + use std::mem::{transmute, swap}; + let a = 1; + let b = 2; + unsafe {swap::<&mut _>(transmute(&a), transmute(&b))}; + //~^ ERROR cannot determine a type for this expression: unconstrained type +}