2020-07-24 12:16:54 +00:00
|
|
|
use rustc_middle::mir::interpret::InterpResult;
|
2023-02-22 02:18:40 +00:00
|
|
|
use rustc_middle::ty::{
|
|
|
|
self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
|
|
|
|
};
|
2020-10-21 12:26:34 +00:00
|
|
|
use std::ops::ControlFlow;
|
2020-07-24 12:16:54 +00:00
|
|
|
|
2022-01-20 13:47:31 +00:00
|
|
|
/// Checks whether a type contains generic parameters which require substitution.
|
|
|
|
///
|
|
|
|
/// 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 23:51:09 +00:00
|
|
|
pub(crate) fn ensure_monomorphic_enough<'tcx, T>(tcx: TyCtxt<'tcx>, ty: T) -> InterpResult<'tcx>
|
2020-07-24 12:16:54 +00:00
|
|
|
where
|
2023-02-22 02:18:40 +00:00
|
|
|
T: TypeVisitable<TyCtxt<'tcx>>,
|
2020-07-24 12:16:54 +00:00
|
|
|
{
|
|
|
|
debug!("ensure_monomorphic_enough: ty={:?}", ty);
|
2023-04-27 06:52:17 +00:00
|
|
|
if !ty.has_param() {
|
2020-07-24 12:16:54 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2020-12-06 20:31:42 +00:00
|
|
|
struct FoundParam;
|
2020-07-24 12:16:54 +00:00
|
|
|
struct UsedParamsNeedSubstVisitor<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-11-25 22:00:28 +00:00
|
|
|
}
|
2020-07-24 12:16:54 +00:00
|
|
|
|
2023-02-09 19:38:07 +00:00
|
|
|
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for UsedParamsNeedSubstVisitor<'tcx> {
|
2020-12-06 20:31:42 +00:00
|
|
|
type BreakTy = FoundParam;
|
2020-11-14 20:46:39 +00:00
|
|
|
|
2020-11-05 16:30:39 +00:00
|
|
|
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2023-04-27 06:52:17 +00:00
|
|
|
if !ty.has_param() {
|
2023-01-18 07:17:13 +00:00
|
|
|
return ControlFlow::Continue(());
|
2020-07-24 12:16:54 +00:00
|
|
|
}
|
|
|
|
|
2020-08-02 22:49:11 +00:00
|
|
|
match *ty.kind() {
|
2020-12-06 20:31:42 +00:00
|
|
|
ty::Param(_) => ControlFlow::Break(FoundParam),
|
2023-07-11 21:35:29 +00:00
|
|
|
ty::Closure(def_id, args)
|
|
|
|
| ty::Generator(def_id, args, ..)
|
|
|
|
| ty::FnDef(def_id, args) => {
|
2022-05-08 13:53:19 +00: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);
|
2023-07-11 21:35:29 +00:00
|
|
|
for (index, subst) in args.into_iter().enumerate() {
|
2020-07-24 12:16:54 +00:00
|
|
|
let index = index
|
|
|
|
.try_into()
|
|
|
|
.expect("more generic parameters than can fit into a `u32`");
|
|
|
|
// Only recurse when generic parameters in fns, closures and generators
|
|
|
|
// are used and require substitution.
|
2021-10-01 01:58:49 +00:00
|
|
|
// Just in case there are closures or generators within this subst,
|
|
|
|
// recurse.
|
2023-04-27 06:52:17 +00:00
|
|
|
if unused_params.is_used(index) && subst.has_param() {
|
2021-10-01 01:58:49 +00:00
|
|
|
return subst.visit_with(self);
|
2020-07-24 12:16:54 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-18 07:17:13 +00:00
|
|
|
ControlFlow::Continue(())
|
2020-07-24 12:16:54 +00:00
|
|
|
}
|
|
|
|
_ => ty.super_visit_with(self),
|
|
|
|
}
|
|
|
|
}
|
2021-07-17 16:48:07 +00:00
|
|
|
|
2022-02-02 03:24:45 +00:00
|
|
|
fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2022-06-10 01:18:06 +00:00
|
|
|
match c.kind() {
|
2021-07-17 16:48:07 +00:00
|
|
|
ty::ConstKind::Param(..) => ControlFlow::Break(FoundParam),
|
|
|
|
_ => c.super_visit_with(self),
|
|
|
|
}
|
|
|
|
}
|
2020-07-24 12:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut vis = UsedParamsNeedSubstVisitor { tcx };
|
2020-12-06 20:31:42 +00:00
|
|
|
if matches!(ty.visit_with(&mut vis), ControlFlow::Break(FoundParam)) {
|
2020-07-24 12:16:54 +00:00
|
|
|
throw_inval!(TooGeneric);
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|