Skip to content

Commit 38e86d7

Browse files
committed
Properly alias-check bindings in for-each loops
1 parent 45bdff0 commit 38e86d7

File tree

1 file changed

+32
-40
lines changed

1 file changed

+32
-40
lines changed

src/comp/middle/alias.rs

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ import std::option::{some, none, is_none};
1515
tag valid { valid; overwritten(span, ast::path); val_taken(span, ast::path); }
1616
tag copied { not_allowed; copied; not_copied; }
1717

18-
type restrict = @{root_var: option::t<node_id>,
19-
node_id: node_id,
18+
type restrict = @{node_id: node_id,
2019
span: span,
2120
local_id: uint,
22-
binding: option::t<node_id>,
21+
root_var: option::t<node_id>,
2322
unsafe_tys: [ty::t],
24-
depends_on: [uint],
2523
mutable ok: valid,
2624
mutable copied: copied};
2725

@@ -71,7 +69,7 @@ fn visit_expr(cx: @ctx, ex: @ast::expr, sc: scope, v: vt<scope>) {
7169
let handled = true;
7270
alt ex.node {
7371
ast::expr_call(f, args) {
74-
check_call(*cx, f, args, sc);
72+
check_call(*cx, f, args);
7573
handled = false;
7674
}
7775
ast::expr_alt(input, arms) { check_alt(*cx, input, arms, sc, v); }
@@ -160,8 +158,7 @@ fn cant_copy(cx: ctx, r: restrict) -> bool {
160158
} else { ret true; }
161159
}
162160

163-
fn check_call(cx: ctx, f: @ast::expr, args: [@ast::expr], sc: scope) ->
164-
[restrict] {
161+
fn check_call(cx: ctx, f: @ast::expr, args: [@ast::expr]) -> [restrict] {
165162
let fty = ty::type_autoderef(cx.tcx, ty::expr_ty(cx.tcx, f));
166163
let arg_ts = ty::ty_fn_args(cx.tcx, fty);
167164
let mut_roots: [{arg: uint, node: node_id}] = [];
@@ -180,13 +177,11 @@ fn check_call(cx: ctx, f: @ast::expr, args: [@ast::expr], sc: scope) ->
180177
}
181178
}
182179
let root_var = path_def_id(cx, root.ex);
183-
restricts += [@{root_var: root_var,
184-
node_id: arg.id,
180+
restricts += [@{node_id: arg.id,
185181
span: arg.span,
186182
local_id: cx.next_local,
187-
binding: none::<node_id>,
183+
root_var: root_var,
188184
unsafe_tys: inner_mut(root.ds),
189-
depends_on: deps(sc, root_var),
190185
mutable ok: valid,
191186
mutable copied: alt arg_t.mode {
192187
ast::by_move. { copied }
@@ -284,13 +279,11 @@ fn check_alt(cx: ctx, input: @ast::expr, arms: [ast::arm], sc: scope,
284279
}
285280
}
286281
for info in binding_info {
287-
new_sc += [@{root_var: root_var,
288-
node_id: info.id,
282+
new_sc += [@{node_id: info.id,
289283
span: info.span,
290284
local_id: cx.next_local,
291-
binding: some(info.id),
285+
root_var: root_var,
292286
unsafe_tys: info.unsafe,
293-
depends_on: deps(sc, root_var),
294287
mutable ok: valid,
295288
mutable copied: not_copied}];
296289
}
@@ -304,9 +297,18 @@ fn check_for_each(cx: ctx, local: @ast::local, call: @ast::expr,
304297
v.visit_expr(call, sc, v);
305298
alt call.node {
306299
ast::expr_call(f, args) {
307-
let restricts = check_call(cx, f, args, sc);
300+
let new_sc = *sc + check_call(cx, f, args);
301+
for proot in *pattern_roots(cx.tcx, [], local.node.pat) {
302+
new_sc += [@{node_id: proot.id,
303+
span: proot.span,
304+
local_id: cx.next_local,
305+
root_var: none::<node_id>,
306+
unsafe_tys: inner_mut(proot.ds),
307+
mutable ok: valid,
308+
mutable copied: not_copied}];
309+
}
308310
register_locals(cx, local.node.pat);
309-
visit::visit_block(blk, @(*sc + restricts), v);
311+
visit::visit_block(blk, @new_sc, v);
310312
}
311313
}
312314
}
@@ -330,13 +332,11 @@ fn check_for(cx: ctx, local: @ast::local, seq: @ast::expr, blk: ast::blk,
330332
let root_var = path_def_id(cx, root.ex);
331333
let new_sc = *sc;
332334
for proot in *pattern_roots(cx.tcx, ext_ds, local.node.pat) {
333-
new_sc += [@{root_var: root_var,
334-
node_id: proot.id,
335+
new_sc += [@{node_id: proot.id,
335336
span: proot.span,
336337
local_id: cx.next_local,
337-
binding: some(proot.id),
338+
root_var: root_var,
338339
unsafe_tys: inner_mut(proot.ds),
339-
depends_on: deps(sc, root_var),
340340
mutable ok: valid,
341341
mutable copied: not_copied}];
342342
}
@@ -360,7 +360,7 @@ fn check_var(cx: ctx, ex: @ast::expr, p: ast::path, id: ast::node_id,
360360
r.ok = val_taken(ex.span, p);
361361
}
362362
}
363-
} else if r.binding == some(my_defnum) {
363+
} else if r.node_id == my_defnum {
364364
test_scope(cx, sc, r, p);
365365
}
366366
}
@@ -387,9 +387,16 @@ fn check_assign(cx: @ctx, dest: @ast::expr, src: @ast::expr, sc: scope,
387387

388388
fn test_scope(cx: ctx, sc: scope, r: restrict, p: ast::path) {
389389
let prob = r.ok;
390-
for dep: uint in r.depends_on {
391-
if prob != valid { break; }
392-
prob = sc[dep].ok;
390+
alt r.root_var {
391+
some(dn) {
392+
for other in *sc {
393+
if other.node_id == dn {
394+
prob = other.ok;
395+
if prob != valid { break; }
396+
}
397+
}
398+
}
399+
_ {}
393400
}
394401
if prob != valid && cant_copy(cx, r) {
395402
let msg =
@@ -409,21 +416,6 @@ fn test_scope(cx: ctx, sc: scope, r: restrict, p: ast::path) {
409416
}
410417
}
411418

412-
fn deps(sc: scope, root: option::t<node_id>) -> [uint] {
413-
let result = [];
414-
alt root {
415-
some(dn) {
416-
let i = 0u;
417-
for r: restrict in *sc {
418-
if r.binding == some(dn) { result += [i]; }
419-
i += 1u;
420-
}
421-
}
422-
_ { }
423-
}
424-
ret result;
425-
}
426-
427419
fn path_def(cx: ctx, ex: @ast::expr) -> option::t<ast::def> {
428420
ret alt ex.node {
429421
ast::expr_path(_) { some(cx.tcx.def_map.get(ex.id)) }

0 commit comments

Comments
 (0)