Skip to content

Commit

Permalink
rustc: simplify tcx.closure_type(...) as it can copy the cached values.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Feb 25, 2017
1 parent 91374f8 commit 3146ee8
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 34 deletions.
12 changes: 4 additions & 8 deletions src/librustc/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1649,20 +1649,16 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
Some(self.tcx.closure_kind(def_id))
}

pub fn closure_type(&self,
def_id: DefId,
substs: ty::ClosureSubsts<'tcx>)
-> ty::PolyFnSig<'tcx>
{
pub fn closure_type(&self, def_id: DefId) -> ty::PolyFnSig<'tcx> {
if let InferTables::InProgress(tables) = self.tables {
if let Some(id) = self.tcx.hir.as_local_node_id(def_id) {
if let Some(ty) = tables.borrow().closure_tys.get(&id) {
return ty.subst(self.tcx, substs.substs);
if let Some(&ty) = tables.borrow().closure_tys.get(&id) {
return ty;
}
}
}

self.tcx.closure_type(def_id, substs)
self.tcx.closure_type(def_id)
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,12 +1432,16 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
body: &hir::Body)
{
let fn_ty = self.ir.tcx.item_type(self.ir.tcx.hir.local_def_id(id));
let fn_ret = match fn_ty.sty {
ty::TyClosure(closure_def_id, substs) =>
self.ir.tcx.closure_type(closure_def_id, substs).output(),
_ => fn_ty.fn_ret()
let fn_sig = match fn_ty.sty {
ty::TyClosure(closure_def_id, substs) => {
self.ir.tcx.closure_type(closure_def_id)
.subst(self.ir.tcx, substs.substs)
}
_ => fn_ty.fn_sig()
};

let fn_ret = fn_sig.output();

// within the fn body, late-bound regions are liberated
// and must outlive the *call-site* of the function.
let fn_ret =
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,8 @@ fn confirm_closure_candidate<'cx, 'gcx, 'tcx>(
-> Progress<'tcx>
{
let closure_typer = selcx.closure_typer();
let closure_type = closure_typer.closure_type(vtable.closure_def_id, vtable.substs);
let closure_type = closure_typer.closure_type(vtable.closure_def_id)
.subst(selcx.tcx(), vtable.substs.substs);
let Normalized {
value: closure_type,
obligations
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/traits/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2779,7 +2779,8 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
substs: ty::ClosureSubsts<'tcx>)
-> ty::PolyTraitRef<'tcx>
{
let closure_type = self.infcx.closure_type(closure_def_id, substs);
let closure_type = self.infcx.closure_type(closure_def_id)
.subst(self.tcx(), substs.substs);
let ty::Binder((trait_ref, _)) =
self.tcx().closure_trait_ref_and_return_type(obligation.predicate.def_id(),
obligation.predicate.0.self_ty(), // (1)
Expand Down
12 changes: 2 additions & 10 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2467,16 +2467,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
self.maps.closure_kind(self, def_id)
}

pub fn closure_type(self,
def_id: DefId,
substs: ClosureSubsts<'tcx>)
-> ty::PolyFnSig<'tcx>
{
if let Some(ty) = self.maps.closure_type.borrow().get(&def_id) {
return ty.subst(self, substs.substs);
}

self.maps.closure_type(self, def_id).subst(self, substs.substs)
pub fn closure_type(self, def_id: DefId) -> ty::PolyFnSig<'tcx> {
self.maps.closure_type(self, def_id)
}

/// Given the def_id of an impl, return the def_id of the trait it implements.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {

let data = ClosureData {
kind: tcx.closure_kind(def_id),
ty: self.lazy(&tcx.maps.closure_type.borrow()[&def_id]),
ty: self.lazy(&tcx.closure_type(def_id)),
};

Entry {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_trans/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub use self::CalleeData::*;

use llvm::{self, ValueRef, get_params};
use rustc::hir::def_id::DefId;
use rustc::ty::subst::Substs;
use rustc::ty::subst::{Substs, Subst};
use rustc::traits;
use abi::{Abi, FnType};
use attributes;
Expand Down Expand Up @@ -306,7 +306,7 @@ fn trans_fn_once_adapter_shim<'a, 'tcx>(
let ref_closure_ty = tcx.mk_imm_ref(tcx.mk_region(ty::ReErased), closure_ty);

// Make a version with the type of by-ref closure.
let sig = tcx.closure_type(def_id, substs);
let sig = tcx.closure_type(def_id).subst(tcx, substs.substs);
let sig = tcx.erase_late_bound_regions_and_normalize(&sig);
assert_eq!(sig.abi, Abi::RustCall);
let llref_fn_ty = tcx.mk_fn_ptr(ty::Binder(tcx.mk_fn_sig(
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_trans/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use type_::Type;
use value::Value;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::layout::Layout;
use rustc::ty::subst::Subst;
use rustc::traits::{self, SelectionContext, Reveal};
use rustc::hir;

Expand Down Expand Up @@ -579,7 +580,7 @@ pub fn ty_fn_sig<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
ty::TyFnPtr(sig) => sig,
ty::TyClosure(def_id, substs) => {
let tcx = ccx.tcx();
let sig = tcx.closure_type(def_id, substs);
let sig = tcx.closure_type(def_id).subst(tcx, substs.substs);

let env_region = ty::ReLateBound(ty::DebruijnIndex::new(1), ty::BrEnv);
let env_ty = match tcx.closure_kind(def_id) {
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_trans/mir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use rustc::mir;
use rustc::mir::tcx::LvalueTy;
use rustc::ty::{self, layout, Ty, TyCtxt, TypeFoldable};
use rustc::ty::cast::{CastTy, IntTy};
use rustc::ty::subst::{Kind, Substs};
use rustc::ty::subst::{Kind, Substs, Subst};
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
use {abi, adt, base, Disr, machine};
use callee::Callee;
Expand Down Expand Up @@ -588,7 +588,8 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
.find(|it| it.kind == ty::AssociatedKind::Method)
.unwrap().def_id;
// Now create its substs [Closure, Tuple]
let input = tcx.closure_type(def_id, substs).input(0);
let input = tcx.closure_type(def_id)
.subst(tcx, substs.substs).input(0);
let substs = tcx.mk_substs([operand.ty, input.skip_binder()]
.iter().cloned().map(Kind::from));
Callee::def(self.ccx, call_once, substs)
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_trans/mir/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use llvm::{self, ValueRef};
use rustc::ty::{self, Ty};
use rustc::ty::cast::{CastTy, IntTy};
use rustc::ty::layout::Layout;
use rustc::ty::subst::Kind;
use rustc::ty::subst::{Kind, Subst};
use rustc::mir::tcx::LvalueTy;
use rustc::mir;
use middle::lang_items::ExchangeMallocFnLangItem;
Expand Down Expand Up @@ -201,7 +201,8 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
.find(|it| it.kind == ty::AssociatedKind::Method)
.unwrap().def_id;
// Now create its substs [Closure, Tuple]
let input = bcx.tcx().closure_type(def_id, substs).input(0);
let input = bcx.tcx().closure_type(def_id)
.subst(bcx.tcx(), substs.substs).input(0);
let substs = bcx.tcx().mk_substs([operand.ty, input.skip_binder()]
.iter().cloned().map(Kind::from));
OperandValue::Immediate(
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_typeck/check/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use hir::def::Def;
use hir::def_id::{DefId, LOCAL_CRATE};
use rustc::{infer, traits};
use rustc::ty::{self, TyCtxt, LvaluePreference, Ty};
use rustc::ty::subst::Subst;
use syntax::abi;
use syntax::symbol::Symbol;
use syntax_pos::Span;
Expand Down Expand Up @@ -110,7 +111,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
// haven't yet decided on whether the closure is fn vs
// fnmut vs fnonce. If so, we have to defer further processing.
if self.closure_kind(def_id).is_none() {
let closure_ty = self.closure_type(def_id, substs);
let closure_ty = self.closure_type(def_id).subst(self.tcx, substs.substs);
let fn_sig = self.replace_late_bound_regions_with_fresh_var(call_expr.span,
infer::FnCall,
&closure_ty)
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_typeck/check/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ use rustc::ty::{self, LvaluePreference, TypeAndMut,
use rustc::ty::fold::TypeFoldable;
use rustc::ty::error::TypeError;
use rustc::ty::relate::RelateResult;
use rustc::ty::subst::Subst;
use syntax::abi;
use syntax::feature_gate;
use util::common::indent;
Expand Down Expand Up @@ -587,7 +588,7 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
// `extern "rust-call" fn((arg0,arg1,...)) -> _`
// to
// `fn(arg0,arg1,...) -> _`
let sig = self.closure_type(def_id_a, substs_a);
let sig = self.closure_type(def_id_a).subst(self.tcx, substs_a.substs);
let converted_sig = sig.map_bound(|s| {
let params_iter = match s.inputs()[0].sty {
ty::TyTuple(params, _) => {
Expand Down

0 comments on commit 3146ee8

Please sign in to comment.