Skip to content

Commit

Permalink
Reading values should not be looking at the variant
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Aug 2, 2018
1 parent 2c836a7 commit c8e30c4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 19 deletions.
17 changes: 2 additions & 15 deletions src/librustc_mir/interpret/eval_context.rs
Expand Up @@ -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>> {
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 {
Expand All @@ -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)?;
Expand All @@ -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),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/const-eval/ub-enum-ptr.rs
Expand Up @@ -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() {
}
7 changes: 4 additions & 3 deletions 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`.

0 comments on commit c8e30c4

Please sign in to comment.