Skip to content

Commit c4b1500

Browse files
committed
Rollup merge of #22966 - nikomatsakis:closure-region-hierarchy, r=pnkfelix
Remove the synthetic \"region bound\" from closures and instead update how type-outlives works for closure types so that it ensures that all upvars outlive the region in question. This gives the same guarantees but without introducing artificial regions (and gives better error messages to boot). This is refactoring towards #3696. r? @pnkfelix
2 parents f608afe + 00fcf79 commit c4b1500

28 files changed

+93
-230
lines changed

src/librustc/metadata/tydecode.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,11 +555,9 @@ fn parse_ty_<'a, 'tcx, F>(st: &mut PState<'a, 'tcx>, conv: &mut F) -> Ty<'tcx> w
555555
'k' => {
556556
assert_eq!(next(st), '[');
557557
let did = parse_def_(st, ClosureSource, conv);
558-
let region = parse_region_(st, conv);
559558
let substs = parse_substs_(st, conv);
560559
assert_eq!(next(st), ']');
561-
return ty::mk_closure(st.tcx, did,
562-
st.tcx.mk_region(region), st.tcx.mk_substs(substs));
560+
return ty::mk_closure(st.tcx, did, st.tcx.mk_substs(substs));
563561
}
564562
'P' => {
565563
assert_eq!(next(st), '[');

src/librustc/metadata/tyencode.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,8 @@ pub fn enc_ty<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>, t: Ty<'t
139139
enc_substs(w, cx, substs);
140140
mywrite!(w, "]");
141141
}
142-
ty::ty_closure(def, region, substs) => {
142+
ty::ty_closure(def, substs) => {
143143
mywrite!(w, "k[{}|", (cx.ds)(def));
144-
enc_region(w, cx, *region);
145144
enc_substs(w, cx, substs);
146145
mywrite!(w, "]");
147146
}

src/librustc/middle/fast_reject.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub fn simplify_type(tcx: &ty::ctxt,
7474
let def_id = tcx.lang_items.owned_box().unwrap();
7575
Some(StructSimplifiedType(def_id))
7676
}
77-
ty::ty_closure(def_id, _, _) => {
77+
ty::ty_closure(def_id, _) => {
7878
Some(ClosureSimplifiedType(def_id))
7979
}
8080
ty::ty_tup(ref tys) => {

src/librustc/middle/infer/combine.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,15 +503,14 @@ pub fn super_tys<'tcx, C>(this: &C,
503503
Ok(ty::mk_struct(tcx, a_id, tcx.mk_substs(substs)))
504504
}
505505

506-
(&ty::ty_closure(a_id, a_region, a_substs),
507-
&ty::ty_closure(b_id, b_region, b_substs))
506+
(&ty::ty_closure(a_id, a_substs),
507+
&ty::ty_closure(b_id, b_substs))
508508
if a_id == b_id => {
509509
// All ty_closure types with the same id represent
510510
// the (anonymous) type of the same closure expression. So
511511
// all of their regions should be equated.
512-
let region = try!(this.equate().regions(*a_region, *b_region));
513512
let substs = try!(this.substs_variances(None, a_substs, b_substs));
514-
Ok(ty::mk_closure(tcx, a_id, tcx.mk_region(region), tcx.mk_substs(substs)))
513+
Ok(ty::mk_closure(tcx, a_id, tcx.mk_substs(substs)))
515514
}
516515

517516
(&ty::ty_uniq(a_inner), &ty::ty_uniq(b_inner)) => {

src/librustc/middle/liveness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
14961496
fn fn_ret(&self, id: NodeId) -> ty::PolyFnOutput<'tcx> {
14971497
let fn_ty = ty::node_id_to_type(self.ir.tcx, id);
14981498
match fn_ty.sty {
1499-
ty::ty_closure(closure_def_id, _, substs) =>
1499+
ty::ty_closure(closure_def_id, substs) =>
15001500
self.ir.tcx.closure_type(closure_def_id, substs).sig.output(),
15011501
_ =>
15021502
ty::ty_fn_ret(fn_ty),

src/librustc/middle/mem_categorization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
607607
def::DefUpvar(var_id, fn_node_id) => {
608608
let ty = try!(self.node_ty(fn_node_id));
609609
match ty.sty {
610-
ty::ty_closure(closure_id, _, _) => {
610+
ty::ty_closure(closure_id, _) => {
611611
match self.typer.closure_kind(closure_id) {
612612
Some(kind) => {
613613
self.cat_upvar(id, span, var_id, fn_node_id, kind)

src/librustc/middle/region.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,10 @@ impl InnermostEnclosingExpr {
320320

321321
#[derive(Debug, Copy)]
322322
pub struct Context {
323+
/// the scope that contains any new variables declared
323324
var_parent: InnermostDeclaringBlock,
324325

326+
/// region parent of expressions etc
325327
parent: InnermostEnclosingExpr,
326328
}
327329

src/librustc/middle/traits/project.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ fn consider_unification_despite_ambiguity<'cx,'tcx>(selcx: &mut SelectionContext
154154
debug!("consider_unification_despite_ambiguity: self_ty.sty={:?}",
155155
self_ty.sty);
156156
match self_ty.sty {
157-
ty::ty_closure(closure_def_id, _, substs) => {
157+
ty::ty_closure(closure_def_id, substs) => {
158158
let closure_typer = selcx.closure_typer();
159159
let closure_type = closure_typer.closure_type(closure_def_id, substs);
160160
let ty::Binder((_, ret_type)) =

src/librustc/middle/traits/select.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
293293
// lifetimes can appear inside the self-type.
294294
let self_ty = self.infcx.shallow_resolve(obligation.self_ty());
295295
let (closure_def_id, substs) = match self_ty.sty {
296-
ty::ty_closure(id, _, ref substs) => (id, substs.clone()),
296+
ty::ty_closure(id, ref substs) => (id, substs.clone()),
297297
_ => { return; }
298298
};
299299
assert!(!substs.has_escaping_regions());
@@ -1054,7 +1054,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
10541054

10551055
let self_ty = self.infcx.shallow_resolve(obligation.self_ty());
10561056
let (closure_def_id, substs) = match self_ty.sty {
1057-
ty::ty_closure(id, _, ref substs) => (id, substs.clone()),
1057+
ty::ty_closure(id, ref substs) => (id, substs.clone()),
10581058
ty::ty_infer(ty::TyVar(_)) => {
10591059
debug!("assemble_unboxed_closure_candidates: ambiguous self-type");
10601060
candidates.ambiguous = true;
@@ -1533,7 +1533,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
15331533
// (T1, ..., Tn) -- meets any bound that all of T1...Tn meet
15341534
ty::ty_tup(ref tys) => Ok(If(tys.clone())),
15351535

1536-
ty::ty_closure(def_id, _, substs) => {
1536+
ty::ty_closure(def_id, substs) => {
15371537
// FIXME -- This case is tricky. In the case of by-ref
15381538
// closures particularly, we need the results of
15391539
// inference to decide how to reflect the type of each
@@ -1687,7 +1687,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
16871687
Some(tys.clone())
16881688
}
16891689

1690-
ty::ty_closure(def_id, _, substs) => {
1690+
ty::ty_closure(def_id, substs) => {
16911691
assert_eq!(def_id.krate, ast::LOCAL_CRATE);
16921692

16931693
match self.closure_typer.closure_upvars(def_id, substs) {

src/librustc/middle/ty.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,7 @@ pub enum sty<'tcx> {
13671367
ty_trait(Box<TyTrait<'tcx>>),
13681368
ty_struct(DefId, &'tcx Substs<'tcx>),
13691369

1370-
ty_closure(DefId, &'tcx Region, &'tcx Substs<'tcx>),
1370+
ty_closure(DefId, &'tcx Substs<'tcx>),
13711371

13721372
ty_tup(Vec<Ty<'tcx>>),
13731373

@@ -2658,8 +2658,7 @@ impl FlagComputation {
26582658
}
26592659
}
26602660

2661-
&ty_closure(_, region, substs) => {
2662-
self.add_region(*region);
2661+
&ty_closure(_, substs) => {
26632662
self.add_substs(substs);
26642663
}
26652664

@@ -2927,10 +2926,9 @@ pub fn mk_struct<'tcx>(cx: &ctxt<'tcx>, struct_id: ast::DefId,
29272926
mk_t(cx, ty_struct(struct_id, substs))
29282927
}
29292928

2930-
pub fn mk_closure<'tcx>(cx: &ctxt<'tcx>, closure_id: ast::DefId,
2931-
region: &'tcx Region, substs: &'tcx Substs<'tcx>)
2929+
pub fn mk_closure<'tcx>(cx: &ctxt<'tcx>, closure_id: ast::DefId, substs: &'tcx Substs<'tcx>)
29322930
-> Ty<'tcx> {
2933-
mk_t(cx, ty_closure(closure_id, region, substs))
2931+
mk_t(cx, ty_closure(closure_id, substs))
29342932
}
29352933

29362934
pub fn mk_var<'tcx>(cx: &ctxt<'tcx>, v: TyVid) -> Ty<'tcx> {
@@ -3513,13 +3511,11 @@ pub fn type_contents<'tcx>(cx: &ctxt<'tcx>, ty: Ty<'tcx>) -> TypeContents {
35133511
apply_lang_items(cx, did, res)
35143512
}
35153513

3516-
ty_closure(did, r, substs) => {
3514+
ty_closure(did, substs) => {
35173515
// FIXME(#14449): `borrowed_contents` below assumes `&mut` closure.
35183516
let param_env = ty::empty_parameter_environment(cx);
35193517
let upvars = closure_upvars(&param_env, did, substs).unwrap();
3520-
TypeContents::union(&upvars,
3521-
|f| tc_ty(cx, &f.ty, cache))
3522-
| borrowed_contents(*r, MutMutable)
3518+
TypeContents::union(&upvars, |f| tc_ty(cx, &f.ty, cache))
35233519
}
35243520

35253521
ty_tup(ref tys) => {
@@ -5175,7 +5171,7 @@ pub fn ty_to_def_id(ty: Ty) -> Option<ast::DefId> {
51755171
Some(tt.principal_def_id()),
51765172
ty_struct(id, _) |
51775173
ty_enum(id, _) |
5178-
ty_closure(id, _, _) =>
5174+
ty_closure(id, _) =>
51795175
Some(id),
51805176
_ =>
51815177
None
@@ -6301,10 +6297,9 @@ pub fn hash_crate_independent<'tcx>(tcx: &ctxt<'tcx>, ty: Ty<'tcx>, svh: &Svh) -
63016297
}
63026298
ty_infer(_) => unreachable!(),
63036299
ty_err => byte!(21),
6304-
ty_closure(d, r, _) => {
6300+
ty_closure(d, _) => {
63056301
byte!(22);
63066302
did(state, d);
6307-
region(state, *r);
63086303
}
63096304
ty_projection(ref data) => {
63106305
byte!(23);
@@ -6618,8 +6613,7 @@ pub fn accumulate_lifetimes_in_type(accumulator: &mut Vec<ty::Region>,
66186613
ty_struct(_, substs) => {
66196614
accum_substs(accumulator, substs);
66206615
}
6621-
ty_closure(_, region, substs) => {
6622-
accumulator.push(*region);
6616+
ty_closure(_, substs) => {
66236617
accum_substs(accumulator, substs);
66246618
}
66256619
ty_bool |

0 commit comments

Comments
 (0)