From c8e30c429503de90f7bad8a4c336af11ffaa48e6 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Thu, 2 Aug 2018 10:19:37 +0200 Subject: [PATCH] Reading values should not be looking at the variant --- src/librustc_mir/interpret/eval_context.rs | 17 ++--------------- src/test/ui/const-eval/ub-enum-ptr.rs | 2 +- src/test/ui/const-eval/ub-enum-ptr.stderr | 7 ++++--- 3 files changed, 7 insertions(+), 19 deletions(-) diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs index b4c9d15715097..1d973e2091fad 100644 --- a/src/librustc_mir/interpret/eval_context.rs +++ b/src/librustc_mir/interpret/eval_context.rs @@ -1521,7 +1521,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M } pub fn try_read_value(&self, ptr: Scalar, ptr_align: Align, ty: Ty<'tcx>) -> EvalResult<'tcx, Option> { - let mut layout = self.layout_of(ty)?; + let layout = self.layout_of(ty)?; self.memory.check_align(ptr, ptr_align)?; if layout.size.bytes() == 0 { @@ -1530,19 +1530,6 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M let ptr = ptr.to_ptr()?; - match layout.variants { - layout::Variants::NicheFilling { .. } | - layout::Variants::Tagged { .. } => { - let variant_index = self.read_discriminant_as_variant_index( - Place::from_ptr(ptr, ptr_align), - layout, - )?; - layout = layout.for_variant(self, variant_index); - trace!("variant layout: {:#?}", layout); - }, - layout::Variants::Single { .. } => {}, - } - match layout.abi { layout::Abi::Scalar(..) => { let scalar = self.memory.read_scalar(ptr, ptr_align, layout.size)?; @@ -1558,7 +1545,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M let b_val = self.memory.read_scalar(b_ptr, ptr_align, b_size)?; Ok(Some(Value::ScalarPair(a_val, b_val))) } - _ => Ok(None), + _ => Ok(None), } } diff --git a/src/test/ui/const-eval/ub-enum-ptr.rs b/src/test/ui/const-eval/ub-enum-ptr.rs index 66eda13a7344a..8538dd14afed1 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 cannot be used +//~^ ERROR this constant likely exhibits undefined behavior 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 a0aa459db3f91..4b7ccc25c6c01 100644 --- a/src/test/ui/const-eval/ub-enum-ptr.stderr +++ b/src/test/ui/const-eval/ub-enum-ptr.stderr @@ -1,10 +1,11 @@ -error: this constant cannot be used +error[E0080]: this constant likely exhibits undefined behavior --> $DIR/ub-enum-ptr.rs:23:1 | LL | const BAD_ENUM: Enum = unsafe { Foo { a: &1 }.b}; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ a raw memory access tried to access part of a pointer value as raw bytes + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer at .TAG, but expected something in the range 0..=0 | - = note: #[deny(const_err)] on by default + = 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 error: aborting due to previous error +For more information about this error, try `rustc --explain E0080`.