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