Skip to content

Commit

Permalink
do not normalize non-scalar constants to a ConstValue::ScalarPair
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung authored and pietroalbini committed Oct 4, 2018
1 parent 310a921 commit e3f6931
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/librustc_mir/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use rustc::hir::{self, def_id::DefId};
use rustc::mir::interpret::ConstEvalErr;
use rustc::mir;
use rustc::ty::{self, TyCtxt, Instance, query::TyCtxtAt};
use rustc::ty::layout::{LayoutOf, TyLayout};
use rustc::ty::layout::{self, LayoutOf, TyLayout};
use rustc::ty::subst::Subst;
use rustc_data_structures::indexed_vec::IndexVec;

Expand Down Expand Up @@ -90,8 +90,18 @@ pub fn eval_promoted<'a, 'mir, 'tcx>(
pub fn op_to_const<'tcx>(
ecx: &EvalContext<'_, '_, 'tcx, CompileTimeEvaluator>,
op: OpTy<'tcx>,
normalize: bool,
may_normalize: bool,
) -> EvalResult<'tcx, &'tcx ty::Const<'tcx>> {
// We do not normalize just any data. Only scalar layout and fat pointers.
let normalize = may_normalize
&& match op.layout.abi {
layout::Abi::Scalar(..) => true,
layout::Abi::ScalarPair(..) => {
// Must be a fat pointer
op.layout.ty.builtin_deref(true).is_some()
},
_ => false,
};
let normalized_op = if normalize {
ecx.try_read_value(op)?
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/consts/const-eval/union-ice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const UNION: DummyUnion = DummyUnion { field1: 1065353216 };

const FIELD3: Field3 = unsafe { UNION.field3 }; //~ ERROR this constant cannot be used

const FIELD_PATH: Struct = Struct { //~ ERROR this constant cannot be used
const FIELD_PATH: Struct = Struct { //~ ERROR this constant likely exhibits undefined behavior
a: 42,
b: unsafe { UNION.field3 },
};
Expand Down
8 changes: 5 additions & 3 deletions src/test/ui/consts/const-eval/union-ice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ LL | const FIELD3: Field3 = unsafe { UNION.field3 }; //~ ERROR this constant can
|
= note: #[deny(const_err)] on by default

error: this constant cannot be used
error[E0080]: this constant likely exhibits undefined behavior
--> $DIR/union-ice.rs:25:1
|
LL | / const FIELD_PATH: Struct = Struct { //~ ERROR this constant cannot be used
LL | / const FIELD_PATH: Struct = Struct { //~ ERROR this constant likely exhibits undefined behavior
LL | | a: 42,
LL | | b: unsafe { UNION.field3 },
LL | | };
| |__^ attempted to read undefined bytes
| |__^ 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

error[E0080]: this constant likely exhibits undefined behavior
--> $DIR/union-ice.rs:35:1
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/issues/issue-54387.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// compile-pass

pub struct GstRc {
_obj: *const (),
_borrowed: bool,
}

const FOO: Option<GstRc> = None;

fn main() {
let _meh = FOO;
}

0 comments on commit e3f6931

Please sign in to comment.