Skip to content

Commit 3146ee8

Browse files
committed
rustc: simplify tcx.closure_type(...) as it can copy the cached values.
1 parent 91374f8 commit 3146ee8

File tree

12 files changed

+33
-34
lines changed

12 files changed

+33
-34
lines changed

src/librustc/infer/mod.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1649,20 +1649,16 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
16491649
Some(self.tcx.closure_kind(def_id))
16501650
}
16511651

1652-
pub fn closure_type(&self,
1653-
def_id: DefId,
1654-
substs: ty::ClosureSubsts<'tcx>)
1655-
-> ty::PolyFnSig<'tcx>
1656-
{
1652+
pub fn closure_type(&self, def_id: DefId) -> ty::PolyFnSig<'tcx> {
16571653
if let InferTables::InProgress(tables) = self.tables {
16581654
if let Some(id) = self.tcx.hir.as_local_node_id(def_id) {
1659-
if let Some(ty) = tables.borrow().closure_tys.get(&id) {
1660-
return ty.subst(self.tcx, substs.substs);
1655+
if let Some(&ty) = tables.borrow().closure_tys.get(&id) {
1656+
return ty;
16611657
}
16621658
}
16631659
}
16641660

1665-
self.tcx.closure_type(def_id, substs)
1661+
self.tcx.closure_type(def_id)
16661662
}
16671663
}
16681664

src/librustc/middle/liveness.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1432,12 +1432,16 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
14321432
body: &hir::Body)
14331433
{
14341434
let fn_ty = self.ir.tcx.item_type(self.ir.tcx.hir.local_def_id(id));
1435-
let fn_ret = match fn_ty.sty {
1436-
ty::TyClosure(closure_def_id, substs) =>
1437-
self.ir.tcx.closure_type(closure_def_id, substs).output(),
1438-
_ => fn_ty.fn_ret()
1435+
let fn_sig = match fn_ty.sty {
1436+
ty::TyClosure(closure_def_id, substs) => {
1437+
self.ir.tcx.closure_type(closure_def_id)
1438+
.subst(self.ir.tcx, substs.substs)
1439+
}
1440+
_ => fn_ty.fn_sig()
14391441
};
14401442

1443+
let fn_ret = fn_sig.output();
1444+
14411445
// within the fn body, late-bound regions are liberated
14421446
// and must outlive the *call-site* of the function.
14431447
let fn_ret =

src/librustc/traits/project.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,8 @@ fn confirm_closure_candidate<'cx, 'gcx, 'tcx>(
12081208
-> Progress<'tcx>
12091209
{
12101210
let closure_typer = selcx.closure_typer();
1211-
let closure_type = closure_typer.closure_type(vtable.closure_def_id, vtable.substs);
1211+
let closure_type = closure_typer.closure_type(vtable.closure_def_id)
1212+
.subst(selcx.tcx(), vtable.substs.substs);
12121213
let Normalized {
12131214
value: closure_type,
12141215
obligations

src/librustc/traits/select.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2779,7 +2779,8 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
27792779
substs: ty::ClosureSubsts<'tcx>)
27802780
-> ty::PolyTraitRef<'tcx>
27812781
{
2782-
let closure_type = self.infcx.closure_type(closure_def_id, substs);
2782+
let closure_type = self.infcx.closure_type(closure_def_id)
2783+
.subst(self.tcx(), substs.substs);
27832784
let ty::Binder((trait_ref, _)) =
27842785
self.tcx().closure_trait_ref_and_return_type(obligation.predicate.def_id(),
27852786
obligation.predicate.0.self_ty(), // (1)

src/librustc/ty/mod.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -2467,16 +2467,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
24672467
self.maps.closure_kind(self, def_id)
24682468
}
24692469

2470-
pub fn closure_type(self,
2471-
def_id: DefId,
2472-
substs: ClosureSubsts<'tcx>)
2473-
-> ty::PolyFnSig<'tcx>
2474-
{
2475-
if let Some(ty) = self.maps.closure_type.borrow().get(&def_id) {
2476-
return ty.subst(self, substs.substs);
2477-
}
2478-
2479-
self.maps.closure_type(self, def_id).subst(self, substs.substs)
2470+
pub fn closure_type(self, def_id: DefId) -> ty::PolyFnSig<'tcx> {
2471+
self.maps.closure_type(self, def_id)
24802472
}
24812473

24822474
/// Given the def_id of an impl, return the def_id of the trait it implements.

src/librustc_metadata/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
10961096

10971097
let data = ClosureData {
10981098
kind: tcx.closure_kind(def_id),
1099-
ty: self.lazy(&tcx.maps.closure_type.borrow()[&def_id]),
1099+
ty: self.lazy(&tcx.closure_type(def_id)),
11001100
};
11011101

11021102
Entry {

src/librustc_trans/callee.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub use self::CalleeData::*;
1818

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

308308
// Make a version with the type of by-ref closure.
309-
let sig = tcx.closure_type(def_id, substs);
309+
let sig = tcx.closure_type(def_id).subst(tcx, substs.substs);
310310
let sig = tcx.erase_late_bound_regions_and_normalize(&sig);
311311
assert_eq!(sig.abi, Abi::RustCall);
312312
let llref_fn_ty = tcx.mk_fn_ptr(ty::Binder(tcx.mk_fn_sig(

src/librustc_trans/common.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use type_::Type;
2929
use value::Value;
3030
use rustc::ty::{self, Ty, TyCtxt};
3131
use rustc::ty::layout::Layout;
32+
use rustc::ty::subst::Subst;
3233
use rustc::traits::{self, SelectionContext, Reveal};
3334
use rustc::hir;
3435

@@ -579,7 +580,7 @@ pub fn ty_fn_sig<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
579580
ty::TyFnPtr(sig) => sig,
580581
ty::TyClosure(def_id, substs) => {
581582
let tcx = ccx.tcx();
582-
let sig = tcx.closure_type(def_id, substs);
583+
let sig = tcx.closure_type(def_id).subst(tcx, substs.substs);
583584

584585
let env_region = ty::ReLateBound(ty::DebruijnIndex::new(1), ty::BrEnv);
585586
let env_ty = match tcx.closure_kind(def_id) {

src/librustc_trans/mir/constant.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc::mir;
2020
use rustc::mir::tcx::LvalueTy;
2121
use rustc::ty::{self, layout, Ty, TyCtxt, TypeFoldable};
2222
use rustc::ty::cast::{CastTy, IntTy};
23-
use rustc::ty::subst::{Kind, Substs};
23+
use rustc::ty::subst::{Kind, Substs, Subst};
2424
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
2525
use {abi, adt, base, Disr, machine};
2626
use callee::Callee;
@@ -588,7 +588,8 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
588588
.find(|it| it.kind == ty::AssociatedKind::Method)
589589
.unwrap().def_id;
590590
// Now create its substs [Closure, Tuple]
591-
let input = tcx.closure_type(def_id, substs).input(0);
591+
let input = tcx.closure_type(def_id)
592+
.subst(tcx, substs.substs).input(0);
592593
let substs = tcx.mk_substs([operand.ty, input.skip_binder()]
593594
.iter().cloned().map(Kind::from));
594595
Callee::def(self.ccx, call_once, substs)

src/librustc_trans/mir/rvalue.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use llvm::{self, ValueRef};
1212
use rustc::ty::{self, Ty};
1313
use rustc::ty::cast::{CastTy, IntTy};
1414
use rustc::ty::layout::Layout;
15-
use rustc::ty::subst::Kind;
15+
use rustc::ty::subst::{Kind, Subst};
1616
use rustc::mir::tcx::LvalueTy;
1717
use rustc::mir;
1818
use middle::lang_items::ExchangeMallocFnLangItem;
@@ -201,7 +201,8 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
201201
.find(|it| it.kind == ty::AssociatedKind::Method)
202202
.unwrap().def_id;
203203
// Now create its substs [Closure, Tuple]
204-
let input = bcx.tcx().closure_type(def_id, substs).input(0);
204+
let input = bcx.tcx().closure_type(def_id)
205+
.subst(bcx.tcx(), substs.substs).input(0);
205206
let substs = bcx.tcx().mk_substs([operand.ty, input.skip_binder()]
206207
.iter().cloned().map(Kind::from));
207208
OperandValue::Immediate(

src/librustc_typeck/check/callee.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use hir::def::Def;
1414
use hir::def_id::{DefId, LOCAL_CRATE};
1515
use rustc::{infer, traits};
1616
use rustc::ty::{self, TyCtxt, LvaluePreference, Ty};
17+
use rustc::ty::subst::Subst;
1718
use syntax::abi;
1819
use syntax::symbol::Symbol;
1920
use syntax_pos::Span;
@@ -110,7 +111,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
110111
// haven't yet decided on whether the closure is fn vs
111112
// fnmut vs fnonce. If so, we have to defer further processing.
112113
if self.closure_kind(def_id).is_none() {
113-
let closure_ty = self.closure_type(def_id, substs);
114+
let closure_ty = self.closure_type(def_id).subst(self.tcx, substs.substs);
114115
let fn_sig = self.replace_late_bound_regions_with_fresh_var(call_expr.span,
115116
infer::FnCall,
116117
&closure_ty)

src/librustc_typeck/check/coercion.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ use rustc::ty::{self, LvaluePreference, TypeAndMut,
7272
use rustc::ty::fold::TypeFoldable;
7373
use rustc::ty::error::TypeError;
7474
use rustc::ty::relate::RelateResult;
75+
use rustc::ty::subst::Subst;
7576
use syntax::abi;
7677
use syntax::feature_gate;
7778
use util::common::indent;
@@ -587,7 +588,7 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
587588
// `extern "rust-call" fn((arg0,arg1,...)) -> _`
588589
// to
589590
// `fn(arg0,arg1,...) -> _`
590-
let sig = self.closure_type(def_id_a, substs_a);
591+
let sig = self.closure_type(def_id_a).subst(self.tcx, substs_a.substs);
591592
let converted_sig = sig.map_bound(|s| {
592593
let params_iter = match s.inputs()[0].sty {
593594
ty::TyTuple(params, _) => {

0 commit comments

Comments
 (0)