Skip to content

Commit 537d879

Browse files
committed
Properly handle lifetime of aliases in nested blocks
There was a bug that would cause the alias analyser to allow you to invalidate an alias that was no longer directly referred to, even if another alias was rooted in it. It now properly tracks dependencies between live aliases. Required another case of copying values in map.rs.
1 parent beda82d commit 537d879

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

src/comp/middle/alias.rs

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type restrict = @rec(vec[def_num] root_vars,
2020
def_num block_defnum,
2121
vec[def_num] bindings,
2222
vec[ty::t] tys,
23+
vec[uint] depends_on,
2324
mutable valid ok);
2425

2526
type scope = vec[restrict];
@@ -182,6 +183,7 @@ fn check_alt(&ctx cx, &@ast::expr input, &vec[ast::arm] arms,
182183
block_defnum=dnums.(0),
183184
bindings=dnums,
184185
tys=forbidden_tp,
186+
depends_on=deps(sc, roots),
185187
mutable ok=valid));
186188
}
187189
visit::visit_arm(a, new_sc, v);
@@ -221,6 +223,7 @@ fn check_for_each(&ctx cx, &@ast::decl decl, &@ast::expr call,
221223
block_defnum=defnum,
222224
bindings=[defnum],
223225
tys=data.unsafe_ts,
226+
depends_on=deps(sc, data.root_vars),
224227
mutable ok=valid);
225228
visit::visit_block(block, sc + [new_sc], v);
226229
}
@@ -256,6 +259,7 @@ fn check_for(&ctx cx, &@ast::decl decl, &@ast::expr seq,
256259
block_defnum=defnum,
257260
bindings=[defnum],
258261
tys=unsafe,
262+
depends_on=deps(sc, root_def),
259263
mutable ok=valid);
260264
visit::visit_block(block, sc + [new_sc], v);
261265
}
@@ -274,24 +278,8 @@ fn check_var(&ctx cx, &@ast::expr ex, &ast::path p, ast::ann ann, bool assign,
274278
r.ok = val_taken(ex.span, p);
275279
}
276280
}
277-
} else if (r.ok != valid && vec::member(my_defnum, r.bindings)) {
278-
fail_alias(cx, r.ok, p);
279-
}
280-
}
281-
}
282-
283-
fn fail_alias(&ctx cx, valid issue, &ast::path pt) {
284-
auto base = " will invalidate alias " + ast::path_name(pt) +
285-
", which is still used";
286-
alt (issue) {
287-
case (overwritten(?sp, ?wpt)) {
288-
cx.tcx.sess.span_err
289-
(sp, "overwriting " + ast::path_name(wpt) + base);
290-
}
291-
case (val_taken(?sp, ?vpt)) {
292-
cx.tcx.sess.span_err
293-
(sp, "taking the value of " + ast::path_name(vpt) +
294-
base);
281+
} else if (vec::member(my_defnum, r.bindings)) {
282+
test_scope(cx, sc, r, p);
295283
}
296284
}
297285
}
@@ -316,6 +304,41 @@ fn check_assign(&@ctx cx, &@ast::expr dest, &@ast::expr src,
316304
}
317305
}
318306

307+
fn test_scope(&ctx cx, &scope sc, &restrict r, &ast::path p) {
308+
auto prob = r.ok;
309+
for (uint dep in r.depends_on) {
310+
if (prob != valid) { break; }
311+
prob = sc.(dep).ok;
312+
}
313+
if (prob != valid) {
314+
auto msg = alt (prob) {
315+
case (overwritten(?sp, ?wpt)) {
316+
tup(sp, "overwriting " + ast::path_name(wpt))
317+
}
318+
case (val_taken(?sp, ?vpt)) {
319+
tup(sp, "taking the value of " + ast::path_name(vpt))
320+
}
321+
};
322+
cx.tcx.sess.span_err
323+
(msg._0, msg._1 + " will invalidate alias " +
324+
ast::path_name(p) + ", which is still used");
325+
}
326+
}
327+
328+
fn deps(&scope sc, vec[def_num] roots) -> vec[uint] {
329+
auto i = 0u;
330+
auto result = [];
331+
for (restrict r in sc) {
332+
for (def_num dn in roots) {
333+
if (vec::member(dn, r.bindings)) {
334+
vec::push(result, i);
335+
}
336+
}
337+
i += 1u;
338+
}
339+
ret result;
340+
}
341+
319342
fn expr_root(&ctx cx, @ast::expr ex, bool autoderef)
320343
-> rec(@ast::expr ex, option::t[ty::t] inner_mut, bool mut_in_box) {
321344
let option::t[ty::t] mut = none;

src/lib/map.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
131131
{
132132
for (bucket[K, V] b in oldbkts) {
133133
alt (b) {
134-
case (some(?k, ?v)) {
134+
case (some(?k_, ?v_)) {
135+
auto k = k_; auto v = v_;
135136
insert_common[K, V](hasher, eqer, newbkts,
136137
nnewbkts, k, v);
137138
}

0 commit comments

Comments
 (0)