diff options
| -rw-r--r-- | compiler/rustc_const_eval/src/const_eval/valtrees.rs | 88 |
1 files changed, 37 insertions, 51 deletions
diff --git a/compiler/rustc_const_eval/src/const_eval/valtrees.rs b/compiler/rustc_const_eval/src/const_eval/valtrees.rs index 572d7f9ac2f..793a1d21c30 100644 --- a/compiler/rustc_const_eval/src/const_eval/valtrees.rs +++ b/compiler/rustc_const_eval/src/const_eval/valtrees.rs @@ -189,12 +189,11 @@ fn reconstruct_place_meta<'tcx>( } #[instrument(skip(ecx), level = "debug", ret)] -fn create_pointee_place<'tcx>( +fn create_valtree_place<'tcx>( ecx: &mut CompileTimeEvalContext<'tcx, 'tcx>, - ty: Ty<'tcx>, + layout: TyAndLayout<'tcx>, valtree: ty::ValTree<'tcx>, ) -> MPlaceTy<'tcx> { - let layout = ecx.layout_of(ty).unwrap(); let meta = reconstruct_place_meta(layout, valtree, ecx.tcx.tcx); ecx.allocate_dyn(layout, MemoryKind::Stack, meta).unwrap() } @@ -216,11 +215,6 @@ pub fn valtree_to_const_value<'tcx>( // FIXME Does this need an example? let (param_env, ty) = param_env_ty.into_parts(); - let mut ecx: crate::interpret::InterpCx< - '_, - '_, - crate::const_eval::CompileTimeInterpreter<'_, '_>, - > = mk_eval_cx(tcx, DUMMY_SP, param_env, CanAccessStatics::No); match ty.kind() { ty::FnDef(..) => { @@ -233,33 +227,29 @@ pub fn valtree_to_const_value<'tcx>( "ValTrees for Bool, Int, Uint, Float or Char should have the form ValTree::Leaf" ), }, - ty::Ref(_, _, _) | ty::Tuple(_) | ty::Array(_, _) | ty::Adt(..) => { - let place = match ty.kind() { - ty::Ref(_, inner_ty, _) => { - // Need to create a place for the pointee (the reference itself will be an immediate) - create_pointee_place(&mut ecx, *inner_ty, valtree) - } - _ => { - // Need to create a place for this valtree. - create_pointee_place(&mut ecx, ty, valtree) - } - }; - debug!(?place); + ty::Ref(_, inner_ty, _) => { + let mut ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, CanAccessStatics::No); + let imm = valtree_to_ref(&mut ecx, valtree, *inner_ty); + let imm = ImmTy::from_immediate(imm, tcx.layout_of(param_env_ty).unwrap()); + op_to_const(&ecx, &imm.into()) + } + ty::Tuple(_) | ty::Array(_, _) | ty::Adt(..) => { + let layout = tcx.layout_of(param_env_ty).unwrap(); + if layout.is_zst() { + // Fast path to avoid some allocations. + return ConstValue::ZeroSized; + } + + let mut ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, CanAccessStatics::No); + + // Need to create a place for this valtree. + let place = create_valtree_place(&mut ecx, layout, valtree); valtree_into_mplace(&mut ecx, &place, valtree); dump_place(&ecx, &place); intern_const_alloc_recursive(&mut ecx, InternKind::Constant, &place).unwrap(); - match ty.kind() { - ty::Ref(_, _, _) => { - let ref_place = place.to_ref(&tcx); - let imm = - ImmTy::from_immediate(ref_place, tcx.layout_of(param_env_ty).unwrap()); - - op_to_const(&ecx, &imm.into()) - } - _ => op_to_const(&ecx, &place.into()), - } + op_to_const(&ecx, &place.into()) } ty::Never | ty::Error(_) @@ -283,6 +273,22 @@ pub fn valtree_to_const_value<'tcx>( } } +/// Put a valtree into memory and return a reference to that. +fn valtree_to_ref<'tcx>( + ecx: &mut CompileTimeEvalContext<'tcx, 'tcx>, + valtree: ty::ValTree<'tcx>, + pointee_ty: Ty<'tcx>, +) -> Immediate { + let pointee_place = create_valtree_place(ecx, ecx.layout_of(pointee_ty).unwrap(), valtree); + debug!(?pointee_place); + + valtree_into_mplace(ecx, &pointee_place, valtree); + dump_place(ecx, &pointee_place); + intern_const_alloc_recursive(ecx, InternKind::Constant, &pointee_place).unwrap(); + + pointee_place.to_ref(&ecx.tcx) +} + #[instrument(skip(ecx), level = "debug")] fn valtree_into_mplace<'tcx>( ecx: &mut CompileTimeEvalContext<'tcx, 'tcx>, @@ -292,7 +298,6 @@ fn valtree_into_mplace<'tcx>( // This will match on valtree and write the value(s) corresponding to the ValTree // inside the place recursively. - let tcx = ecx.tcx.tcx; let ty = place.layout.ty; match ty.kind() { @@ -305,27 +310,8 @@ fn valtree_into_mplace<'tcx>( ecx.write_immediate(Immediate::Scalar(scalar_int.into()), place).unwrap(); } ty::Ref(_, inner_ty, _) => { - let pointee_place = create_pointee_place(ecx, *inner_ty, valtree); - debug!(?pointee_place); - - valtree_into_mplace(ecx, &pointee_place, valtree); - dump_place(ecx, &pointee_place); - intern_const_alloc_recursive(ecx, InternKind::Constant, &pointee_place).unwrap(); - - let imm = match inner_ty.kind() { - ty::Slice(_) | ty::Str => { - let len = valtree.unwrap_branch().len(); - let len_scalar = Scalar::from_target_usize(len as u64, &tcx); - - Immediate::ScalarPair( - Scalar::from_maybe_pointer(pointee_place.ptr(), &tcx), - len_scalar, - ) - } - _ => pointee_place.to_ref(&tcx), - }; + let imm = valtree_to_ref(ecx, valtree, *inner_ty); debug!(?imm); - ecx.write_immediate(imm, place).unwrap(); } ty::Adt(_, _) | ty::Tuple(_) | ty::Array(_, _) | ty::Str | ty::Slice(_) => { |
