Skip to content

Commit c8e30c4

Browse files
committed
Reading values should not be looking at the variant
1 parent 2c836a7 commit c8e30c4

File tree

3 files changed

+7
-19
lines changed

3 files changed

+7
-19
lines changed

src/librustc_mir/interpret/eval_context.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
15211521
}
15221522

15231523
pub fn try_read_value(&self, ptr: Scalar, ptr_align: Align, ty: Ty<'tcx>) -> EvalResult<'tcx, Option<Value>> {
1524-
let mut layout = self.layout_of(ty)?;
1524+
let layout = self.layout_of(ty)?;
15251525
self.memory.check_align(ptr, ptr_align)?;
15261526

15271527
if layout.size.bytes() == 0 {
@@ -1530,19 +1530,6 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
15301530

15311531
let ptr = ptr.to_ptr()?;
15321532

1533-
match layout.variants {
1534-
layout::Variants::NicheFilling { .. } |
1535-
layout::Variants::Tagged { .. } => {
1536-
let variant_index = self.read_discriminant_as_variant_index(
1537-
Place::from_ptr(ptr, ptr_align),
1538-
layout,
1539-
)?;
1540-
layout = layout.for_variant(self, variant_index);
1541-
trace!("variant layout: {:#?}", layout);
1542-
},
1543-
layout::Variants::Single { .. } => {},
1544-
}
1545-
15461533
match layout.abi {
15471534
layout::Abi::Scalar(..) => {
15481535
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
15581545
let b_val = self.memory.read_scalar(b_ptr, ptr_align, b_size)?;
15591546
Ok(Some(Value::ScalarPair(a_val, b_val)))
15601547
}
1561-
_ => Ok(None),
1548+
_ => Ok(None),
15621549
}
15631550
}
15641551

src/test/ui/const-eval/ub-enum-ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ union Foo {
2121

2222
// A pointer is guaranteed non-null
2323
const BAD_ENUM: Enum = unsafe { Foo { a: &1 }.b};
24-
//~^ ERROR this constant cannot be used
24+
//~^ ERROR this constant likely exhibits undefined behavior
2525

2626
fn main() {
2727
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
error: this constant cannot be used
1+
error[E0080]: this constant likely exhibits undefined behavior
22
--> $DIR/ub-enum-ptr.rs:23:1
33
|
44
LL | const BAD_ENUM: Enum = unsafe { Foo { a: &1 }.b};
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ a raw memory access tried to access part of a pointer value as raw bytes
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer at .TAG, but expected something in the range 0..=0
66
|
7-
= note: #[deny(const_err)] on by default
7+
= 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
88

99
error: aborting due to previous error
1010

11+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)