2020-07-24 12:16:54 +00:00
|
|
|
use rustc_middle::mir::interpret::InterpResult;
|
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeVisitor};
|
|
|
|
use std::convert::TryInto;
|
2020-10-21 12:26:34 +00:00
|
|
|
use std::ops::ControlFlow;
|
2020-07-24 12:16:54 +00:00
|
|
|
|
|
|
|
/// Returns `true` if a used generic parameter requires substitution.
|
|
|
|
crate fn ensure_monomorphic_enough<'tcx, T>(tcx: TyCtxt<'tcx>, ty: T) -> InterpResult<'tcx>
|
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
{
|
|
|
|
debug!("ensure_monomorphic_enough: ty={:?}", ty);
|
|
|
|
if !ty.needs_subst() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
struct UsedParamsNeedSubstVisitor<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
};
|
|
|
|
|
|
|
|
impl<'tcx> TypeVisitor<'tcx> for UsedParamsNeedSubstVisitor<'tcx> {
|
2020-10-25 10:50:56 +00:00
|
|
|
fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> ControlFlow<()> {
|
2020-07-24 12:16:54 +00:00
|
|
|
if !c.needs_subst() {
|
2020-10-21 12:26:34 +00:00
|
|
|
return ControlFlow::CONTINUE;
|
2020-07-24 12:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
match c.val {
|
2020-10-21 12:26:34 +00:00
|
|
|
ty::ConstKind::Param(..) => ControlFlow::BREAK,
|
2020-07-24 12:16:54 +00:00
|
|
|
_ => c.super_visit_with(self),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 10:50:56 +00:00
|
|
|
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<()> {
|
2020-07-24 12:16:54 +00:00
|
|
|
if !ty.needs_subst() {
|
2020-10-21 12:26:34 +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-10-21 12:26:34 +00:00
|
|
|
ty::Param(_) => ControlFlow::BREAK,
|
2020-07-24 12:16:54 +00:00
|
|
|
ty::Closure(def_id, substs)
|
|
|
|
| ty::Generator(def_id, substs, ..)
|
|
|
|
| ty::FnDef(def_id, substs) => {
|
|
|
|
let unused_params = self.tcx.unused_generic_params(def_id);
|
|
|
|
for (index, subst) in substs.into_iter().enumerate() {
|
|
|
|
let index = index
|
|
|
|
.try_into()
|
|
|
|
.expect("more generic parameters than can fit into a `u32`");
|
|
|
|
let is_used =
|
|
|
|
unused_params.contains(index).map(|unused| !unused).unwrap_or(true);
|
|
|
|
// Only recurse when generic parameters in fns, closures and generators
|
|
|
|
// are used and require substitution.
|
2020-08-04 17:18:27 +00:00
|
|
|
match (is_used, subst.needs_subst()) {
|
2020-07-24 12:16:54 +00:00
|
|
|
// Just in case there are closures or generators within this subst,
|
|
|
|
// recurse.
|
2020-10-21 12:26:34 +00:00
|
|
|
(true, true) => return subst.super_visit_with(self),
|
2020-08-04 17:18:27 +00:00
|
|
|
// Confirm that polymorphization replaced the parameter with
|
|
|
|
// `ty::Param`/`ty::ConstKind::Param`.
|
|
|
|
(false, true) if cfg!(debug_assertions) => match subst.unpack() {
|
|
|
|
ty::subst::GenericArgKind::Type(ty) => {
|
2020-08-02 22:49:11 +00:00
|
|
|
assert!(matches!(ty.kind(), ty::Param(_)))
|
2020-08-04 17:18:27 +00:00
|
|
|
}
|
|
|
|
ty::subst::GenericArgKind::Const(ct) => {
|
|
|
|
assert!(matches!(ct.val, ty::ConstKind::Param(_)))
|
|
|
|
}
|
|
|
|
ty::subst::GenericArgKind::Lifetime(..) => (),
|
|
|
|
},
|
|
|
|
_ => {}
|
2020-07-24 12:16:54 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-21 12:26:34 +00:00
|
|
|
ControlFlow::CONTINUE
|
2020-07-24 12:16:54 +00:00
|
|
|
}
|
|
|
|
_ => ty.super_visit_with(self),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut vis = UsedParamsNeedSubstVisitor { tcx };
|
2020-10-22 08:20:24 +00:00
|
|
|
if ty.visit_with(&mut vis).is_break() {
|
2020-07-24 12:16:54 +00:00
|
|
|
throw_inval!(TooGeneric);
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|