From bdc1bfd8f1740461d2ee39ba41f5245ed8913a2a Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 7 Jan 2015 09:36:11 -0500 Subject: [PATCH] Rename common::normalize to common::erase_regions --- src/librustc/middle/ty_fold.rs | 5 ++++- src/librustc_trans/trans/callee.rs | 2 +- src/librustc_trans/trans/closure.rs | 2 +- src/librustc_trans/trans/common.rs | 22 +++++++++++++--------- src/librustc_trans/trans/type_of.rs | 2 +- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/librustc/middle/ty_fold.rs b/src/librustc/middle/ty_fold.rs index 424e357d8d45..e18e62948fb6 100644 --- a/src/librustc/middle/ty_fold.rs +++ b/src/librustc/middle/ty_fold.rs @@ -853,7 +853,10 @@ impl<'a, 'tcx> TypeFolder<'tcx> for RegionFolder<'a, 'tcx> /////////////////////////////////////////////////////////////////////////// // Region eraser // -// Replaces all free regions with 'static. Useful in trans. +// Replaces all free regions with 'static. Useful in contexts, such as +// method probing, where precise region relationships are not +// important. Note that in trans you should use +// `common::erase_regions` instead. pub struct RegionEraser<'a, 'tcx: 'a> { tcx: &'a ty::ctxt<'tcx>, diff --git a/src/librustc_trans/trans/callee.rs b/src/librustc_trans/trans/callee.rs index b7b486f1d0a5..4cfd831a3251 100644 --- a/src/librustc_trans/trans/callee.rs +++ b/src/librustc_trans/trans/callee.rs @@ -265,7 +265,7 @@ pub fn trans_fn_pointer_shim<'a, 'tcx>( let _icx = push_ctxt("trans_fn_pointer_shim"); let tcx = ccx.tcx(); - let bare_fn_ty = normalize_ty(tcx, bare_fn_ty); + let bare_fn_ty = erase_regions(tcx, &bare_fn_ty); match ccx.fn_pointer_shims().borrow().get(&bare_fn_ty) { Some(&llval) => { return llval; } None => { } diff --git a/src/librustc_trans/trans/closure.rs b/src/librustc_trans/trans/closure.rs index ad2ed67b22c9..06a1182b1f43 100644 --- a/src/librustc_trans/trans/closure.rs +++ b/src/librustc_trans/trans/closure.rs @@ -466,7 +466,7 @@ pub fn get_or_create_declaration_if_unboxed_closure<'a, 'tcx>(ccx: &CrateContext // Normalize type so differences in regions and typedefs don't cause // duplicate declarations - let function_type = normalize_ty(ccx.tcx(), function_type); + let function_type = erase_regions(ccx.tcx(), &function_type); let params = match function_type.sty { ty::ty_unboxed_closure(_, _, ref substs) => substs.types.clone(), _ => unreachable!() diff --git a/src/librustc_trans/trans/common.rs b/src/librustc_trans/trans/common.rs index 237fc1856369..bdec9ad378b2 100644 --- a/src/librustc_trans/trans/common.rs +++ b/src/librustc_trans/trans/common.rs @@ -58,16 +58,21 @@ use util::nodemap::FnvHashSet; pub use trans::context::CrateContext; -/// Returns an equivalent type with all the typedefs and self regions removed. -pub fn normalize_ty<'tcx>(cx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> { - let u = TypeNormalizer(cx).fold_ty(ty); - debug!("normalize_ty({}) = {}", - ty.repr(cx), u.repr(cx)); - return u; +/// Returns an equivalent value with all free regions removed (note +/// that late-bound regions remain, because they are important for +/// subtyping, but they are anonymized and normalized as well). This +/// is a stronger, caching version of `ty_fold::erase_regions`. +pub fn erase_regions<'tcx,T>(cx: &ty::ctxt<'tcx>, value: &T) -> T + where T : TypeFoldable<'tcx> + Repr<'tcx> +{ + let value1 = value.fold_with(&mut RegionEraser(cx)); + debug!("erase_regions({}) = {}", + value.repr(cx), value1.repr(cx)); + return value1; - struct TypeNormalizer<'a, 'tcx: 'a>(&'a ty::ctxt<'tcx>); + struct RegionEraser<'a, 'tcx: 'a>(&'a ty::ctxt<'tcx>); - impl<'a, 'tcx> TypeFolder<'tcx> for TypeNormalizer<'a, 'tcx> { + impl<'a, 'tcx> TypeFolder<'tcx> for RegionEraser<'a, 'tcx> { fn tcx(&self) -> &ty::ctxt<'tcx> { self.0 } fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> { @@ -84,7 +89,6 @@ pub fn normalize_ty<'tcx>(cx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> { fn fold_binder(&mut self, t: &ty::Binder) -> ty::Binder where T : TypeFoldable<'tcx> + Repr<'tcx> { - // FIXME(#20526) this should replace `enter_region_binder`/`exit_region_binder`. let u = ty::anonymize_late_bound_regions(self.tcx(), t); ty_fold::super_fold_binder(self, &u) } diff --git a/src/librustc_trans/trans/type_of.rs b/src/librustc_trans/trans/type_of.rs index 19d50cdd4832..ef5394f06b3b 100644 --- a/src/librustc_trans/trans/type_of.rs +++ b/src/librustc_trans/trans/type_of.rs @@ -285,7 +285,7 @@ pub fn type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> Type { // Rust types are defined as the same LLVM types. If we don't do // this then, e.g. `Option<{myfield: bool}>` would be a different // type than `Option`. - let t_norm = normalize_ty(cx.tcx(), t); + let t_norm = erase_regions(cx.tcx(), &t); if t != t_norm { let llty = type_of(cx, t_norm);