|
| 1 | +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +//! Methods for normalizing when you don't care about regions (and |
| 12 | +//! aren't doing type inference). If either of those things don't |
| 13 | +//! apply to you, use `infcx.normalize(...)`. |
| 14 | +//! |
| 15 | +//! The methods in this file use a `TypeFolder` to recursively process |
| 16 | +//! contents, invoking the underlying |
| 17 | +//! `normalize_ty_after_erasing_regions` query for each type found |
| 18 | +//! within. (This underlying query is what is cached.) |
| 19 | +
|
| 20 | +use ty::{self, Ty, TyCtxt}; |
| 21 | +use ty::fold::{TypeFoldable, TypeFolder}; |
| 22 | + |
| 23 | +impl<'cx, 'tcx> TyCtxt<'cx, 'tcx, 'tcx> { |
| 24 | + /// Erase the regions in `value` and then fully normalize all the |
| 25 | + /// types found within. The result will also have regions erased. |
| 26 | + /// |
| 27 | + /// This is appropriate to use only after type-check: it assumes |
| 28 | + /// that normalization will succeed, for example. |
| 29 | + pub fn normalize_erasing_regions<T>(self, param_env: ty::ParamEnv<'tcx>, value: T) -> T |
| 30 | + where |
| 31 | + T: TypeFoldable<'tcx>, |
| 32 | + { |
| 33 | + // Erase first before we do the real query -- this keeps the |
| 34 | + // cache from being too polluted. |
| 35 | + let value = self.erase_regions(&value); |
| 36 | + if !value.has_projections() { |
| 37 | + value |
| 38 | + } else { |
| 39 | + value.fold_with(&mut NormalizeAfterErasingRegionsFolder { |
| 40 | + tcx: self, |
| 41 | + param_env: param_env, |
| 42 | + }) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// If you have a `Binder<T>`, you can do this to strip out the |
| 47 | + /// late-bound regions and then normalize the result, yielding up |
| 48 | + /// a `T` (with regions erased). This is appropriate when the |
| 49 | + /// binder is being instantiated at the call site. |
| 50 | + /// |
| 51 | + /// NB. Currently, higher-ranked type bounds inhibit |
| 52 | + /// normalization. Therefore, each time we erase them in |
| 53 | + /// translation, we need to normalize the contents. |
| 54 | + pub fn normalize_erasing_late_bound_regions<T>( |
| 55 | + self, |
| 56 | + param_env: ty::ParamEnv<'tcx>, |
| 57 | + value: &ty::Binder<T>, |
| 58 | + ) -> T |
| 59 | + where |
| 60 | + T: TypeFoldable<'tcx>, |
| 61 | + { |
| 62 | + assert!(!value.needs_subst()); |
| 63 | + let value = self.erase_late_bound_regions(value); |
| 64 | + self.normalize_erasing_regions(param_env, value) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +struct NormalizeAfterErasingRegionsFolder<'cx, 'tcx: 'cx> { |
| 69 | + tcx: TyCtxt<'cx, 'tcx, 'tcx>, |
| 70 | + param_env: ty::ParamEnv<'tcx>, |
| 71 | +} |
| 72 | + |
| 73 | +impl<'cx, 'tcx> TypeFolder<'tcx, 'tcx> for NormalizeAfterErasingRegionsFolder<'cx, 'tcx> { |
| 74 | + fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> { |
| 75 | + self.tcx |
| 76 | + } |
| 77 | + |
| 78 | + fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> { |
| 79 | + self.tcx.normalize_ty_after_erasing_regions(self.param_env.and(ty)) |
| 80 | + } |
| 81 | +} |
0 commit comments