Skip to content

Commit

Permalink
cache projections in trans
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Aug 31, 2016
1 parent c5be6f6 commit 7057c42
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions src/librustc_trans/monomorphize.rs
Expand Up @@ -8,13 +8,14 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use common::*;
use rustc::hir::def_id::DefId;
use rustc::infer::TransNormalize;
use rustc::ty::fold::{TypeFolder, TypeFoldable};
use rustc::ty::subst::{Subst, Substs};
use rustc::ty::{self, Ty, TyCtxt};
use common::*;
use rustc::util::ppaux;

use rustc::util::common::MemoizationMap;
use std::fmt;

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


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

struct AssociatedTypeNormalizer<'a, 'b: 'a, 'gcx: 'b> {
shared: &'a SharedCrateContext<'b, 'gcx>,
}

impl<'a, 'b, 'gcx> AssociatedTypeNormalizer<'a, 'b, 'gcx> {
fn new(shared: &'a SharedCrateContext<'b, 'gcx>) -> Self {
AssociatedTypeNormalizer {
shared: shared,
}
}

fn fold<T:TypeFoldable<'gcx>>(&mut self, value: &T) -> T {
if !value.has_projection_types() {
value.clone()
} else {
value.fold_with(self)
}
}
}

impl<'a, 'b, 'gcx> TypeFolder<'gcx, 'gcx> for AssociatedTypeNormalizer<'a, 'b, 'gcx> {
fn tcx<'c>(&'c self) -> TyCtxt<'c, 'gcx, 'gcx> {
self.shared.tcx()
}

fn fold_ty(&mut self, ty: Ty<'gcx>) -> Ty<'gcx> {
if !ty.has_projection_types() {
ty
} else {
self.shared.project_cache().memoize(ty, || {
debug!("AssociatedTypeNormalizer: ty={:?}", ty);
self.shared.tcx().normalize_associated_type(&ty)
})
}
}
}

0 comments on commit 7057c42

Please sign in to comment.