Fallout from fixing try_read_value to work with enums
This commit is contained in:
parent
551df45935
commit
2c836a7ebd
7 changed files with 35 additions and 56 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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`.
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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`.
|
||||
|
|
|
|||
|
|
@ -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 },
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue