From 2c836a7ebd3b32bff4c78daeb04ac4a5fe0fa165 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Wed, 1 Aug 2018 15:29:13 +0200 Subject: [PATCH] Fallout from fixing `try_read_value` to work with enums --- src/librustc_mir/interpret/const_eval.rs | 51 +++++++------------ src/test/ui/const-eval/ub-enum-ptr.rs | 2 +- src/test/ui/const-eval/ub-enum-ptr.stderr | 7 ++- .../ui/const-eval/union-const-eval-field.rs | 2 +- .../const-eval/union-const-eval-field.stderr | 9 ++-- src/test/ui/const-eval/union-ice.rs | 4 +- src/test/ui/const-eval/union-ice.stderr | 16 +++--- 7 files changed, 35 insertions(+), 56 deletions(-) diff --git a/src/librustc_mir/interpret/const_eval.rs b/src/librustc_mir/interpret/const_eval.rs index b28858b0cf01..121812e87138 100644 --- a/src/librustc_mir/interpret/const_eval.rs +++ b/src/librustc_mir/interpret/const_eval.rs @@ -5,7 +5,7 @@ use rustc::hir; use rustc::mir::interpret::{ConstEvalErr, ScalarMaybeUndef}; use rustc::mir; use rustc::ty::{self, TyCtxt, Ty, Instance}; -use rustc::ty::layout::{self, LayoutOf, Primitive, TyLayout, Size}; +use rustc::ty::layout::{self, LayoutOf, Primitive, TyLayout}; use rustc::ty::subst::Subst; use rustc_data_structures::indexed_vec::IndexVec; @@ -77,7 +77,7 @@ pub fn value_to_const_value<'tcx>( ecx: &EvalContext<'_, '_, 'tcx, CompileTimeEvaluator>, val: Value, layout: TyLayout<'tcx>, -) -> &'tcx ty::Const<'tcx> { +) -> EvalResult<'tcx, &'tcx ty::Const<'tcx>> { match (val, &layout.abi) { (Value::Scalar(ScalarMaybeUndef::Scalar(Scalar::Bits { size: 0, ..})), _) if layout.is_zst() => {}, (Value::ByRef(..), _) | @@ -85,38 +85,21 @@ pub fn value_to_const_value<'tcx>( (Value::ScalarPair(..), &layout::Abi::ScalarPair(..)) => {}, _ => bug!("bad value/layout combo: {:#?}, {:#?}", val, layout), } - let val = (|| { - match val { - Value::Scalar(val) => Ok(ConstValue::Scalar(val.unwrap_or_err()?)), - Value::ScalarPair(a, b) => Ok(ConstValue::ScalarPair(a.unwrap_or_err()?, b.unwrap_or_err()?)), - Value::ByRef(ptr, align) => { - let ptr = ptr.to_ptr().unwrap(); - let alloc = ecx.memory.get(ptr.alloc_id)?; - assert!(alloc.align.abi() >= align.abi()); - assert!(alloc.bytes.len() as u64 - ptr.offset.bytes() >= layout.size.bytes()); - let mut alloc = alloc.clone(); - alloc.align = align; - let alloc = ecx.tcx.intern_const_alloc(alloc); - Ok(ConstValue::ByRef(alloc, ptr.offset)) - } - } - })(); - match val { - Ok(val) => ty::Const::from_const_value(ecx.tcx.tcx, val, layout.ty), - Err(error) => { - let (stacktrace, span) = ecx.generate_stacktrace(None); - let err = ConstEvalErr { span, error, stacktrace }; - if let Some(mut err) = err.struct_error(ecx.tcx, "failed to convert Value to ConstValue") { - err.delay_as_bug(); - } else { - span_bug!(span, "failed to convert Value to ConstValue") - } - let alloc = Allocation::undef(layout.size, layout.align); + let val = match val { + Value::Scalar(val) => ConstValue::Scalar(val.unwrap_or_err()?), + Value::ScalarPair(a, b) => ConstValue::ScalarPair(a.unwrap_or_err()?, b.unwrap_or_err()?), + Value::ByRef(ptr, align) => { + let ptr = ptr.to_ptr().unwrap(); + let alloc = ecx.memory.get(ptr.alloc_id)?; + assert!(alloc.align.abi() >= align.abi()); + assert!(alloc.bytes.len() as u64 - ptr.offset.bytes() >= layout.size.bytes()); + let mut alloc = alloc.clone(); + alloc.align = align; let alloc = ecx.tcx.intern_const_alloc(alloc); - let val = ConstValue::ByRef(alloc, Size::ZERO); - ty::Const::from_const_value(ecx.tcx.tcx, val, layout.ty) + ConstValue::ByRef(alloc, ptr.offset) } - } + }; + Ok(ty::Const::from_const_value(ecx.tcx.tcx, val, layout.ty)) } fn eval_body_and_ecx<'a, 'mir, 'tcx>( @@ -454,7 +437,7 @@ pub fn const_val_field<'a, 'tcx>( ), _ => {}, } - Ok(value_to_const_value(&ecx, new_value, layout)) + value_to_const_value(&ecx, new_value, layout) })(); result.map_err(|err| { let (trace, span) = ecx.generate_stacktrace(None); @@ -555,7 +538,7 @@ pub fn const_eval_provider<'a, 'tcx>( if tcx.is_static(def_id).is_none() && cid.promoted.is_none() { val = ecx.try_read_by_ref(val, layout.ty)?; } - Ok(value_to_const_value(&ecx, val, layout)) + value_to_const_value(&ecx, val, layout) }).map_err(|err| { let (trace, span) = ecx.generate_stacktrace(None); let err = ConstEvalErr { diff --git a/src/test/ui/const-eval/ub-enum-ptr.rs b/src/test/ui/const-eval/ub-enum-ptr.rs index 8538dd14afed..66eda13a7344 100644 --- a/src/test/ui/const-eval/ub-enum-ptr.rs +++ b/src/test/ui/const-eval/ub-enum-ptr.rs @@ -21,7 +21,7 @@ union Foo { // A pointer is guaranteed non-null const BAD_ENUM: Enum = unsafe { Foo { a: &1 }.b}; -//~^ ERROR this constant likely exhibits undefined behavior +//~^ ERROR this constant cannot be used fn main() { } diff --git a/src/test/ui/const-eval/ub-enum-ptr.stderr b/src/test/ui/const-eval/ub-enum-ptr.stderr index 4b7ccc25c6c0..a0aa459db3f9 100644 --- a/src/test/ui/const-eval/ub-enum-ptr.stderr +++ b/src/test/ui/const-eval/ub-enum-ptr.stderr @@ -1,11 +1,10 @@ -error[E0080]: this constant likely exhibits undefined behavior +error: this constant cannot be used --> $DIR/ub-enum-ptr.rs:23:1 | LL | const BAD_ENUM: Enum = unsafe { Foo { a: &1 }.b}; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer at .TAG, but expected something in the range 0..=0 + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ a raw memory access tried to access part of a pointer value as raw bytes | - = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior + = note: #[deny(const_err)] on by default error: aborting due to previous error -For more information about this error, try `rustc --explain E0080`. diff --git a/src/test/ui/const-eval/union-const-eval-field.rs b/src/test/ui/const-eval/union-const-eval-field.rs index 41981e125679..5723f4a4159a 100644 --- a/src/test/ui/const-eval/union-const-eval-field.rs +++ b/src/test/ui/const-eval/union-const-eval-field.rs @@ -34,7 +34,7 @@ const fn read_field2() -> Field2 { } const fn read_field3() -> Field3 { - const FIELD3: Field3 = unsafe { UNION.field3 }; //~ ERROR exhibits undefined behavior + const FIELD3: Field3 = unsafe { UNION.field3 }; //~ ERROR cannot be used FIELD3 } diff --git a/src/test/ui/const-eval/union-const-eval-field.stderr b/src/test/ui/const-eval/union-const-eval-field.stderr index 94896d6c2257..811450c8cba4 100644 --- a/src/test/ui/const-eval/union-const-eval-field.stderr +++ b/src/test/ui/const-eval/union-const-eval-field.stderr @@ -1,11 +1,10 @@ -error[E0080]: this constant likely exhibits undefined behavior +error: this constant cannot be used --> $DIR/union-const-eval-field.rs:37:5 | -LL | const FIELD3: Field3 = unsafe { UNION.field3 }; //~ ERROR exhibits undefined behavior - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered undefined bytes +LL | const FIELD3: Field3 = unsafe { UNION.field3 }; //~ ERROR cannot be used + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempted to read undefined bytes | - = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior + = note: #[deny(const_err)] on by default error: aborting due to previous error -For more information about this error, try `rustc --explain E0080`. diff --git a/src/test/ui/const-eval/union-ice.rs b/src/test/ui/const-eval/union-ice.rs index 426710389eb2..0cdb78c97803 100644 --- a/src/test/ui/const-eval/union-ice.rs +++ b/src/test/ui/const-eval/union-ice.rs @@ -20,9 +20,9 @@ union DummyUnion { const UNION: DummyUnion = DummyUnion { field1: 1065353216 }; -const FIELD3: Field3 = unsafe { UNION.field3 }; //~ ERROR this constant likely exhibits undefined +const FIELD3: Field3 = unsafe { UNION.field3 }; //~ ERROR this constant cannot be used -const FIELD_PATH: Struct = Struct { //~ ERROR this constant likely exhibits undefined behavior +const FIELD_PATH: Struct = Struct { //~ ERROR this constant cannot be used a: 42, b: unsafe { UNION.field3 }, }; diff --git a/src/test/ui/const-eval/union-ice.stderr b/src/test/ui/const-eval/union-ice.stderr index 58e9033a071a..e8a7b2f50056 100644 --- a/src/test/ui/const-eval/union-ice.stderr +++ b/src/test/ui/const-eval/union-ice.stderr @@ -1,21 +1,19 @@ -error[E0080]: this constant likely exhibits undefined behavior +error: this constant cannot be used --> $DIR/union-ice.rs:23:1 | -LL | const FIELD3: Field3 = unsafe { UNION.field3 }; //~ ERROR this constant likely exhibits undefined - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered undefined bytes +LL | const FIELD3: Field3 = unsafe { UNION.field3 }; //~ ERROR this constant cannot be used + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempted to read undefined bytes | - = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior + = note: #[deny(const_err)] on by default -error[E0080]: this constant likely exhibits undefined behavior +error: this constant cannot be used --> $DIR/union-ice.rs:25:1 | -LL | / const FIELD_PATH: Struct = Struct { //~ ERROR this constant likely exhibits undefined behavior +LL | / const FIELD_PATH: Struct = Struct { //~ ERROR this constant cannot be used LL | | a: 42, LL | | b: unsafe { UNION.field3 }, LL | | }; - | |__^ type validation failed: encountered undefined bytes at .b - | - = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior + | |__^ attempted to read undefined bytes error[E0080]: this constant likely exhibits undefined behavior --> $DIR/union-ice.rs:35:1