Skip to content

Commit 7057c42

Browse files
committed
cache projections in trans
1 parent c5be6f6 commit 7057c42

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

src/librustc_trans/monomorphize.rs

+41-6
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use common::*;
1112
use rustc::hir::def_id::DefId;
1213
use rustc::infer::TransNormalize;
14+
use rustc::ty::fold::{TypeFolder, TypeFoldable};
1315
use rustc::ty::subst::{Subst, Substs};
1416
use rustc::ty::{self, Ty, TyCtxt};
15-
use common::*;
1617
use rustc::util::ppaux;
17-
18+
use rustc::util::common::MemoizationMap;
1819
use std::fmt;
1920

2021
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
@@ -51,10 +52,8 @@ pub fn apply_param_substs<'a, 'tcx, T>(scx: &SharedCrateContext<'a, 'tcx>,
5152
let tcx = scx.tcx();
5253
debug!("apply_param_substs(param_substs={:?}, value={:?})", param_substs, value);
5354
let substituted = value.subst(tcx, param_substs);
54-
debug!("apply_param_substs: substituted={:?}{}",
55-
substituted,
56-
if substituted.has_projection_types() { " [needs projection]" } else { "" });
57-
tcx.normalize_associated_type(&substituted)
55+
let substituted = scx.tcx().erase_regions(&substituted);
56+
AssociatedTypeNormalizer::new(scx).fold(&substituted)
5857
}
5958

6059

@@ -67,3 +66,39 @@ pub fn field_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
6766
tcx.normalize_associated_type(&f.ty(tcx, param_substs))
6867
}
6968

69+
struct AssociatedTypeNormalizer<'a, 'b: 'a, 'gcx: 'b> {
70+
shared: &'a SharedCrateContext<'b, 'gcx>,
71+
}
72+
73+
impl<'a, 'b, 'gcx> AssociatedTypeNormalizer<'a, 'b, 'gcx> {
74+
fn new(shared: &'a SharedCrateContext<'b, 'gcx>) -> Self {
75+
AssociatedTypeNormalizer {
76+
shared: shared,
77+
}
78+
}
79+
80+
fn fold<T:TypeFoldable<'gcx>>(&mut self, value: &T) -> T {
81+
if !value.has_projection_types() {
82+
value.clone()
83+
} else {
84+
value.fold_with(self)
85+
}
86+
}
87+
}
88+
89+
impl<'a, 'b, 'gcx> TypeFolder<'gcx, 'gcx> for AssociatedTypeNormalizer<'a, 'b, 'gcx> {
90+
fn tcx<'c>(&'c self) -> TyCtxt<'c, 'gcx, 'gcx> {
91+
self.shared.tcx()
92+
}
93+
94+
fn fold_ty(&mut self, ty: Ty<'gcx>) -> Ty<'gcx> {
95+
if !ty.has_projection_types() {
96+
ty
97+
} else {
98+
self.shared.project_cache().memoize(ty, || {
99+
debug!("AssociatedTypeNormalizer: ty={:?}", ty);
100+
self.shared.tcx().normalize_associated_type(&ty)
101+
})
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)