Skip to content

Commit f94c3f3

Browse files
committed
---
yaml --- r: 155247 b: refs/heads/try2 c: 6536a0c h: refs/heads/master i: 155245: abd9ebf 155243: 16c2fff 155239: 603825f 155231: 3b60212 v: v3
1 parent f3ca189 commit f94c3f3

File tree

11 files changed

+90
-91
lines changed

11 files changed

+90
-91
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 6543c5b9a42144eb7036dccbef6e447a41f95485
8+
refs/heads/try2: 6536a0c0d6285c00675a6b9342c3630c309aba36
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/librustc/middle/astencode.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,10 @@ impl tr for def::Def {
472472
def::DefPrimTy(p) => def::DefPrimTy(p),
473473
def::DefTyParam(s, did, v) => def::DefTyParam(s, did.tr(dcx), v),
474474
def::DefUse(did) => def::DefUse(did.tr(dcx)),
475-
def::DefUpvar(nid1, def, nid2, nid3) => {
475+
def::DefUpvar(nid1, def, depth, nid2, nid3) => {
476476
def::DefUpvar(dcx.tr_id(nid1),
477477
box(GC) (*def).tr(dcx),
478+
depth,
478479
dcx.tr_id(nid2),
479480
dcx.tr_id(nid3))
480481
}

branches/try2/src/librustc/middle/def.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ pub enum Def {
3030
DefPrimTy(ast::PrimTy),
3131
DefTyParam(ParamSpace, ast::DefId, uint),
3232
DefUse(ast::DefId),
33-
DefUpvar(ast::NodeId, // id of closed over var
34-
Gc<Def>, // closed over def
33+
DefUpvar(ast::NodeId, // id of closed over local
34+
Gc<Def>, // closed over def
35+
u32, // number of closures implicitely capturing this local
3536
ast::NodeId, // expr node that creates the closure
36-
ast::NodeId), // id for the block/body of the closure expr
37+
ast::NodeId), // block node for the closest enclosing proc
38+
// or unboxed closure, DUMMY_NODE_ID otherwise
3739

3840
/// Note that if it's a tuple struct's definition, the node id of the ast::DefId
3941
/// may either refer to the item definition's id or the StructDef.ctor_id.
@@ -68,7 +70,7 @@ impl Def {
6870
}
6971
DefLocal(id) |
7072
DefSelfTy(id) |
71-
DefUpvar(id, _, _, _) |
73+
DefUpvar(id, _, _, _, _) |
7274
DefRegion(id) |
7375
DefTyParamBinder(id) |
7476
DefLabel(id) => {

branches/try2/src/librustc/middle/freevars.rs

Lines changed: 42 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use syntax::visit;
2626

2727
#[deriving(Clone, Decodable, Encodable, Show)]
2828
pub enum CaptureMode {
29-
/// Copy/move the value from this llvm ValueRef into the environment.
29+
/// Copy/move the value into the environment.
3030
CaptureByValue,
3131

3232
/// Access by reference (used for stack closures).
@@ -45,23 +45,22 @@ pub type freevar_map = NodeMap<Vec<freevar_entry>>;
4545

4646
pub type CaptureModeMap = NodeMap<CaptureMode>;
4747

48-
struct CollectFreevarsVisitor<'a> {
48+
struct CollectFreevarsVisitor<'a, 'b:'a> {
49+
node_id: ast::NodeId,
4950
seen: NodeSet,
50-
refs: Vec<freevar_entry>,
51-
def_map: &'a resolve::DefMap,
52-
capture_mode_map: &'a mut CaptureModeMap,
53-
depth: uint
51+
cx: &'a mut AnnotateFreevarsVisitor<'b>,
52+
depth: u32
5453
}
5554

56-
impl<'a, 'v> Visitor<'v> for CollectFreevarsVisitor<'a> {
55+
impl<'a, 'b, 'v> Visitor<'v> for CollectFreevarsVisitor<'a, 'b> {
5756
fn visit_item(&mut self, _: &ast::Item) {
5857
// ignore_item
5958
}
6059

6160
fn visit_expr(&mut self, expr: &ast::Expr) {
6261
match expr.node {
6362
ast::ExprProc(..) => {
64-
self.capture_mode_map.insert(expr.id, CaptureByValue);
63+
self.cx.capture_mode_map.insert(expr.id, CaptureByValue);
6564
self.depth += 1;
6665
visit::walk_expr(self, expr);
6766
self.depth -= 1;
@@ -74,7 +73,7 @@ impl<'a, 'v> Visitor<'v> for CollectFreevarsVisitor<'a> {
7473
// ast::CaptureByRef => CaptureByRef,
7574
//};
7675
let capture_mode = CaptureByRef;
77-
self.capture_mode_map.insert(expr.id, capture_mode);
76+
self.cx.capture_mode_map.insert(expr.id, capture_mode);
7877
self.depth += 1;
7978
visit::walk_expr(self, expr);
8079
self.depth -= 1;
@@ -84,60 +83,45 @@ impl<'a, 'v> Visitor<'v> for CollectFreevarsVisitor<'a> {
8483
ast::CaptureByValue => CaptureByValue,
8584
ast::CaptureByRef => CaptureByRef,
8685
};
87-
self.capture_mode_map.insert(expr.id, capture_mode);
86+
self.cx.capture_mode_map.insert(expr.id, capture_mode);
8887
self.depth += 1;
8988
visit::walk_expr(self, expr);
9089
self.depth -= 1;
9190
}
9291
ast::ExprPath(..) => {
93-
let mut def = *self.def_map.borrow().find(&expr.id)
94-
.expect("path not found");
95-
let mut i = 0;
96-
while i < self.depth {
97-
match def {
98-
def::DefUpvar(_, inner, _, _) => { def = *inner; }
99-
_ => break
100-
}
101-
i += 1;
102-
}
103-
if i == self.depth { // Made it to end of loop
104-
let dnum = def.def_id().node;
105-
if !self.seen.contains(&dnum) {
106-
self.refs.push(freevar_entry {
107-
def: def,
108-
span: expr.span,
109-
});
110-
self.seen.insert(dnum);
111-
}
92+
let def = *self.cx.def_map.borrow().find(&expr.id)
93+
.expect("path not found");
94+
let dnum = def.def_id().node;
95+
if self.seen.contains(&dnum) {
96+
return;
11297
}
98+
let def = match def {
99+
def::DefUpvar(_, _, depth, _, _) => {
100+
if depth < self.depth {
101+
return;
102+
}
103+
let mut def = def;
104+
for _ in range(0, depth - self.depth) {
105+
match def {
106+
def::DefUpvar(_, inner, _, _, _) => { def = *inner; }
107+
_ => unreachable!()
108+
}
109+
}
110+
def
111+
},
112+
_ => return
113+
};
114+
self.cx.freevars.find_or_insert(self.node_id, vec![]).push(freevar_entry {
115+
def: def,
116+
span: expr.span,
117+
});
118+
self.seen.insert(dnum);
113119
}
114120
_ => visit::walk_expr(self, expr)
115121
}
116122
}
117123
}
118124

119-
// Searches through part of the AST for all references to locals or
120-
// upvars in this frame and returns the list of definition IDs thus found.
121-
// Since we want to be able to collect upvars in some arbitrary piece
122-
// of the AST, we take a walker function that we invoke with a visitor
123-
// in order to start the search.
124-
fn collect_freevars(def_map: &resolve::DefMap,
125-
blk: &ast::Block,
126-
capture_mode_map: &mut CaptureModeMap)
127-
-> Vec<freevar_entry> {
128-
let mut v = CollectFreevarsVisitor {
129-
seen: NodeSet::new(),
130-
refs: Vec::new(),
131-
def_map: def_map,
132-
capture_mode_map: &mut *capture_mode_map,
133-
depth: 1
134-
};
135-
136-
v.visit_block(blk);
137-
138-
v.refs
139-
}
140-
141125
struct AnnotateFreevarsVisitor<'a> {
142126
def_map: &'a resolve::DefMap,
143127
freevars: freevar_map,
@@ -147,10 +131,12 @@ struct AnnotateFreevarsVisitor<'a> {
147131
impl<'a, 'v> Visitor<'v> for AnnotateFreevarsVisitor<'a> {
148132
fn visit_fn(&mut self, fk: visit::FnKind<'v>, fd: &'v ast::FnDecl,
149133
blk: &'v ast::Block, s: Span, nid: ast::NodeId) {
150-
let vars = collect_freevars(self.def_map,
151-
blk,
152-
&mut self.capture_mode_map);
153-
self.freevars.insert(nid, vars);
134+
CollectFreevarsVisitor {
135+
node_id: nid,
136+
seen: NodeSet::new(),
137+
cx: self,
138+
depth: 0
139+
}.visit_block(blk);
154140
visit::walk_fn(self, fk, fd, blk, s);
155141
}
156142
}
@@ -168,13 +154,7 @@ pub fn annotate_freevars(def_map: &resolve::DefMap, krate: &ast::Crate)
168154
capture_mode_map: NodeMap::new(),
169155
};
170156
visit::walk_crate(&mut visitor, krate);
171-
172-
let AnnotateFreevarsVisitor {
173-
freevars,
174-
capture_mode_map,
175-
..
176-
} = visitor;
177-
(freevars, capture_mode_map)
157+
(visitor.freevars, visitor.capture_mode_map)
178158
}
179159

180160
pub fn with_freevars<T>(tcx: &ty::ctxt, fid: ast::NodeId, f: |&[freevar_entry]| -> T) -> T {

branches/try2/src/librustc/middle/mem_categorization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
547547
}))
548548
}
549549

550-
def::DefUpvar(var_id, _, fn_node_id, _) => {
550+
def::DefUpvar(var_id, _, _, fn_node_id, _) => {
551551
let ty = if_ok!(self.node_ty(fn_node_id));
552552
match ty::get(ty).sty {
553553
ty::ty_closure(ref closure_ty) => {

branches/try2/src/librustc/middle/resolve.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,13 @@ enum RibKind {
277277
// No translation needs to be applied.
278278
NormalRibKind,
279279

280-
// We passed through a function scope at the given node ID. Translate
281-
// upvars as appropriate.
282-
FunctionRibKind(NodeId /* func id */, NodeId /* body id */),
280+
// We passed through a closure scope at the given node ID.
281+
// Translate upvars as appropriate.
282+
ClosureRibKind(NodeId /* func id */),
283+
284+
// We passed through a proc or unboxed closure scope at the given node ID.
285+
// Translate upvars as appropriate.
286+
ProcRibKind(NodeId /* func id */, NodeId /* body id */),
283287

284288
// We passed through an impl or trait and are now in one of its
285289
// methods. Allow references to ty params that impl or trait
@@ -3859,12 +3863,22 @@ impl<'a> Resolver<'a> {
38593863
NormalRibKind => {
38603864
// Nothing to do. Continue.
38613865
}
3862-
FunctionRibKind(function_id, body_id) => {
3866+
ClosureRibKind(function_id) => {
38633867
if !is_ty_param {
3864-
def = DefUpvar(def.def_id().node,
3865-
box(GC) def,
3866-
function_id,
3867-
body_id);
3868+
let (depth, block_id) = match def {
3869+
DefUpvar(_, _, depth, _, block_id) => (depth + 1, block_id),
3870+
_ => (0, ast::DUMMY_NODE_ID)
3871+
};
3872+
def = DefUpvar(def.def_id().node, box(GC) def, depth, function_id, block_id);
3873+
}
3874+
}
3875+
ProcRibKind(function_id, block_id) => {
3876+
if !is_ty_param {
3877+
let depth = match def {
3878+
DefUpvar(_, _, depth, _, _) => depth + 1,
3879+
_ => 0
3880+
};
3881+
def = DefUpvar(def.def_id().node, box(GC) def, depth, function_id, block_id);
38683882
}
38693883
}
38703884
MethodRibKind(item_id, _) => {
@@ -5758,10 +5772,14 @@ impl<'a> Resolver<'a> {
57585772
visit::walk_expr(self, expr);
57595773
}
57605774

5761-
ExprFnBlock(_, ref fn_decl, ref block) |
5775+
ExprFnBlock(_, ref fn_decl, ref block) => {
5776+
self.resolve_function(ClosureRibKind(expr.id),
5777+
Some(&**fn_decl), NoTypeParameters,
5778+
&**block);
5779+
}
57625780
ExprProc(ref fn_decl, ref block) |
57635781
ExprUnboxedFn(_, _, ref fn_decl, ref block) => {
5764-
self.resolve_function(FunctionRibKind(expr.id, block.id),
5782+
self.resolve_function(ProcRibKind(expr.id, block.id),
57655783
Some(&**fn_decl), NoTypeParameters,
57665784
&**block);
57675785
}

branches/try2/src/librustc/middle/save/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
232232
def::DefStatic(_, _) |
233233
def::DefLocal(_) |
234234
def::DefVariant(_, _, _) |
235-
def::DefUpvar(_, _, _, _) => Some(recorder::VarRef),
235+
def::DefUpvar(..) => Some(recorder::VarRef),
236236

237237
def::DefFn(_, _) => Some(recorder::FnRef),
238238

branches/try2/src/librustc/middle/trans/_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@ pub fn trans_match<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
12261226
fn is_discr_reassigned(bcx: Block, discr: &ast::Expr, body: &ast::Expr) -> bool {
12271227
match discr.node {
12281228
ast::ExprPath(..) => match bcx.def(discr.id) {
1229-
def::DefLocal(vid) | def::DefUpvar(vid, _, _, _) => {
1229+
def::DefLocal(vid) | def::DefUpvar(vid, _, _, _, _) => {
12301230
let mut rc = ReassignmentChecker {
12311231
node: vid,
12321232
reassigned: false

branches/try2/src/librustc/middle/trans/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,7 @@ pub fn trans_local_var<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
11761176
let _icx = push_ctxt("trans_local_var");
11771177

11781178
match def {
1179-
def::DefUpvar(nid, _, _, _) => {
1179+
def::DefUpvar(nid, _, _, _, _) => {
11801180
// Can't move upvars, so this is never a ZeroMemLastUse.
11811181
let local_ty = node_id_type(bcx, nid);
11821182
match bcx.fcx.llupvars.borrow().find(&nid) {

branches/try2/src/librustc/middle/typeck/check/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5027,7 +5027,7 @@ pub fn polytype_for_def(fcx: &FnCtxt,
50275027
defn: def::Def)
50285028
-> Polytype {
50295029
match defn {
5030-
def::DefLocal(nid) => {
5030+
def::DefLocal(nid) | def::DefUpvar(nid, _, _, _, _) => {
50315031
let typ = fcx.local_ty(sp, nid);
50325032
return no_params(typ);
50335033
}
@@ -5036,9 +5036,6 @@ pub fn polytype_for_def(fcx: &FnCtxt,
50365036
def::DefStruct(id) => {
50375037
return ty::lookup_item_type(fcx.ccx.tcx, id);
50385038
}
5039-
def::DefUpvar(_, inner, _, _) => {
5040-
return polytype_for_def(fcx, sp, *inner);
5041-
}
50425039
def::DefTrait(_) |
50435040
def::DefTy(..) |
50445041
def::DefAssociatedTy(..) |

branches/try2/src/librustc/middle/typeck/check/regionck.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,11 @@ fn region_of_def(fcx: &FnCtxt, def: def::Def) -> ty::Region {
245245
def::DefLocal(node_id) => {
246246
tcx.region_maps.var_region(node_id)
247247
}
248-
def::DefUpvar(_, subdef, closure_id, body_id) => {
249-
match ty::ty_closure_store(fcx.node_ty(closure_id)) {
250-
ty::RegionTraitStore(..) => region_of_def(fcx, *subdef),
251-
ty::UniqTraitStore => ReScope(body_id)
248+
def::DefUpvar(node_id, _, _, _, body_id) => {
249+
if body_id == ast::DUMMY_NODE_ID {
250+
tcx.region_maps.var_region(node_id)
251+
} else {
252+
ReScope(body_id)
252253
}
253254
}
254255
_ => {
@@ -1029,7 +1030,7 @@ fn check_expr_fn_block(rcx: &mut Rcx,
10291030
// determining the final borrow_kind) and propagate that as
10301031
// a constraint on the outer closure.
10311032
match freevar.def {
1032-
def::DefUpvar(var_id, _, outer_closure_id, _) => {
1033+
def::DefUpvar(var_id, _, _, outer_closure_id, _) => {
10331034
// thing being captured is itself an upvar:
10341035
let outer_upvar_id = ty::UpvarId {
10351036
var_id: var_id,

0 commit comments

Comments
 (0)