Files
rust/compiler/rustc_const_eval/src/interpret/util.rs

107 lines
4.2 KiB
Rust
Raw Normal View History

use crate::const_eval::{CompileTimeInterpCx, CompileTimeMachine, InterpretationResult};
use rustc_hir::def_id::LocalDefId;
use rustc_middle::mir;
use rustc_middle::mir::interpret::{Allocation, InterpResult, Pointer};
use rustc_middle::ty::layout::TyAndLayout;
2023-02-22 02:18:40 +00:00
use rustc_middle::ty::{
self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
};
use std::ops::ControlFlow;
use tracing::debug;
use super::{throw_inval, InterpCx, MPlaceTy, MemPlaceMeta, MemoryKind};
2023-09-25 15:46:38 +02:00
/// Checks whether a type contains generic parameters which must be instantiated.
2022-01-20 14:47:31 +01:00
///
/// In case it does, returns a `TooGeneric` const eval error. Note that due to polymorphization
/// types may be "concrete enough" even though they still contain generic parameters in
/// case these parameters are unused.
pub(crate) fn ensure_monomorphic_enough<'tcx, T>(tcx: TyCtxt<'tcx>, ty: T) -> InterpResult<'tcx>
where
2023-02-22 02:18:40 +00:00
T: TypeVisitable<TyCtxt<'tcx>>,
{
debug!("ensure_monomorphic_enough: ty={:?}", ty);
2023-04-27 07:52:17 +01:00
if !ty.has_param() {
return Ok(());
}
2020-12-06 21:31:42 +01:00
struct FoundParam;
2024-02-12 15:39:32 +09:00
struct UsedParamsNeedInstantiationVisitor<'tcx> {
tcx: TyCtxt<'tcx>,
}
2024-02-12 15:39:32 +09:00
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for UsedParamsNeedInstantiationVisitor<'tcx> {
type Result = ControlFlow<FoundParam>;
2020-11-14 21:46:39 +01:00
fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
2023-04-27 07:52:17 +01:00
if !ty.has_param() {
return ControlFlow::Continue(());
}
2020-08-03 00:49:11 +02:00
match *ty.kind() {
2020-12-06 21:31:42 +01:00
ty::Param(_) => ControlFlow::Break(FoundParam),
ty::Closure(def_id, args)
2024-02-11 22:09:28 +00:00
| ty::CoroutineClosure(def_id, args, ..)
2023-10-19 16:06:43 +00:00
| ty::Coroutine(def_id, args, ..)
| ty::FnDef(def_id, args) => {
2022-05-08 15:53:19 +02:00
let instance = ty::InstanceDef::Item(def_id);
let unused_params = self.tcx.unused_generic_params(instance);
2024-02-12 15:39:32 +09:00
for (index, arg) in args.into_iter().enumerate() {
let index = index
.try_into()
.expect("more generic parameters than can fit into a `u32`");
2023-10-19 21:46:28 +00:00
// Only recurse when generic parameters in fns, closures and coroutines
2023-09-25 15:46:38 +02:00
// are used and have to be instantiated.
//
2024-02-12 15:39:32 +09:00
// Just in case there are closures or coroutines within this arg,
// recurse.
2024-02-12 15:39:32 +09:00
if unused_params.is_used(index) && arg.has_param() {
return arg.visit_with(self);
}
}
ControlFlow::Continue(())
}
_ => ty.super_visit_with(self),
}
}
fn visit_const(&mut self, c: ty::Const<'tcx>) -> Self::Result {
match c.kind() {
ty::ConstKind::Param(..) => ControlFlow::Break(FoundParam),
_ => c.super_visit_with(self),
}
}
}
2024-02-12 15:39:32 +09:00
let mut vis = UsedParamsNeedInstantiationVisitor { tcx };
2020-12-06 21:31:42 +01:00
if matches!(ty.visit_with(&mut vis), ControlFlow::Break(FoundParam)) {
throw_inval!(TooGeneric);
} else {
Ok(())
}
}
impl<'tcx> InterpretationResult<'tcx> for mir::interpret::ConstAllocation<'tcx> {
fn make_result(
mplace: MPlaceTy<'tcx>,
ecx: &mut InterpCx<'tcx, CompileTimeMachine<'tcx>>,
) -> Self {
let alloc_id = mplace.ptr().provenance.unwrap().alloc_id();
let alloc = ecx.memory.alloc_map.swap_remove(&alloc_id).unwrap().1;
ecx.tcx.mk_const_alloc(alloc)
}
}
pub(crate) fn create_static_alloc<'tcx>(
ecx: &mut CompileTimeInterpCx<'tcx>,
static_def_id: LocalDefId,
layout: TyAndLayout<'tcx>,
) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
let alloc = Allocation::try_uninit(layout.size, layout.align.abi)?;
let alloc_id = ecx.tcx.reserve_and_set_static_alloc(static_def_id.into());
assert_eq!(ecx.machine.static_root_ids, None);
ecx.machine.static_root_ids = Some((alloc_id, static_def_id));
assert!(ecx.memory.alloc_map.insert(alloc_id, (MemoryKind::Stack, alloc)).is_none());
Ok(ecx.ptr_with_meta_to_mplace(Pointer::from(alloc_id).into(), MemPlaceMeta::None, layout))
}