]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/normalize_erasing_regions.rs
Rollup merge of #68348 - xfix:patch-14, r=nagisa
[rust.git] / src / librustc / 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_ty_after_erasing_regions` query for each type found
8 //! within. (This underlying query is what is cached.)
9
10 use crate::ty::fold::{TypeFoldable, TypeFolder};
11 use crate::ty::subst::{Subst, SubstsRef};
12 use crate::ty::{self, Ty, TyCtxt};
13
14 impl<'tcx> TyCtxt<'tcx> {
15     /// Erase the regions in `value` and then fully normalize all the
16     /// types found within. The result will also have regions erased.
17     ///
18     /// This is appropriate to use only after type-check: it assumes
19     /// that normalization will succeed, for example.
20     pub fn normalize_erasing_regions<T>(self, param_env: ty::ParamEnv<'tcx>, value: T) -> T
21     where
22         T: TypeFoldable<'tcx>,
23     {
24         debug!(
25             "normalize_erasing_regions::<{}>(value={:?}, param_env={:?})",
26             ::std::any::type_name::<T>(),
27             value,
28             param_env,
29         );
30
31         // Erase first before we do the real query -- this keeps the
32         // cache from being too polluted.
33         let value = self.erase_regions(&value);
34         if !value.has_projections() {
35             value
36         } else {
37             value.fold_with(&mut NormalizeAfterErasingRegionsFolder {
38                 tcx: self,
39                 param_env: param_env,
40             })
41         }
42     }
43
44     /// If you have a `Binder<T>`, you can do this to strip out the
45     /// late-bound regions and then normalize the result, yielding up
46     /// a `T` (with regions erased). This is appropriate when the
47     /// binder is being instantiated at the call site.
48     ///
49     /// N.B., currently, higher-ranked type bounds inhibit
50     /// normalization. Therefore, each time we erase them in
51     /// codegen, we need to normalize the contents.
52     pub fn normalize_erasing_late_bound_regions<T>(
53         self,
54         param_env: ty::ParamEnv<'tcx>,
55         value: &ty::Binder<T>,
56     ) -> T
57     where
58         T: TypeFoldable<'tcx>,
59     {
60         assert!(!value.needs_subst());
61         let value = self.erase_late_bound_regions(value);
62         self.normalize_erasing_regions(param_env, value)
63     }
64
65     /// Monomorphizes a type from the AST by first applying the
66     /// in-scope substitutions and then normalizing any associated
67     /// types.
68     pub fn subst_and_normalize_erasing_regions<T>(
69         self,
70         param_substs: SubstsRef<'tcx>,
71         param_env: ty::ParamEnv<'tcx>,
72         value: &T,
73     ) -> T
74     where
75         T: TypeFoldable<'tcx>,
76     {
77         debug!(
78             "subst_and_normalize_erasing_regions(\
79              param_substs={:?}, \
80              value={:?}, \
81              param_env={:?})",
82             param_substs, value, param_env,
83         );
84         let substituted = value.subst(self, param_substs);
85         self.normalize_erasing_regions(param_env, substituted)
86     }
87 }
88
89 struct NormalizeAfterErasingRegionsFolder<'tcx> {
90     tcx: TyCtxt<'tcx>,
91     param_env: ty::ParamEnv<'tcx>,
92 }
93
94 impl TypeFolder<'tcx> for NormalizeAfterErasingRegionsFolder<'tcx> {
95     fn tcx(&self) -> TyCtxt<'tcx> {
96         self.tcx
97     }
98
99     fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
100         self.tcx.normalize_ty_after_erasing_regions(self.param_env.and(ty))
101     }
102 }