From 5fb518abc9539ef114cfe9a08fa1964d28c98ef3 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Wed, 15 Jun 2011 12:17:51 +0200 Subject: [PATCH] Fix assignments to immutable fields throughout the code --- src/comp/middle/metadata.rs | 10 +++++----- src/comp/middle/resolve.rs | 11 ++++++----- src/comp/middle/trans.rs | 15 +++++++-------- src/comp/middle/tstate/auxiliary.rs | 4 ++-- src/comp/middle/tstate/ck.rs | 10 +++++----- src/comp/pretty/pp.rs | 14 +++++++------- src/lib/deque.rs | 19 ++++++++++--------- src/lib/vec.rs | 4 ++-- src/test/bench/shootout/fannkuchredux.rs | 6 +++--- src/test/run-pass/vec-reverse.rs | 16 ++++++++-------- 10 files changed, 55 insertions(+), 54 deletions(-) diff --git a/src/comp/middle/metadata.rs b/src/comp/middle/metadata.rs index 07293e0a306..ed385dcad75 100644 --- a/src/comp/middle/metadata.rs +++ b/src/comp/middle/metadata.rs @@ -672,10 +672,9 @@ fn hash_path(&str s) -> uint { fn create_index[T](&vec[tup(T, uint)] index, fn(&T) -> uint hash_fn) -> vec[vec[tup(T, uint)]] { - let vec[vec[tup(T, uint)]] buckets = []; + let vec[mutable vec[tup(T, uint)]] buckets = vec::empty_mut(); for each (uint i in uint::range(0u, 256u)) { - let vec[tup(T, uint)] bucket = []; - buckets += [bucket]; + buckets += [mutable []]; } for (tup(T, uint) elt in index) { @@ -683,10 +682,11 @@ fn create_index[T](&vec[tup(T, uint)] index, fn(&T) -> uint hash_fn) buckets.(h % 256u) += [elt]; } - ret buckets; + ret vec::freeze(buckets); } -fn encode_index[T](&ebml::writer ebml_w, &vec[vec[tup(T, uint)]] buckets, +fn encode_index[T](&ebml::writer ebml_w, + &vec[vec[tup(T, uint)]] buckets, fn(&io::writer, &T) write_fn) { auto writer = io::new_writer_(ebml_w.writer); diff --git a/src/comp/middle/resolve.rs b/src/comp/middle/resolve.rs index 01570dc7789..41feac5251e 100644 --- a/src/comp/middle/resolve.rs +++ b/src/comp/middle/resolve.rs @@ -80,7 +80,8 @@ tag mod_index_entry { type mod_index = hashmap[ident,list[mod_index_entry]]; type indexed_mod = rec(option::t[ast::_mod] m, - mod_index index, vec[def] glob_imports, + mod_index index, + mutable vec[def] glob_imports, hashmap[str,import_state] glob_imported_names); /* native modules can't contain tags, and we don't store their ASTs because we only need to look at them to determine exports, which they can't control.*/ @@ -141,7 +142,7 @@ fn map_crate(&@env e, &@ast::crate c) { // Register the top-level mod e.mod_map.insert(-1, @rec(m=some(c.node.module), index=index_mod(c.node.module), - glob_imports=vec::empty[def](), + mutable glob_imports=vec::empty[def](), glob_imported_names =new_str_hash[import_state]())); @@ -160,7 +161,7 @@ fn map_crate(&@env e, &@ast::crate c) { case (ast::item_mod(_, ?md, ?defid)) { e.mod_map.insert(defid._1, @rec(m=some(md), index=index_mod(md), - glob_imports=vec::empty[def](), + mutable glob_imports=vec::empty[def](), glob_imported_names =new_str_hash[import_state]())); e.ast_map.insert(defid, i); @@ -169,7 +170,7 @@ fn map_crate(&@env e, &@ast::crate c) { e.mod_map.insert(defid._1, @rec(m=none[ast::_mod], index=index_nmod(nmd), - glob_imports=vec::empty[def](), + mutable glob_imports=vec::empty[def](), glob_imported_names =new_str_hash[import_state]())); e.ast_map.insert(defid, i); @@ -850,7 +851,7 @@ fn lookup_glob_in_mod(&env e, @indexed_mod info, &span sp, auto matches = vec::filter_map[def, def] (bind l_i_m_r(e, _, sp, id, ns, dr), - info.glob_imports); + {info.glob_imports}); if (vec::len(matches) == 0u) { ret none[def]; } else if (vec::len(matches) == 1u){ diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index ab2bf5a46bd..03e9d83c53e 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -5995,12 +5995,12 @@ fn trans_rec(&@block_ctxt cx, &vec[ast::field] fields, } fn trans_expr(&@block_ctxt cx, &@ast::expr e) -> result { - be trans_expr_out(cx, e, return); + ret trans_expr_out(cx, e, return); } fn trans_expr_out(&@block_ctxt cx, &@ast::expr e, out_method output) -> result { - *cx = rec(sp=e.span with *cx); + // FIXME Fill in cx.sp alt (e.node) { case (ast::expr_lit(?lit, ?ann)) { ret res(cx, trans_lit(cx.fcx.lcx.ccx, *lit, ann)); @@ -6059,7 +6059,6 @@ fn trans_expr_out(&@block_ctxt cx, &@ast::expr e, out_method output) } case (ast::expr_block(?blk, ?ann)) { - *cx = rec(sp=blk.span with *cx); auto sub_cx = new_scope_block_ctxt(cx, "block-expr body"); auto next_cx = new_sub_block_ctxt(cx, "next"); auto sub = with_out_method(bind trans_block(sub_cx, blk, _), @@ -6072,7 +6071,7 @@ fn trans_expr_out(&@block_ctxt cx, &@ast::expr e, out_method output) case (ast::expr_move(?dst, ?src, _)) { auto lhs_res = trans_lval(cx, dst); assert (lhs_res.is_mem); - *(lhs_res.res.bcx) = rec(sp=src.span with *(lhs_res.res.bcx)); + // FIXME Fill in lhs_res.res.bcx.sp auto rhs_res = trans_lval(lhs_res.res.bcx, src); auto t = ty::expr_ty(cx.fcx.lcx.ccx.tcx, src); // FIXME: calculate copy init-ness in typestate. @@ -6084,7 +6083,7 @@ fn trans_expr_out(&@block_ctxt cx, &@ast::expr e, out_method output) case (ast::expr_assign(?dst, ?src, _)) { auto lhs_res = trans_lval(cx, dst); assert (lhs_res.is_mem); - *(lhs_res.res.bcx) = rec(sp=src.span with *(lhs_res.res.bcx)); + // FIXME Fill in lhs_res.res.bcx.sp auto rhs_res = trans_expr(lhs_res.res.bcx, src); auto t = ty::expr_ty(cx.fcx.lcx.ccx.tcx, src); // FIXME: calculate copy init-ness in typestate. @@ -6097,7 +6096,7 @@ fn trans_expr_out(&@block_ctxt cx, &@ast::expr e, out_method output) auto t = ty::expr_ty(cx.fcx.lcx.ccx.tcx, src); auto lhs_res = trans_lval(cx, dst); assert (lhs_res.is_mem); - *(lhs_res.res.bcx) = rec(sp=src.span with *(lhs_res.res.bcx)); + // FIXME Fill in lhs_res.res.bcx.sp auto rhs_res = trans_expr(lhs_res.res.bcx, src); if (ty::type_is_sequence(cx.fcx.lcx.ccx.tcx, t)) { alt (op) { @@ -7182,7 +7181,7 @@ fn zero_alloca(&@block_ctxt cx, ValueRef llptr, ty::t t) -> result { } fn trans_stmt(&@block_ctxt cx, &ast::stmt s) -> result { - *cx = rec(sp=s.span with *cx); + // FIXME Fill in cx.sp auto bcx = cx; alt (s.node) { case (ast::stmt_expr(?e,_)) { @@ -7352,7 +7351,7 @@ fn alloc_local(&@block_ctxt cx, &@ast::local_ local) -> result { fn trans_block(&@block_ctxt cx, &ast::block b, &out_method output) -> result { auto bcx = cx; for each (@ast::local_ local in block_locals(b)) { - *bcx = rec(sp=local_rhs_span(local, cx.sp) with *bcx); + // FIXME Update bcx.sp bcx = alloc_local(bcx, local).bcx; } auto r = res(bcx, C_nil()); diff --git a/src/comp/middle/tstate/auxiliary.rs b/src/comp/middle/tstate/auxiliary.rs index 9b5c44bc6ad..df715da7c0f 100644 --- a/src/comp/middle/tstate/auxiliary.rs +++ b/src/comp/middle/tstate/auxiliary.rs @@ -255,7 +255,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 = @mutable vec[ts_ann]; +type node_ann_table = @mutable vec[mutable ts_ann]; /* mapping from function name to fn_info map */ type fn_info_map = @std::map::hashmap[def_id, fn_info]; @@ -485,7 +485,7 @@ fn num_constraints(fn_info m) -> uint { } fn new_crate_ctxt(ty::ctxt cx) -> crate_ctxt { - let vec[ts_ann] na = []; + let vec[mutable ts_ann] na = vec::empty_mut(); ret rec(tcx=cx, node_anns=@mutable na, fm=@new_def_hash[fn_info]()); } diff --git a/src/comp/middle/tstate/ck.rs b/src/comp/middle/tstate/ck.rs index 27e885e66d8..0ebc3541ef9 100644 --- a/src/comp/middle/tstate/ck.rs +++ b/src/comp/middle/tstate/ck.rs @@ -114,9 +114,9 @@ fn check_states_stmt(&fn_ctxt fcx, &stmt s) -> () { fn check_states_against_conditions(&fn_ctxt fcx, &_fn f, &ann a) -> () { auto enclosing = fcx.enclosing; auto nv = num_constraints(enclosing); - auto post = @empty_poststate(nv); + auto post = @mutable empty_poststate(nv); - fn do_one_(fn_ctxt fcx, &@stmt s, @poststate post) -> () { + fn do_one_(fn_ctxt fcx, &@stmt s, @mutable poststate post) -> () { check_states_stmt(fcx, *s); *post = stmt_poststate(fcx.ccx, *s); } @@ -124,7 +124,7 @@ fn check_states_against_conditions(&fn_ctxt fcx, &_fn f, &ann a) -> () { auto do_one = bind do_one_(fcx, _, post); vec::map[@stmt, ()](do_one, f.body.node.stmts); - fn do_inner_(fn_ctxt fcx, &@expr e, @poststate post) -> () { + fn do_inner_(fn_ctxt fcx, &@expr e, @mutable poststate post) -> () { check_states_expr(fcx, e); *post = expr_poststate(fcx.ccx, e); } @@ -135,7 +135,7 @@ fn check_states_against_conditions(&fn_ctxt fcx, &_fn f, &ann a) -> () { /* Finally, check that the return value is initialized */ let aux::constr_ ret_c = rec(id=fcx.id, c=aux::ninit(fcx.name)); if (f.proto == ast::proto_fn - && ! promises(fcx, *post, ret_c) + && ! promises(fcx, {*post}, ret_c) && ! type_is_nil(fcx.ccx.tcx, ret_ty_of_fn(fcx.ccx.tcx, a)) && cf == return) { @@ -149,7 +149,7 @@ fn check_states_against_conditions(&fn_ctxt fcx, &_fn f, &ann a) -> () { // check that this really always fails // the fcx.id bit means "returns" for a returning fn, // "diverges" for a non-returning fn - if (! promises(fcx, *post, ret_c)) { + if (! promises(fcx, {*post}, ret_c)) { fcx.ccx.tcx.sess.span_err(f.body.span, "In non-returning function " + fcx.name + ", some control paths may return to the caller"); diff --git a/src/comp/pretty/pp.rs b/src/comp/pretty/pp.rs index 9af781e88ba..e009c1b17e1 100644 --- a/src/comp/pretty/pp.rs +++ b/src/comp/pretty/pp.rs @@ -78,7 +78,7 @@ fn tok_str(token t) -> str { } } -fn buf_str(vec[token] toks, vec[int] szs, +fn buf_str(vec[mutable token] toks, vec[mutable int] szs, uint left, uint right, uint lim) -> str { auto n = vec::len(toks); assert n == vec::len(szs); @@ -112,9 +112,9 @@ fn mk_printer(io::writer out, uint linewidth) -> printer { log #fmt("mk_printer %u", linewidth); - let vec[token] token = vec::init_elt[token](EOF, n); - let vec[int] size = vec::init_elt[int](0, n); - let vec[uint] scan_stack = vec::init_elt[uint](0u, n); + let vec[mutable token] token = vec::init_elt_mut(EOF, n); + let vec[mutable int] size = vec::init_elt_mut(0, n); + let vec[mutable uint] scan_stack = vec::init_elt_mut(0u, n); let vec[print_stack_elt] print_stack = []; ret printer(out, @@ -220,8 +220,8 @@ obj printer(io::writer out, mutable uint left, // index of left side of input stream mutable uint right, // index of right side of input stream - mutable vec[token] token, // ring-buffer stream goes through - mutable vec[int] size, // ring-buffer of calculated sizes + mutable vec[mutable token] token,// ring-buffr stream goes through + mutable vec[mutable int] size, // ring-buffer of calculated sizes mutable int left_total, // running size of stream "...left" mutable int right_total, // running size of stream "...right" @@ -231,7 +231,7 @@ obj printer(io::writer out, // on top of it. Stuff is flushed off the bottom as it becomes // irrelevant due to the primary ring-buffer advancing. - mutable vec[uint] scan_stack, + mutable vec[mutable uint] scan_stack, mutable bool scan_stack_empty, // top==bottom disambiguator mutable uint top, // index of top of scan_stack mutable uint bottom, // index of bottom of scan_stack diff --git a/src/lib/deque.rs b/src/lib/deque.rs index 6ad44af5288..060b435215d 100644 --- a/src/lib/deque.rs +++ b/src/lib/deque.rs @@ -27,26 +27,27 @@ fn create[T]() -> t[T] { * Grow is only called on full elts, so nelts is also len(elts), unlike * elsewhere. */ - fn grow[T](uint nelts, uint lo, vec[cell[T]] elts) -> vec[cell[T]] { - assert (nelts == vec::len[cell[T]](elts)); + fn grow[T](uint nelts, uint lo, vec[mutable cell[T]] elts) + -> vec[mutable cell[T]] { + assert (nelts == vec::len(elts)); // FIXME: Making the vector argument an alias is a workaround for // issue #375 fn fill[T](uint i, uint nelts, uint lo, - &vec[cell[T]] old) -> cell[T] { + &vec[mutable cell[T]] old) -> cell[T] { ret if (i < nelts) { old.((lo + i) % nelts) } else { - option::none[T] + option::none }; } let uint nalloc = uint::next_power_of_two(nelts + 1u); let vec::init_op[cell[T]] copy_op = bind fill[T](_, nelts, lo, elts); - ret vec::init_fn[cell[T]](copy_op, nalloc); + ret vec::init_fn_mut[cell[T]](copy_op, nalloc); } - fn get[T](vec[cell[T]] elts, uint i) -> T { + fn get[T](vec[mutable cell[T]] elts, uint i) -> T { ret alt (elts.(i)) { case (option::some(?t)) { t } case (_) { fail } @@ -56,7 +57,7 @@ fn create[T]() -> t[T] { obj deque[T](mutable uint nelts, mutable uint lo, mutable uint hi, - mutable vec[cell[T]] elts) + mutable vec[mutable cell[T]] elts) { fn size() -> uint { ret nelts; } @@ -130,8 +131,8 @@ fn create[T]() -> t[T] { } } - let vec[cell[T]] v = vec::init_elt[cell[T]](option::none[T], - initial_capacity); + let vec[mutable cell[T]] v = vec::init_elt_mut(option::none, + initial_capacity); ret deque[T](0u, 0u, 0u, v); } diff --git a/src/lib/vec.rs b/src/lib/vec.rs index dbbe4b132a3..4afeb6c8431 100644 --- a/src/lib/vec.rs +++ b/src/lib/vec.rs @@ -385,14 +385,14 @@ fn freeze[T](vec[mutable T] v) -> vec[T] { } // Swaps two elements in a vector -fn swap[T](&vec[T] v, uint a, uint b) { +fn swap[T](&vec[mutable T] v, uint a, uint b) { let T t = v.(a); v.(a) = v.(b); v.(b) = t; } // In place vector reversal -fn reverse[T](&vec[T] v) -> () { +fn reverse[T](&vec[mutable T] v) -> () { let uint i = 0u; auto ln = len[T](v); diff --git a/src/test/bench/shootout/fannkuchredux.rs b/src/test/bench/shootout/fannkuchredux.rs index cca3592ce68..a808c9fea4e 100644 --- a/src/test/bench/shootout/fannkuchredux.rs +++ b/src/test/bench/shootout/fannkuchredux.rs @@ -12,9 +12,9 @@ fn fannkuch(int n) -> int { } auto perm1init_ = perm1init; // Rustboot workaround - auto perm = vec::init_elt(0, n as uint); - auto perm1 = vec::init_fn(perm1init_, n as uint); - auto count = vec::init_elt(0, n as uint); + auto perm = vec::init_elt_mut(0, n as uint); + auto perm1 = vec::init_fn_mut(perm1init_, n as uint); + auto count = vec::init_elt_mut(0, n as uint); auto f = 0; auto i = 0; diff --git a/src/test/run-pass/vec-reverse.rs b/src/test/run-pass/vec-reverse.rs index 5cc9b8a363b..1aecc108bb8 100644 --- a/src/test/run-pass/vec-reverse.rs +++ b/src/test/run-pass/vec-reverse.rs @@ -2,28 +2,28 @@ use std; import std::vec; fn main() { - let vec[int] v = [10, 20]; + let vec[mutable int] v = [mutable 10, 20]; assert v.(0) == 10; assert v.(1) == 20; - vec::reverse[int](v); + vec::reverse(v); assert v.(0) == 20; assert v.(1) == 10; - auto v2 = vec::reversed[int](v); + auto v2 = vec::reversed[int]([10, 20]); - assert v2.(0) == 10; - assert v2.(1) == 20; + assert v2.(0) == 20; + assert v2.(1) == 10; v.(0) = 30; - assert v2.(0) == 10; + assert v2.(0) == 20; // Make sure they work with 0-length vectors too. - let vec[int] v3 = []; - auto v4 = vec::reversed[int](v3); + auto v4 = vec::reversed[int]([]); + let vec[mutable int] v3 = vec::empty_mut(); vec::reverse[int](v3); }