Fix unsafe uses of mutable references

This commit is contained in:
Marijn Haverbeke 2011-06-10 16:39:09 +02:00
parent fccf065266
commit f28796ed99
8 changed files with 38 additions and 28 deletions

View file

@ -3889,7 +3889,7 @@ fn trans_for(&@block_ctxt cx,
fn collect_upvars(&@block_ctxt cx, &ast::block bloc,
&ast::def_id initial_decl) -> vec[ast::def_id] {
type env = @rec(
vec[ast::def_id] refs,
mutable vec[ast::def_id] refs,
hashmap[ast::def_id,()] decls,
resolve::def_map def_map
);
@ -3923,7 +3923,7 @@ fn collect_upvars(&@block_ctxt cx, &ast::block bloc,
let vec[ast::def_id] refs = [];
let hashmap[ast::def_id,()] decls = new_def_hash[()]();
decls.insert(initial_decl, ());
let env e = @rec(refs=refs,
let env e = @rec(mutable refs=refs,
decls=decls,
def_map=cx.fcx.lcx.ccx.tcx.def_map);
@ -3934,7 +3934,8 @@ fn collect_upvars(&@block_ctxt cx, &ast::block bloc,
// Calculate (refs - decls). This is the set of captured upvars.
let vec[ast::def_id] result = [];
for (ast::def_id ref_id in e.refs) {
for (ast::def_id ref_id_ in e.refs) {
auto ref_id = ref_id_;
if (!decls.contains_key(ref_id)) {
result += [ref_id];
}

View file

@ -57,14 +57,14 @@ import aux::crate_ctxt;
import aux::add_node;
import middle::tstate::ann::empty_ann;
fn collect_ids_expr(&@expr e, @vec[uint] res) -> () {
fn collect_ids_expr(&@expr e, @mutable vec[uint] res) -> () {
vec::push(*res, (expr_ann(e)).id);
}
fn collect_ids_block(&block b, @vec[uint] res) -> () {
fn collect_ids_block(&block b, @mutable vec[uint] res) -> () {
vec::push(*res, b.node.a.id);
}
fn collect_ids_stmt(&@stmt s, @vec[uint] res) -> () {
fn collect_ids_stmt(&@stmt s, @mutable vec[uint] res) -> () {
alt (s.node) {
case (stmt_decl(_,?a)) {
log("node_id " + uistr(a.id));
@ -82,7 +82,7 @@ fn collect_ids_stmt(&@stmt s, @vec[uint] res) -> () {
}
}
fn collect_ids_decl(&@decl d, @vec[uint] res) -> () {
fn collect_ids_decl(&@decl d, @mutable vec[uint] res) -> () {
alt (d.node) {
case (decl_local(?l)) {
vec::push(*res, l.ann.id);
@ -92,7 +92,7 @@ fn collect_ids_decl(&@decl d, @vec[uint] res) -> () {
}
fn node_ids_in_fn(&_fn f, &span sp, &ident i, &def_id d, &ann a,
@vec[uint] res) -> () {
@mutable vec[uint] res) -> () {
auto collect_ids = walk::default_visitor();
collect_ids = rec(visit_expr_pre = bind collect_ids_expr(_,res),
visit_block_pre = bind collect_ids_block(_,res),
@ -102,8 +102,8 @@ fn node_ids_in_fn(&_fn f, &span sp, &ident i, &def_id d, &ann a,
walk::walk_fn(collect_ids, f, sp, i, d, a);
}
fn init_vecs(&crate_ctxt ccx, @vec[uint] node_ids, uint len) -> () {
for (uint i in *node_ids) {
fn init_vecs(&crate_ctxt ccx, &vec[uint] node_ids, uint len) -> () {
for (uint i in node_ids) {
log(uistr(i) + " |-> " + uistr(len));
add_node(ccx, i, empty_ann(len));
}
@ -111,10 +111,10 @@ fn init_vecs(&crate_ctxt ccx, @vec[uint] node_ids, uint len) -> () {
fn visit_fn(&crate_ctxt ccx, uint num_constraints, &_fn f,
&span sp, &ident i, &def_id d, &ann a) -> () {
let vec[uint] node_ids_ = [];
let @vec[uint] node_ids = @node_ids_;
let @mutable vec[uint] node_ids = @mutable [];
node_ids_in_fn(f, sp, i, d, a, node_ids);
init_vecs(ccx, node_ids, num_constraints);
auto node_id_vec = *node_ids;
init_vecs(ccx, node_id_vec, num_constraints);
}
fn annotate_in_fn(&crate_ctxt ccx, &_fn f, &span sp, &ident i,

View file

@ -216,7 +216,7 @@ type pred_desc_ = rec(vec[@constr_arg] args,
type pred_desc = spanned[pred_desc_];
tag constraint {
cinit(uint, span, ident);
cpred(path, @vec[pred_desc]);
cpred(path, @mutable vec[pred_desc]);
}
tag constr_ {
ninit(ident);
@ -239,7 +239,7 @@ type constr_map = @std::map::hashmap[def_id, constraint];
type fn_info = rec(constr_map constrs, uint num_constraints, controlflow cf);
/* mapping from node ID to typestate annotation */
type node_ann_table = @vec[ts_ann];
type node_ann_table = @mutable vec[ts_ann];
/* mapping from function name to fn_info map */
type fn_info_map = @std::map::hashmap[def_id, fn_info];
@ -470,7 +470,7 @@ fn num_constraints(fn_info m) -> uint {
fn new_crate_ctxt(ty::ctxt cx) -> crate_ctxt {
let vec[ts_ann] na = [];
ret rec(tcx=cx, node_anns=@na, fm=@new_def_hash[fn_info]());
ret rec(tcx=cx, node_anns=@mutable na, fm=@new_def_hash[fn_info]());
}
fn controlflow_def_id(&crate_ctxt ccx, &def_id d) -> controlflow {
@ -551,10 +551,10 @@ fn constraints(&fn_ctxt fcx) -> vec[norm_constraint] {
// FIXME:
// this probably doesn't handle name shadowing well (or at all)
// variables should really always be id'd by def_id and not ident
fn match_args(&fn_ctxt fcx, @vec[pred_desc] occs,
fn match_args(&fn_ctxt fcx, vec[pred_desc] occs,
vec[@constr_arg] occ) -> uint {
log ("match_args: looking at " + constr_args_to_str(occ));
for (pred_desc pd in *occs) {
for (pred_desc pd in occs) {
log ("match_args: candidate " + pred_desc_to_str(pd));
if (ty::args_eq(pd.node.args, occ)) {
ret pd.node.bit_num;

View file

@ -53,7 +53,7 @@ fn bit_num(&fn_ctxt fcx, &def_id v, &constr_occ o) -> uint {
case (occ_args(?args)) {
alt (res) {
case (cpred(_, ?descs)) {
ret match_args(fcx, descs, args);
ret match_args(fcx, *descs, args);
}
case (_) {
fcx.ccx.tcx.sess.bug("bit_num: asked for pred constraint,"

View file

@ -25,7 +25,7 @@ import util::common::uistr;
import util::common::span;
import util::common::respan;
type ctxt = rec(@vec[constraint_info] cs,
type ctxt = rec(@mutable vec[constraint_info] cs,
ty::ctxt tcx);
fn collect_local(&ctxt cx, &@decl d) -> () {
@ -52,7 +52,7 @@ fn collect_pred(&ctxt cx, &@expr e) -> () {
fn find_locals(&ty::ctxt tcx, &_fn f, &span sp, &ident i, &def_id d, &ann a)
-> ctxt {
let ctxt cx = rec(cs=@vec::alloc[constraint_info](0u), tcx=tcx);
let ctxt cx = rec(cs=@mutable vec::alloc[constraint_info](0u), tcx=tcx);
auto visitor = walk::default_visitor();
visitor = rec(visit_decl_pre=bind collect_local(cx,_),
visit_expr_pre=bind collect_pred(cx,_)
@ -86,7 +86,8 @@ fn add_constraint(&ty::ctxt tcx, constraint_info c, uint next, constr_map tbl)
}
case (none[constraint]) {
tbl.insert(c.id, cpred(p,
@[respan(cn.span, rec(args=args, bit_num=next))]));
@mutable [respan(cn.span, rec(args=args,
bit_num=next))]));
}
}
}
@ -110,7 +111,7 @@ fn mk_fn_info(&crate_ctxt ccx, &_fn f, &span f_sp,
/* now we have to add bit nums for both the constraints
and the variables... */
for (constraint_info c in *cx.cs) {
for (constraint_info c in {*cx.cs}) {
next = add_constraint(cx.tcx, c, next, res_map);
}
/* add a pseudo-entry for the function's return value

View file

@ -5,8 +5,8 @@ native "rust" mod rustrt {
type timeval = rec(u32 sec, u32 usec);
fn get_time() -> timeval {
let timeval res = rec(sec=0u32, usec=0u32);
rustrt::get_time(res.sec, res.usec);
ret res;
auto sec = 0u32; auto usec = 0u32;
rustrt::get_time(sec, usec);
ret rec(sec=sec, usec=usec);
}

View file

@ -293,6 +293,14 @@ fn member[T](&T x, &array[T] v) -> bool {
ret false;
}
fn count[T](&T x, &array[T] v) -> uint {
auto cnt = 0u;
for (T elt in v) {
if (x == elt) { cnt += 1u; }
}
ret cnt;
}
fn foldl[T, U](fn (&U, &T) -> U p, &U z, &vec[T] v) -> U {
auto sz = len[T](v);

View file

@ -3,7 +3,7 @@ fn push[T](&mutable vec[mutable? T] v, &T t) {
}
fn main() {
auto v = @[1, 2, 3];
push[int](*v, 1);
auto v = [1, 2, 3];
push(v, 1);
}