2023-10-12 11:27:43 +00:00
|
|
|
use crate::const_eval::CompileTimeEvalContext;
|
|
|
|
|
use crate::interpret::{MemPlaceMeta, MemoryKind};
|
|
|
|
|
use rustc_middle::mir::interpret::{AllocId, 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,
|
|
|
|
|
};
|
2023-10-12 11:27:43 +00:00
|
|
|
use rustc_span::def_id::DefId;
|
2020-10-21 14:26:34 +02:00
|
|
|
use std::ops::ControlFlow;
|
2020-07-24 13:16:54 +01:00
|
|
|
|
2023-10-12 11:27:43 +00:00
|
|
|
use super::MPlaceTy;
|
|
|
|
|
|
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.
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn ensure_monomorphic_enough<'tcx, T>(tcx: TyCtxt<'tcx>, ty: T) -> InterpResult<'tcx>
|
2020-07-24 13:16:54 +01:00
|
|
|
where
|
2023-02-22 02:18:40 +00:00
|
|
|
T: TypeVisitable<TyCtxt<'tcx>>,
|
2020-07-24 13:16:54 +01:00
|
|
|
{
|
|
|
|
|
debug!("ensure_monomorphic_enough: ty={:?}", ty);
|
2023-04-27 07:52:17 +01:00
|
|
|
if !ty.has_param() {
|
2020-07-24 13:16:54 +01:00
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-06 21:31:42 +01:00
|
|
|
struct FoundParam;
|
2024-02-12 15:39:32 +09:00
|
|
|
struct UsedParamsNeedInstantiationVisitor<'tcx> {
|
2020-07-24 13:16:54 +01:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-11-25 17:00:28 -05:00
|
|
|
}
|
2020-07-24 13:16:54 +01:00
|
|
|
|
2024-02-12 15:39:32 +09:00
|
|
|
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for UsedParamsNeedInstantiationVisitor<'tcx> {
|
2020-12-06 21:31:42 +01:00
|
|
|
type BreakTy = FoundParam;
|
2020-11-14 21:46:39 +01:00
|
|
|
|
2020-11-05 17:30:39 +01:00
|
|
|
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2023-04-27 07:52:17 +01:00
|
|
|
if !ty.has_param() {
|
2023-01-17 23:17:13 -08:00
|
|
|
return ControlFlow::Continue(());
|
2020-07-24 13:16:54 +01:00
|
|
|
}
|
|
|
|
|
|
2020-08-03 00:49:11 +02:00
|
|
|
match *ty.kind() {
|
2020-12-06 21:31:42 +01:00
|
|
|
ty::Param(_) => ControlFlow::Break(FoundParam),
|
2023-07-11 22:35:29 +01:00
|
|
|
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, ..)
|
2023-07-11 22:35:29 +01:00
|
|
|
| ty::FnDef(def_id, args) => {
|
2022-05-08 15:53:19 +02:00
|
|
|
let instance = ty::InstanceDef::Item(def_id);
|
2021-10-01 17:08:06 +00:00
|
|
|
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() {
|
2020-07-24 13:16:54 +01:00
|
|
|
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,
|
2021-09-30 21:58:49 -04:00
|
|
|
// recurse.
|
2024-02-12 15:39:32 +09:00
|
|
|
if unused_params.is_used(index) && arg.has_param() {
|
|
|
|
|
return arg.visit_with(self);
|
2020-07-24 13:16:54 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-01-17 23:17:13 -08:00
|
|
|
ControlFlow::Continue(())
|
2020-07-24 13:16:54 +01:00
|
|
|
}
|
|
|
|
|
_ => ty.super_visit_with(self),
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-17 18:48:07 +02:00
|
|
|
|
2022-02-02 14:24:45 +11:00
|
|
|
fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2022-06-10 11:18:06 +10:00
|
|
|
match c.kind() {
|
2021-07-17 18:48:07 +02:00
|
|
|
ty::ConstKind::Param(..) => ControlFlow::Break(FoundParam),
|
|
|
|
|
_ => c.super_visit_with(self),
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-24 13:16:54 +01:00
|
|
|
}
|
|
|
|
|
|
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)) {
|
2020-07-24 13:16:54 +01:00
|
|
|
throw_inval!(TooGeneric);
|
|
|
|
|
} else {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-12 11:27:43 +00:00
|
|
|
|
|
|
|
|
pub(crate) fn take_static_root_alloc<'mir, 'tcx: 'mir>(
|
|
|
|
|
ecx: &mut CompileTimeEvalContext<'mir, 'tcx>,
|
|
|
|
|
alloc_id: AllocId,
|
|
|
|
|
) -> Allocation {
|
|
|
|
|
ecx.memory.alloc_map.swap_remove(&alloc_id).unwrap().1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn create_static_alloc<'mir, 'tcx: 'mir>(
|
|
|
|
|
ecx: &mut CompileTimeEvalContext<'mir, 'tcx>,
|
|
|
|
|
static_def_id: DefId,
|
|
|
|
|
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);
|
|
|
|
|
assert_eq!(ecx.machine.static_root_alloc_id, None);
|
|
|
|
|
ecx.machine.static_root_alloc_id = Some(alloc_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))
|
|
|
|
|
}
|