2020-07-24 12:16:54 +00:00
|
|
|
use rustc_middle::mir::interpret::InterpResult;
|
Folding revamp.
This commit makes type folding more like the way chalk does it.
Currently, `TypeFoldable` has `fold_with` and `super_fold_with` methods.
- `fold_with` is the standard entry point, and defaults to calling
`super_fold_with`.
- `super_fold_with` does the actual work of traversing a type.
- For a few types of interest (`Ty`, `Region`, etc.) `fold_with` instead
calls into a `TypeFolder`, which can then call back into
`super_fold_with`.
With the new approach, `TypeFoldable` has `fold_with` and
`TypeSuperFoldable` has `super_fold_with`.
- `fold_with` is still the standard entry point, *and* it does the
actual work of traversing a type, for all types except types of
interest.
- `super_fold_with` is only implemented for the types of interest.
Benefits of the new model.
- I find it easier to understand. The distinction between types of
interest and other types is clearer, and `super_fold_with` doesn't
exist for most types.
- With the current model is easy to get confused and implement a
`super_fold_with` method that should be left defaulted. (Some of the
precursor commits fixed such cases.)
- With the current model it's easy to call `super_fold_with` within
`TypeFolder` impls where `fold_with` should be called. The new
approach makes this mistake impossible, and this commit fixes a number
of such cases.
- It's potentially faster, because it avoids the `fold_with` ->
`super_fold_with` call in all cases except types of interest. A lot of
the time the compile would inline those away, but not necessarily
always.
2022-06-02 01:38:15 +00:00
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable, TypeVisitor};
|
2020-07-24 12:16:54 +00:00
|
|
|
use std::convert::TryInto;
|
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
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
{
|
|
|
|
debug!("ensure_monomorphic_enough: ty={:?}", ty);
|
2022-01-12 03:19:52 +00:00
|
|
|
if !ty.needs_subst() {
|
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
|
|
|
|
|
|
|
impl<'tcx> TypeVisitor<'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> {
|
2022-01-12 03:19:52 +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-12-06 20:31:42 +00:00
|
|
|
ty::Param(_) => ControlFlow::Break(FoundParam),
|
2020-07-24 12:16:54 +00:00
|
|
|
ty::Closure(def_id, substs)
|
|
|
|
| ty::Generator(def_id, substs, ..)
|
|
|
|
| ty::FnDef(def_id, substs) => {
|
2021-10-01 17:08:06 +00:00
|
|
|
let instance = ty::InstanceDef::Item(ty::WithOptConstParam::unknown(def_id));
|
|
|
|
let unused_params = self.tcx.unused_generic_params(instance);
|
2020-07-24 12:16:54 +00:00
|
|
|
for (index, subst) in substs.into_iter().enumerate() {
|
|
|
|
let index = index
|
|
|
|
.try_into()
|
|
|
|
.expect("more generic parameters than can fit into a `u32`");
|
2021-01-11 19:45:33 +00:00
|
|
|
let is_used = unused_params.contains(index).map_or(true, |unused| !unused);
|
2020-07-24 12:16:54 +00:00
|
|
|
// Only recurse when generic parameters in fns, closures and generators
|
|
|
|
// are used and require substitution.
|
2022-01-12 03:19:52 +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.
|
Folding revamp.
This commit makes type folding more like the way chalk does it.
Currently, `TypeFoldable` has `fold_with` and `super_fold_with` methods.
- `fold_with` is the standard entry point, and defaults to calling
`super_fold_with`.
- `super_fold_with` does the actual work of traversing a type.
- For a few types of interest (`Ty`, `Region`, etc.) `fold_with` instead
calls into a `TypeFolder`, which can then call back into
`super_fold_with`.
With the new approach, `TypeFoldable` has `fold_with` and
`TypeSuperFoldable` has `super_fold_with`.
- `fold_with` is still the standard entry point, *and* it does the
actual work of traversing a type, for all types except types of
interest.
- `super_fold_with` is only implemented for the types of interest.
Benefits of the new model.
- I find it easier to understand. The distinction between types of
interest and other types is clearer, and `super_fold_with` doesn't
exist for most types.
- With the current model is easy to get confused and implement a
`super_fold_with` method that should be left defaulted. (Some of the
precursor commits fixed such cases.)
- With the current model it's easy to call `super_fold_with` within
`TypeFolder` impls where `fold_with` should be called. The new
approach makes this mistake impossible, and this commit fixes a number
of such cases.
- It's potentially faster, because it avoids the `fold_with` ->
`super_fold_with` call in all cases except types of interest. A lot of
the time the compile would inline those away, but not necessarily
always.
2022-06-02 01:38:15 +00:00
|
|
|
(true, true) => return subst.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) => {
|
2022-06-10 01:18:06 +00:00
|
|
|
assert!(matches!(ct.kind(), ty::ConstKind::Param(_)))
|
2020-08-04 17:18:27 +00:00
|
|
|
}
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
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(())
|
|
|
|
}
|
|
|
|
}
|