1
Fork 0

Reading values should not be looking at the variant

This commit is contained in:
Oliver Schneider 2018-08-02 10:19:37 +02:00
parent 2c836a7ebd
commit c8e30c4295
3 changed files with 7 additions and 19 deletions

View file

@ -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<Value>> { pub fn try_read_value(&self, ptr: Scalar, ptr_align: Align, ty: Ty<'tcx>) -> EvalResult<'tcx, Option<Value>> {
let mut layout = self.layout_of(ty)?; let layout = self.layout_of(ty)?;
self.memory.check_align(ptr, ptr_align)?; self.memory.check_align(ptr, ptr_align)?;
if layout.size.bytes() == 0 { 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()?; 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 { match layout.abi {
layout::Abi::Scalar(..) => { layout::Abi::Scalar(..) => {
let scalar = self.memory.read_scalar(ptr, ptr_align, layout.size)?; 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)?; let b_val = self.memory.read_scalar(b_ptr, ptr_align, b_size)?;
Ok(Some(Value::ScalarPair(a_val, b_val))) Ok(Some(Value::ScalarPair(a_val, b_val)))
} }
_ => Ok(None), _ => Ok(None),
} }
} }

View file

@ -21,7 +21,7 @@ union Foo {
// A pointer is guaranteed non-null // A pointer is guaranteed non-null
const BAD_ENUM: Enum = unsafe { Foo { a: &1 }.b}; const BAD_ENUM: Enum = unsafe { Foo { a: &1 }.b};
//~^ ERROR this constant cannot be used //~^ ERROR this constant likely exhibits undefined behavior
fn main() { fn main() {
} }

View file

@ -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 --> $DIR/ub-enum-ptr.rs:23:1
| |
LL | const BAD_ENUM: Enum = unsafe { Foo { a: &1 }.b}; 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 error: aborting due to previous error
For more information about this error, try `rustc --explain E0080`.