Skip to content

Commit ca9b566

Browse files
committed
rustc: move closure upvar types to the closure substs
This moves closures to the (DefId, Substs) scheme like all other items, and saves a word from the size of TyS now that Substs is 2 words.
1 parent f3af8c8 commit ca9b566

File tree

32 files changed

+179
-92
lines changed

32 files changed

+179
-92
lines changed

src/librustc/infer/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1657,7 +1657,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
16571657
{
16581658
if let InferTables::Local(tables) = self.tables {
16591659
if let Some(ty) = tables.borrow().closure_tys.get(&def_id) {
1660-
return ty.subst(self.tcx, substs.func_substs);
1660+
return ty.subst(self.tcx, substs.substs);
16611661
}
16621662
}
16631663

src/librustc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#![feature(rustc_private)]
4141
#![feature(slice_patterns)]
4242
#![feature(staged_api)]
43+
#![feature(unboxed_closures)]
4344
#![cfg_attr(stage0, feature(question_mark))]
4445
#![cfg_attr(test, feature(test))]
4546

src/librustc/traits/select.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,16 +1912,16 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
19121912
tys.to_vec()
19131913
}
19141914

1915-
ty::TyClosure(_, ref substs) => {
1915+
ty::TyClosure(def_id, ref substs) => {
19161916
// FIXME(#27086). We are invariant w/r/t our
1917-
// substs.func_substs, but we don't see them as
1917+
// func_substs, but we don't see them as
19181918
// constituent types; this seems RIGHT but also like
19191919
// something that a normal type couldn't simulate. Is
19201920
// this just a gap with the way that PhantomData and
19211921
// OIBIT interact? That is, there is no way to say
19221922
// "make me invariant with respect to this TYPE, but
19231923
// do not act as though I can reach it"
1924-
substs.upvar_tys.to_vec()
1924+
substs.upvar_tys(def_id, self.tcx()).collect()
19251925
}
19261926

19271927
// for `PhantomData<T>`, we pass `T`

src/librustc/ty/contents.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,11 @@ impl TypeContents {
9898
TC::OwnsOwned | (*self & TC::OwnsAll)
9999
}
100100

101-
pub fn union<T, F>(v: &[T], mut f: F) -> TypeContents where
102-
F: FnMut(&T) -> TypeContents,
101+
pub fn union<I, T, F>(v: I, mut f: F) -> TypeContents where
102+
I: IntoIterator<Item=T>,
103+
F: FnMut(T) -> TypeContents,
103104
{
104-
v.iter().fold(TC::None, |tc, ty| tc | f(ty))
105+
v.into_iter().fold(TC::None, |tc, ty| tc | f(ty))
105106
}
106107

107108
pub fn has_dtor(&self) -> bool {
@@ -215,8 +216,10 @@ impl<'a, 'tcx> ty::TyS<'tcx> {
215216
}
216217
ty::TyStr => TC::None,
217218

218-
ty::TyClosure(_, ref substs) => {
219-
TypeContents::union(&substs.upvar_tys, |ty| tc_ty(tcx, &ty, cache))
219+
ty::TyClosure(def_id, ref substs) => {
220+
TypeContents::union(
221+
substs.upvar_tys(def_id, tcx),
222+
|ty| tc_ty(tcx, &ty, cache))
220223
}
221224

222225
ty::TyTuple(ref tys) => {

src/librustc/ty/context.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,12 +1446,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
14461446

14471447
pub fn mk_closure(self,
14481448
closure_id: DefId,
1449-
substs: &'tcx Substs<'tcx>,
1450-
tys: &[Ty<'tcx>])
1449+
substs: &'tcx Substs<'tcx>)
14511450
-> Ty<'tcx> {
14521451
self.mk_closure_from_closure_substs(closure_id, ClosureSubsts {
1453-
func_substs: substs,
1454-
upvar_tys: self.intern_type_list(tys)
1452+
substs: substs
14551453
})
14561454
}
14571455

@@ -1574,4 +1572,3 @@ impl<T, R, E> InternIteratorElement<T, R> for Result<T, E> {
15741572
Ok(f(&iter.collect::<Result<AccumulateVec<[_; 8]>, _>>()?))
15751573
}
15761574
}
1577-

src/librustc/ty/flags.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ impl FlagComputation {
8888
&ty::TyClosure(_, ref substs) => {
8989
self.add_flags(TypeFlags::HAS_TY_CLOSURE);
9090
self.add_flags(TypeFlags::HAS_LOCAL_NAMES);
91-
self.add_substs(&substs.func_substs);
92-
self.add_tys(&substs.upvar_tys);
91+
self.add_substs(&substs.substs);
9392
}
9493

9594
&ty::TyInfer(infer) => {

src/librustc/ty/layout.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,9 @@ impl<'a, 'gcx, 'tcx> Struct {
631631

632632
// Perhaps one of the upvars of this closure is non-zero
633633
// Let's recurse and find out!
634-
(_, &ty::TyClosure(_, ty::ClosureSubsts { upvar_tys: tys, .. })) |
634+
(_, &ty::TyClosure(def_id, ref substs)) => {
635+
Struct::non_zero_field_path(infcx, substs.upvar_tys(def_id, tcx))
636+
}
635637
// Can we use one of the fields in this tuple?
636638
(_, &ty::TyTuple(tys)) => {
637639
Struct::non_zero_field_path(infcx, tys.iter().cloned())
@@ -961,7 +963,13 @@ impl<'a, 'gcx, 'tcx> Layout {
961963
}
962964

963965
// Tuples and closures.
964-
ty::TyClosure(_, ty::ClosureSubsts { upvar_tys: tys, .. }) |
966+
ty::TyClosure(def_id, ref substs) => {
967+
let mut st = Struct::new(dl, false);
968+
let tys = substs.upvar_tys(def_id, tcx);
969+
st.extend(dl, tys.map(|ty| ty.layout(infcx)), ty)?;
970+
Univariant { variant: st, non_zero: false }
971+
}
972+
965973
ty::TyTuple(tys) => {
966974
let mut st = Struct::new(dl, false);
967975
st.extend(dl, tys.iter().map(|ty| ty.layout(infcx)), ty)?;

src/librustc/ty/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2544,12 +2544,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
25442544
// tables by typeck; else, it will be retreived from
25452545
// the external crate metadata.
25462546
if let Some(ty) = self.tables.borrow().closure_tys.get(&def_id) {
2547-
return ty.subst(self, substs.func_substs);
2547+
return ty.subst(self, substs.substs);
25482548
}
25492549

25502550
let ty = self.sess.cstore.closure_ty(self.global_tcx(), def_id);
25512551
self.tables.borrow_mut().closure_tys.insert(def_id, ty.clone());
2552-
ty.subst(self, substs.func_substs)
2552+
ty.subst(self, substs.substs)
25532553
}
25542554

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

src/librustc/ty/outlives.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
7272
// in the `subtys` iterator (e.g., when encountering a
7373
// projection).
7474
match ty.sty {
75-
ty::TyClosure(_, ref substs) => {
75+
ty::TyClosure(def_id, ref substs) => {
7676
// FIXME(#27086). We do not accumulate from substs, since they
7777
// don't represent reachable data. This means that, in
7878
// practice, some of the lifetime parameters might not
@@ -110,7 +110,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
110110
// what func/type parameters are used and unused,
111111
// taking into consideration UFCS and so forth.
112112

113-
for &upvar_ty in substs.upvar_tys {
113+
for upvar_ty in substs.upvar_tys(def_id, *self) {
114114
self.compute_components(upvar_ty, out);
115115
}
116116
}

src/librustc/ty/relate.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -534,13 +534,8 @@ impl<'tcx> Relate<'tcx> for ty::ClosureSubsts<'tcx> {
534534
-> RelateResult<'tcx, ty::ClosureSubsts<'tcx>>
535535
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
536536
{
537-
let substs = relate_substs(relation, None, a.func_substs, b.func_substs)?;
538-
assert_eq!(a.upvar_tys.len(), b.upvar_tys.len());
539-
Ok(ty::ClosureSubsts {
540-
func_substs: substs,
541-
upvar_tys: relation.tcx().mk_type_list(
542-
a.upvar_tys.iter().zip(b.upvar_tys).map(|(a, b)| relation.relate(a, b)))?
543-
})
537+
let substs = relate_substs(relation, None, a.substs, b.substs)?;
538+
Ok(ty::ClosureSubsts { substs: substs })
544539
}
545540
}
546541

0 commit comments

Comments
 (0)