]> git.lizzy.rs Git - rust.git/blob - src/librustc/traits/query/normalize_erasing_regions.rs
Auto merge of #58406 - Disasm:rv64-support, r=nagisa
[rust.git] / src / librustc / traits / query / 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::{self, Ty, TyCtxt};
11 use crate::ty::fold::{TypeFoldable, TypeFolder};
12
13 impl<'cx, 'tcx> TyCtxt<'cx, 'tcx, 'tcx> {
14     /// Erase the regions in `value` and then fully normalize all the
15     /// types found within. The result will also have regions erased.
16     ///
17     /// This is appropriate to use only after type-check: it assumes
18     /// that normalization will succeed, for example.
19     pub fn normalize_erasing_regions<T>(self, param_env: ty::ParamEnv<'tcx>, value: T) -> T
20     where
21         T: TypeFoldable<'tcx>,
22     {
23         debug!(
24             "normalize_erasing_regions::<{}>(value={:?}, param_env={:?})",
25             unsafe { ::std::intrinsics::type_name::<T>() },
26             value,
27             param_env,
28         );
29
30         // Erase first before we do the real query -- this keeps the
31         // cache from being too polluted.
32         let value = self.erase_regions(&value);
33         if !value.has_projections() {
34             value
35         } else {
36             value.fold_with(&mut NormalizeAfterErasingRegionsFolder {
37                 tcx: self,
38                 param_env: param_env,
39             })
40         }
41     }
42
43     /// If you have a `Binder<T>`, you can do this to strip out the
44     /// late-bound regions and then normalize the result, yielding up
45     /// a `T` (with regions erased). This is appropriate when the
46     /// binder is being instantiated at the call site.
47     ///
48     /// N.B., currently, higher-ranked type bounds inhibit
49     /// normalization. Therefore, each time we erase them in
50     /// codegen, we need to normalize the contents.
51     pub fn normalize_erasing_late_bound_regions<T>(
52         self,
53         param_env: ty::ParamEnv<'tcx>,
54         value: &ty::Binder<T>,
55     ) -> T
56     where
57         T: TypeFoldable<'tcx>,
58     {
59         assert!(!value.needs_subst());
60         let value = self.erase_late_bound_regions(value);
61         self.normalize_erasing_regions(param_env, value)
62     }
63 }
64
65 struct NormalizeAfterErasingRegionsFolder<'cx, 'tcx: 'cx> {
66     tcx: TyCtxt<'cx, 'tcx, 'tcx>,
67     param_env: ty::ParamEnv<'tcx>,
68 }
69
70 impl<'cx, 'tcx> TypeFolder<'tcx, 'tcx> for NormalizeAfterErasingRegionsFolder<'cx, 'tcx> {
71     fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
72         self.tcx
73     }
74
75     fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
76         self.tcx.normalize_ty_after_erasing_regions(self.param_env.and(ty))
77     }
78 }