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(...)`.
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.)
10 use crate::ty::{self, Ty, TyCtxt};
11 use crate::ty::fold::{TypeFoldable, TypeFolder};
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.
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
21 T: TypeFoldable<'tcx>,
24 "normalize_erasing_regions::<{}>(value={:?}, param_env={:?})",
25 unsafe { ::std::intrinsics::type_name::<T>() },
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() {
36 value.fold_with(&mut NormalizeAfterErasingRegionsFolder {
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.
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>(
53 param_env: ty::ParamEnv<'tcx>,
54 value: &ty::Binder<T>,
57 T: TypeFoldable<'tcx>,
59 assert!(!value.needs_subst());
60 let value = self.erase_late_bound_regions(value);
61 self.normalize_erasing_regions(param_env, value)
65 struct NormalizeAfterErasingRegionsFolder<'cx, 'tcx: 'cx> {
66 tcx: TyCtxt<'cx, 'tcx, 'tcx>,
67 param_env: ty::ParamEnv<'tcx>,
70 impl<'cx, 'tcx> TypeFolder<'tcx, 'tcx> for NormalizeAfterErasingRegionsFolder<'cx, 'tcx> {
71 fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
75 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
76 self.tcx.normalize_ty_after_erasing_regions(self.param_env.and(ty))