Skip to content

Commit f7eae4e

Browse files
committed
include valtree creation and valtree -> constvalue conversion in debug assertions check
1 parent bc698c7 commit f7eae4e

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{CompileTimeEvalContext, CompileTimeInterpreter, ConstEvalErr};
1+
use super::{const_to_valtree, CompileTimeEvalContext, CompileTimeInterpreter, ConstEvalErr};
22
use crate::interpret::eval_nullary_intrinsic;
33
use crate::interpret::{
44
intern_const_alloc_recursive, Allocation, ConstAlloc, ConstValue, CtfeValidationMode, GlobalId,
@@ -215,6 +215,13 @@ fn turn_into_const_value<'tcx>(
215215
"the `eval_to_const_value_raw` query should not be used for statics, use `eval_to_allocation` instead"
216216
);
217217

218+
if cfg!(debug_assertions) {
219+
if let Some(valtree) = const_to_valtree(tcx, key.param_env, constant) {
220+
let const_val = tcx.valtree_to_const_val((constant.ty, valtree));
221+
debug!(?const_val);
222+
}
223+
}
224+
218225
// Turn this into a proper constant.
219226
let const_val = op_to_const(&ecx, &mplace.into());
220227
debug!(?const_val);

compiler/rustc_const_eval/src/const_eval/valtrees.rs

+22-30
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::interpret::{
55
Scalar, ScalarMaybeUninit,
66
};
77
use rustc_middle::mir::interpret::ConstAlloc;
8-
use rustc_middle::mir::{Field, ProjectionElem};
98
use rustc_middle::ty::{self, ScalarInt, Ty, TyCtxt};
109
use rustc_span::source_map::DUMMY_SP;
1110
use rustc_target::abi::VariantIdx;
@@ -197,45 +196,45 @@ pub fn valtree_to_const_value<'tcx>(
197196
let mut ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
198197

199198
match ty.kind() {
199+
ty::FnDef(..) => {
200+
assert!(valtree.unwrap_branch().is_empty());
201+
ConstValue::Scalar(Scalar::ZST)
202+
}
200203
ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Char => match valtree {
201204
ty::ValTree::Leaf(scalar_int) => ConstValue::Scalar(Scalar::Int(scalar_int)),
202205
ty::ValTree::Branch(_) => bug!(
203206
"ValTrees for Bool, Int, Uint, Float or Char should have the form ValTree::Leaf"
204207
),
205208
},
206-
ty::Ref(_, inner_ty, _) => {
207-
// create a place for the pointee
208-
let mut pointee_place = create_pointee_place(&mut ecx, *inner_ty, valtree);
209-
debug!(?pointee_place);
210-
211-
// insert elements of valtree into `place`
212-
fill_place_recursively(&mut ecx, &mut pointee_place, valtree);
213-
dump_place(&ecx, pointee_place.into());
214-
intern_const_alloc_recursive(&mut ecx, InternKind::Constant, &pointee_place).unwrap();
215-
216-
let ref_place = pointee_place.to_ref(&tcx);
217-
let imm = ImmTy::from_immediate(ref_place, tcx.layout_of(param_env_ty).unwrap());
218-
219-
let const_val = op_to_const(&ecx, &imm.into());
220-
debug!(?const_val);
221-
222-
const_val
223-
}
224-
ty::Tuple(_) | ty::Array(_, _) | ty::Adt(..) => {
225-
let mut place = create_mplace_from_layout(&mut ecx, ty);
209+
ty::Ref(_, _, _) | ty::Tuple(_) | ty::Array(_, _) | ty::Adt(..) => {
210+
let mut place = match ty.kind() {
211+
ty::Ref(_, inner_ty, _) => {
212+
// Need to create a place for the pointee to fill for Refs
213+
create_pointee_place(&mut ecx, *inner_ty, valtree)
214+
}
215+
_ => create_mplace_from_layout(&mut ecx, ty),
216+
};
226217
debug!(?place);
227218

228219
fill_place_recursively(&mut ecx, &mut place, valtree);
229220
dump_place(&ecx, place.into());
230221
intern_const_alloc_recursive(&mut ecx, InternKind::Constant, &place).unwrap();
231222

232-
let const_val = op_to_const(&ecx, &place.into());
223+
let const_val = match ty.kind() {
224+
ty::Ref(_, _, _) => {
225+
let ref_place = place.to_ref(&tcx);
226+
let imm =
227+
ImmTy::from_immediate(ref_place, tcx.layout_of(param_env_ty).unwrap());
228+
229+
op_to_const(&ecx, &imm.into())
230+
}
231+
_ => op_to_const(&ecx, &place.into()),
232+
};
233233
debug!(?const_val);
234234

235235
const_val
236236
}
237237
ty::Never
238-
| ty::FnDef(..)
239238
| ty::Error(_)
240239
| ty::Foreign(..)
241240
| ty::Infer(ty::FreshIntTy(_))
@@ -331,13 +330,6 @@ fn fill_place_recursively<'tcx>(
331330
debug!(?i, ?inner_valtree);
332331

333332
let mut place_inner = match *ty.kind() {
334-
ty::Adt(def, substs) if !def.is_enum() => {
335-
let field = &def.variant(VariantIdx::from_usize(0)).fields[i];
336-
let field_ty = field.ty(tcx, substs);
337-
let projection_elem = ProjectionElem::Field(Field::from_usize(i), field_ty);
338-
339-
ecx.mplace_projection(&place_adjusted, projection_elem).unwrap()
340-
}
341333
ty::Adt(_, _) | ty::Tuple(_) => ecx.mplace_field(&place_adjusted, i).unwrap(),
342334
ty::Array(_, _) | ty::Str => {
343335
ecx.mplace_index(&place_adjusted, i as u64).unwrap()

0 commit comments

Comments
 (0)