]> git.proxmox.com Git - rustc.git/blob - src/librustc_middle/ty/normalize_erasing_regions.rs
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_middle / 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::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 { tcx: self, param_env })
38 }
39 }
40
41 /// If you have a `Binder<T>`, you can do this to strip out the
42 /// late-bound regions and then normalize the result, yielding up
43 /// a `T` (with regions erased). This is appropriate when the
44 /// binder is being instantiated at the call site.
45 ///
46 /// N.B., currently, higher-ranked type bounds inhibit
47 /// normalization. Therefore, each time we erase them in
48 /// codegen, we need to normalize the contents.
49 pub fn normalize_erasing_late_bound_regions<T>(
50 self,
51 param_env: ty::ParamEnv<'tcx>,
52 value: &ty::Binder<T>,
53 ) -> T
54 where
55 T: TypeFoldable<'tcx>,
56 {
57 let value = self.erase_late_bound_regions(value);
58 self.normalize_erasing_regions(param_env, value)
59 }
60
61 /// Monomorphizes a type from the AST by first applying the
62 /// in-scope substitutions and then normalizing any associated
63 /// types.
64 pub fn subst_and_normalize_erasing_regions<T>(
65 self,
66 param_substs: SubstsRef<'tcx>,
67 param_env: ty::ParamEnv<'tcx>,
68 value: &T,
69 ) -> T
70 where
71 T: TypeFoldable<'tcx>,
72 {
73 debug!(
74 "subst_and_normalize_erasing_regions(\
75 param_substs={:?}, \
76 value={:?}, \
77 param_env={:?})",
78 param_substs, value, param_env,
79 );
80 let substituted = value.subst(self, param_substs);
81 self.normalize_erasing_regions(param_env, substituted)
82 }
83 }
84
85 struct NormalizeAfterErasingRegionsFolder<'tcx> {
86 tcx: TyCtxt<'tcx>,
87 param_env: ty::ParamEnv<'tcx>,
88 }
89
90 impl TypeFolder<'tcx> for NormalizeAfterErasingRegionsFolder<'tcx> {
91 fn tcx(&self) -> TyCtxt<'tcx> {
92 self.tcx
93 }
94
95 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
96 let arg = self.param_env.and(ty.into());
97 self.tcx.normalize_generic_arg_after_erasing_regions(arg).expect_ty()
98 }
99
100 fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
101 let arg = self.param_env.and(c.into());
102 self.tcx.normalize_generic_arg_after_erasing_regions(arg).expect_const()
103 }
104 }