]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_const_eval/src/interpret/util.rs
Auto merge of #95295 - CAD97:layout-isize, r=scottmcm
[rust.git] / compiler / rustc_const_eval / src / interpret / util.rs
1 use rustc_middle::mir::interpret::InterpResult;
2 use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor};
3 use std::convert::TryInto;
4 use std::ops::ControlFlow;
5
6 /// Checks whether a type contains generic parameters which require substitution.
7 ///
8 /// In case it does, returns a `TooGeneric` const eval error. Note that due to polymorphization
9 /// types may be "concrete enough" even though they still contain generic parameters in
10 /// case these parameters are unused.
11 pub(crate) fn ensure_monomorphic_enough<'tcx, T>(tcx: TyCtxt<'tcx>, ty: T) -> InterpResult<'tcx>
12 where
13     T: TypeVisitable<'tcx>,
14 {
15     debug!("ensure_monomorphic_enough: ty={:?}", ty);
16     if !ty.needs_subst() {
17         return Ok(());
18     }
19
20     struct FoundParam;
21     struct UsedParamsNeedSubstVisitor<'tcx> {
22         tcx: TyCtxt<'tcx>,
23     }
24
25     impl<'tcx> TypeVisitor<'tcx> for UsedParamsNeedSubstVisitor<'tcx> {
26         type BreakTy = FoundParam;
27
28         fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
29             if !ty.needs_subst() {
30                 return ControlFlow::CONTINUE;
31             }
32
33             match *ty.kind() {
34                 ty::Param(_) => ControlFlow::Break(FoundParam),
35                 ty::Closure(def_id, substs)
36                 | ty::Generator(def_id, substs, ..)
37                 | ty::FnDef(def_id, substs) => {
38                     let instance = ty::InstanceDef::Item(ty::WithOptConstParam::unknown(def_id));
39                     let unused_params = self.tcx.unused_generic_params(instance);
40                     for (index, subst) in substs.into_iter().enumerate() {
41                         let index = index
42                             .try_into()
43                             .expect("more generic parameters than can fit into a `u32`");
44                         let is_used = unused_params.contains(index).map_or(true, |unused| !unused);
45                         // Only recurse when generic parameters in fns, closures and generators
46                         // are used and require substitution.
47                         // Just in case there are closures or generators within this subst,
48                         // recurse.
49                         if is_used && subst.needs_subst() {
50                             return subst.visit_with(self);
51                         }
52                     }
53                     ControlFlow::CONTINUE
54                 }
55                 _ => ty.super_visit_with(self),
56             }
57         }
58
59         fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
60             match c.kind() {
61                 ty::ConstKind::Param(..) => ControlFlow::Break(FoundParam),
62                 _ => c.super_visit_with(self),
63             }
64         }
65     }
66
67     let mut vis = UsedParamsNeedSubstVisitor { tcx };
68     if matches!(ty.visit_with(&mut vis), ControlFlow::Break(FoundParam)) {
69         throw_inval!(TooGeneric);
70     } else {
71         Ok(())
72     }
73 }