]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/ty/normalize_erasing_regions.rs
Rollup merge of #85174 - GuillaumeGomez:doc-code-block-border-radius, r=jsha
[rust.git] / compiler / rustc_middle / src / ty / normalize_erasing_regions.rs
1 //! Methods for normalizing when you don't care about regions (and
2 //! aren't doing type inference). If either of those things don't
3 //! apply to you, use `infcx.normalize(...)`.
4 //!
5 //! The methods in this file use a `TypeFolder` to recursively process
6 //! contents, invoking the underlying
7 //! `normalize_generic_arg_after_erasing_regions` query for each type
8 //! or constant found within. (This underlying query is what is cached.)
9
10 use crate::mir;
11 use crate::ty::fold::{TypeFoldable, TypeFolder};
12 use crate::ty::subst::{Subst, SubstsRef};
13 use crate::ty::{self, Ty, TyCtxt};
14
15 impl<'tcx> TyCtxt<'tcx> {
16     /// Erase the regions in `value` and then fully normalize all the
17     /// types found within. The result will also have regions erased.
18     ///
19     /// This is appropriate to use only after type-check: it assumes
20     /// that normalization will succeed, for example.
21     pub fn normalize_erasing_regions<T>(self, param_env: ty::ParamEnv<'tcx>, value: T) -> T
22     where
23         T: TypeFoldable<'tcx>,
24     {
25         debug!(
26             "normalize_erasing_regions::<{}>(value={:?}, param_env={:?})",
27             std::any::type_name::<T>(),
28             value,
29             param_env,
30         );
31
32         // Erase first before we do the real query -- this keeps the
33         // cache from being too polluted.
34         let value = self.erase_regions(value);
35         if !value.has_projections() {
36             value
37         } else {
38             value.fold_with(&mut NormalizeAfterErasingRegionsFolder { tcx: self, param_env })
39         }
40     }
41
42     /// If you have a `Binder<'tcx, T>`, you can do this to strip out the
43     /// late-bound regions and then normalize the result, yielding up
44     /// a `T` (with regions erased). This is appropriate when the
45     /// binder is being instantiated at the call site.
46     ///
47     /// N.B., currently, higher-ranked type bounds inhibit
48     /// normalization. Therefore, each time we erase them in
49     /// codegen, we need to normalize the contents.
50     pub fn normalize_erasing_late_bound_regions<T>(
51         self,
52         param_env: ty::ParamEnv<'tcx>,
53         value: ty::Binder<'tcx, T>,
54     ) -> T
55     where
56         T: TypeFoldable<'tcx>,
57     {
58         let value = self.erase_late_bound_regions(value);
59         self.normalize_erasing_regions(param_env, value)
60     }
61
62     /// Monomorphizes a type from the AST by first applying the
63     /// in-scope substitutions and then normalizing any associated
64     /// types.
65     pub fn subst_and_normalize_erasing_regions<T>(
66         self,
67         param_substs: SubstsRef<'tcx>,
68         param_env: ty::ParamEnv<'tcx>,
69         value: T,
70     ) -> T
71     where
72         T: TypeFoldable<'tcx>,
73     {
74         debug!(
75             "subst_and_normalize_erasing_regions(\
76              param_substs={:?}, \
77              value={:?}, \
78              param_env={:?})",
79             param_substs, value, param_env,
80         );
81         let substituted = value.subst(self, param_substs);
82         self.normalize_erasing_regions(param_env, substituted)
83     }
84 }
85
86 struct NormalizeAfterErasingRegionsFolder<'tcx> {
87     tcx: TyCtxt<'tcx>,
88     param_env: ty::ParamEnv<'tcx>,
89 }
90
91 impl TypeFolder<'tcx> for NormalizeAfterErasingRegionsFolder<'tcx> {
92     fn tcx(&self) -> TyCtxt<'tcx> {
93         self.tcx
94     }
95
96     fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
97         let arg = self.param_env.and(ty.into());
98         self.tcx.normalize_generic_arg_after_erasing_regions(arg).expect_ty()
99     }
100
101     fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
102         let arg = self.param_env.and(c.into());
103         self.tcx.normalize_generic_arg_after_erasing_regions(arg).expect_const()
104     }
105
106     #[inline]
107     fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> {
108         let arg = self.param_env.and(c);
109         self.tcx.normalize_mir_const_after_erasing_regions(arg)
110     }
111 }