From 7fdf06cdde49c8cd87e34e85a95c1fe90ebdb0c3 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Sun, 26 Aug 2018 15:19:34 +0200 Subject: Report const eval error inside the query --- src/librustc_codegen_llvm/mir/constant.rs | 19 +++++++++---------- src/librustc_codegen_llvm/mir/operand.rs | 16 ++++++++++------ 2 files changed, 19 insertions(+), 16 deletions(-) (limited to 'src/librustc_codegen_llvm') diff --git a/src/librustc_codegen_llvm/mir/constant.rs b/src/librustc_codegen_llvm/mir/constant.rs index 9f0f7443890..1f66ee36170 100644 --- a/src/librustc_codegen_llvm/mir/constant.rs +++ b/src/librustc_codegen_llvm/mir/constant.rs @@ -9,12 +9,11 @@ // except according to those terms. use llvm; -use rustc::mir::interpret::{ConstEvalErr, read_target_uint}; +use rustc::mir::interpret::{ErrorHandled, read_target_uint}; use rustc_mir::const_eval::const_field; use rustc::hir::def_id::DefId; use rustc::mir; use rustc_data_structures::indexed_vec::Idx; -use rustc_data_structures::sync::Lrc; use rustc::mir::interpret::{GlobalId, Pointer, Scalar, Allocation, ConstValue, AllocType}; use rustc::ty::{self, Ty}; use rustc::ty::layout::{self, HasDataLayout, LayoutOf, Size}; @@ -124,7 +123,7 @@ pub fn const_alloc_to_llvm(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) -> &'ll pub fn codegen_static_initializer( cx: &CodegenCx<'ll, 'tcx>, def_id: DefId, -) -> Result<(&'ll Value, &'tcx Allocation), Lrc>> { +) -> Result<(&'ll Value, &'tcx Allocation), ErrorHandled> { let instance = ty::Instance::mono(cx.tcx, def_id); let cid = GlobalId { instance, @@ -145,7 +144,7 @@ impl FunctionCx<'a, 'll, 'tcx> { &mut self, bx: &Builder<'a, 'll, 'tcx>, constant: &'tcx ty::Const<'tcx>, - ) -> Result<&'tcx ty::Const<'tcx>, Lrc>> { + ) -> Result<&'tcx ty::Const<'tcx>, ErrorHandled> { match constant.val { ConstValue::Unevaluated(def_id, ref substs) => { let tcx = bx.tcx(); @@ -165,7 +164,7 @@ impl FunctionCx<'a, 'll, 'tcx> { &mut self, bx: &Builder<'a, 'll, 'tcx>, constant: &mir::Constant<'tcx>, - ) -> Result<&'tcx ty::Const<'tcx>, Lrc>> { + ) -> Result<&'tcx ty::Const<'tcx>, ErrorHandled> { let c = self.monomorphize(&constant.literal); self.fully_evaluate(bx, c) } @@ -176,7 +175,7 @@ impl FunctionCx<'a, 'll, 'tcx> { bx: &Builder<'a, 'll, 'tcx>, span: Span, ty: Ty<'tcx>, - constant: Result<&'tcx ty::Const<'tcx>, Lrc>>, + constant: Result<&'tcx ty::Const<'tcx>, ErrorHandled>, ) -> (&'ll Value, Ty<'tcx>) { constant .and_then(|c| { @@ -185,7 +184,7 @@ impl FunctionCx<'a, 'll, 'tcx> { ty::Array(_, n) => n.unwrap_usize(bx.tcx()), ref other => bug!("invalid simd shuffle type: {}", other), }; - let values: Result, Lrc<_>> = (0..fields).map(|field| { + let values: Result, ErrorHandled> = (0..fields).map(|field| { let field = const_field( bx.tcx(), ty::ParamEnv::reveal_all(), @@ -211,9 +210,9 @@ impl FunctionCx<'a, 'll, 'tcx> { let llval = C_struct(bx.cx, &values?, false); Ok((llval, c.ty)) }) - .unwrap_or_else(|e| { - e.report_as_error( - bx.tcx().at(span), + .unwrap_or_else(|_| { + bx.tcx().sess.span_err( + span, "could not evaluate shuffle_indices at compile time", ); // We've errored, so we don't have to produce working code. diff --git a/src/librustc_codegen_llvm/mir/operand.rs b/src/librustc_codegen_llvm/mir/operand.rs index ab43531240f..25f03cbe970 100644 --- a/src/librustc_codegen_llvm/mir/operand.rs +++ b/src/librustc_codegen_llvm/mir/operand.rs @@ -8,11 +8,12 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use rustc::mir::interpret::{ConstValue, ConstEvalErr}; +use rustc::mir::interpret::{ConstValue, ErrorHandled}; use rustc::mir; use rustc::ty; use rustc::ty::layout::{self, Align, LayoutOf, TyLayout}; use rustc_data_structures::sync::Lrc; +use rustc_data_structures::indexed_vec::Idx; use base; use common::{CodegenCx, C_undef, C_usize}; @@ -79,7 +80,7 @@ impl OperandRef<'ll, 'tcx> { pub fn from_const(bx: &Builder<'a, 'll, 'tcx>, val: &'tcx ty::Const<'tcx>) - -> Result, Lrc>> { + -> Result, ErrorHandled> { let layout = bx.cx.layout_of(val.ty); if layout.is_zst() { @@ -424,10 +425,13 @@ impl FunctionCx<'a, 'll, 'tcx> { self.eval_mir_constant(bx, constant) .and_then(|c| OperandRef::from_const(bx, c)) .unwrap_or_else(|err| { - err.report_as_error( - bx.tcx().at(constant.span), - "could not evaluate constant operand", - ); + match err { + // errored or at least linted + ErrorHandled::Reported => {}, + ErrorHandled::TooGeneric => { + bug!("codgen encountered polymorphic constant") + }, + } // Allow RalfJ to sleep soundly knowing that even refactorings that remove // the above error (or silence it under some conditions) will not cause UB let fnname = bx.cx.get_intrinsic(&("llvm.trap")); -- cgit 1.4.1-3-g733a5 From bf8471aeda0a00c37a4798942ef9d5ae551eef20 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 7 Sep 2018 10:53:31 +0200 Subject: Rebase fallout in ui output --- src/librustc_codegen_llvm/mir/operand.rs | 2 -- .../const-eval/const_panic_libcore_main.stderr | 6 +++--- src/test/ui/consts/const-eval/issue-52475.rs | 3 +-- src/test/ui/consts/const-eval/issue-52475.stderr | 24 +++++++--------------- src/test/ui/consts/const-int-unchecked.stderr | 4 ++-- 5 files changed, 13 insertions(+), 26 deletions(-) (limited to 'src/librustc_codegen_llvm') diff --git a/src/librustc_codegen_llvm/mir/operand.rs b/src/librustc_codegen_llvm/mir/operand.rs index 25f03cbe970..d1b6aa7fc42 100644 --- a/src/librustc_codegen_llvm/mir/operand.rs +++ b/src/librustc_codegen_llvm/mir/operand.rs @@ -12,8 +12,6 @@ use rustc::mir::interpret::{ConstValue, ErrorHandled}; use rustc::mir; use rustc::ty; use rustc::ty::layout::{self, Align, LayoutOf, TyLayout}; -use rustc_data_structures::sync::Lrc; -use rustc_data_structures::indexed_vec::Idx; use base; use common::{CodegenCx, C_undef, C_usize}; diff --git a/src/test/ui/consts/const-eval/const_panic_libcore_main.stderr b/src/test/ui/consts/const-eval/const_panic_libcore_main.stderr index 9a7c510029c..50794c8f065 100644 --- a/src/test/ui/consts/const-eval/const_panic_libcore_main.stderr +++ b/src/test/ui/consts/const-eval/const_panic_libcore_main.stderr @@ -1,5 +1,5 @@ error: any use of this value will cause an error - --> $DIR/const_panic_libcore_main.rs:20:1 + --> $DIR/const_panic_libcore_main.rs:19:1 | LL | const Z: () = panic!("cheese"); | ^^^^^^^^^^^^^^----------------^ @@ -10,7 +10,7 @@ LL | const Z: () = panic!("cheese"); = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) error: any use of this value will cause an error - --> $DIR/const_panic_libcore_main.rs:23:1 + --> $DIR/const_panic_libcore_main.rs:22:1 | LL | const Y: () = unreachable!(); | ^^^^^^^^^^^^^^--------------^ @@ -20,7 +20,7 @@ LL | const Y: () = unreachable!(); = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) error: any use of this value will cause an error - --> $DIR/const_panic_libcore_main.rs:26:1 + --> $DIR/const_panic_libcore_main.rs:25:1 | LL | const X: () = unimplemented!(); | ^^^^^^^^^^^^^^----------------^ diff --git a/src/test/ui/consts/const-eval/issue-52475.rs b/src/test/ui/consts/const-eval/issue-52475.rs index 1c1e6535e69..b21c1827e2b 100644 --- a/src/test/ui/consts/const-eval/issue-52475.rs +++ b/src/test/ui/consts/const-eval/issue-52475.rs @@ -13,11 +13,10 @@ fn main() { let _ = [(); { //~^ WARNING Constant evaluating a complex constant, this might take some time - //~| ERROR could not evaluate repeat length let mut x = &0; let mut n = 0; while n < 5 { //~ ERROR constant contains unimplemented expression type - n = (n + 1) % 5; + n = (n + 1) % 5; //~ ERROR evaluation of constant value failed x = &0; // Materialize a new AllocId } 0 diff --git a/src/test/ui/consts/const-eval/issue-52475.stderr b/src/test/ui/consts/const-eval/issue-52475.stderr index f45587f3f75..c0cd98b2fca 100644 --- a/src/test/ui/consts/const-eval/issue-52475.stderr +++ b/src/test/ui/consts/const-eval/issue-52475.stderr @@ -1,8 +1,8 @@ error[E0019]: constant contains unimplemented expression type - --> $DIR/issue-52475.rs:19:9 + --> $DIR/issue-52475.rs:18:9 | LL | / while n < 5 { //~ ERROR constant contains unimplemented expression type -LL | | n = (n + 1) % 5; +LL | | n = (n + 1) % 5; //~ ERROR evaluation of constant value failed LL | | x = &0; // Materialize a new AllocId LL | | } | |_________^ @@ -13,28 +13,18 @@ warning: Constant evaluating a complex constant, this might take some time LL | let _ = [(); { | __________________^ LL | | //~^ WARNING Constant evaluating a complex constant, this might take some time -LL | | //~| ERROR could not evaluate repeat length LL | | let mut x = &0; +LL | | let mut n = 0; ... | LL | | 0 LL | | }]; | |_____^ -error[E0080]: could not evaluate repeat length - --> $DIR/issue-52475.rs:14:18 +error[E0080]: evaluation of constant value failed + --> $DIR/issue-52475.rs:19:17 | -LL | let _ = [(); { - | __________________^ -LL | | //~^ WARNING Constant evaluating a complex constant, this might take some time -LL | | //~| ERROR could not evaluate repeat length -LL | | let mut x = &0; -... | -LL | | n = (n + 1) % 5; - | | ----------- duplicate interpreter state observed here, const evaluation will never terminate -... | -LL | | 0 -LL | | }]; - | |_____^ +LL | n = (n + 1) % 5; //~ ERROR evaluation of constant value failed + | ^^^^^^^^^^^ duplicate interpreter state observed here, const evaluation will never terminate error: aborting due to 2 previous errors diff --git a/src/test/ui/consts/const-int-unchecked.stderr b/src/test/ui/consts/const-int-unchecked.stderr index b8fd0facbc1..314a3e488a1 100644 --- a/src/test/ui/consts/const-int-unchecked.stderr +++ b/src/test/ui/consts/const-int-unchecked.stderr @@ -1,4 +1,4 @@ -error: this constant cannot be used +error: any use of this value will cause an error --> $DIR/const-int-unchecked.rs:15:1 | LL | const SHR: u8 = unsafe { intrinsics::unchecked_shr(5_u8, 8) }; @@ -8,7 +8,7 @@ LL | const SHR: u8 = unsafe { intrinsics::unchecked_shr(5_u8, 8) }; | = note: #[deny(const_err)] on by default -error: this constant cannot be used +error: any use of this value will cause an error --> $DIR/const-int-unchecked.rs:17:1 | LL | const SHL: u8 = unsafe { intrinsics::unchecked_shl(5_u8, 8) }; -- cgit 1.4.1-3-g733a5