Skip to content

Commit d5bf3b8

Browse files
committed
auto merge of rust-lang#4908 : bstrie/rust/rimov3, r=pcwalton
This patch finishes removing inner vector mutability from the vast majority of the compiler. Exceptions: * core::dvec: ideally this entire type will be able to be replaced by `~[]`, but Niko asked me to hold off on removing Dvecs until he makes some fixes to borrowed pointers. * liveness: liveness.rs is an impenetrable neutron star of deprecated semantics. * compile-fail: I'm not sure if a lot of these tests are testing inner mutability or mutability in general. I figure that RIMOVing this folder should wait until this syntax is removed from the parser. I also took this chance to remove many of the inner-mutability-related functions from core::vec, or as many uses of those functions as possible where still necessary. consume_mut and append_mut have been axed. cast_to_mut and cast_from_mut are still needed in a few places.
2 parents 5e6d787 + e6c82c0 commit d5bf3b8

22 files changed

+96
-123
lines changed

src/compiletest/runtest.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ fn check_expected_errors(expected_errors: ~[errors::ExpectedError],
322322
ProcRes: ProcRes) {
323323

324324
// true if we found the error in question
325-
let found_flags = vec::cast_to_mut(vec::from_elem(
326-
vec::len(expected_errors), false));
325+
let mut found_flags = vec::from_elem(
326+
vec::len(expected_errors), false);
327327

328328
if ProcRes.status == 0 {
329329
fatal(~"process did not return an error status");

src/libcore/hash.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ struct SipState {
165165
mut v1: u64,
166166
mut v2: u64,
167167
mut v3: u64,
168-
tail: [mut u8 * 8], // unprocessed bytes
168+
mut tail: [u8 * 8], // unprocessed bytes
169169
mut ntail: uint, // how many bytes in tail are valid
170170
}
171171

@@ -179,7 +179,7 @@ fn SipState(key0: u64, key1: u64) -> SipState {
179179
mut v1 : 0u64,
180180
mut v2 : 0u64,
181181
mut v3 : 0u64,
182-
tail : [mut 0u8,0,0,0,0,0,0,0],
182+
mut tail : [0u8,0,0,0,0,0,0,0],
183183
mut ntail : 0u,
184184
};
185185
(&state).reset();

src/libcore/io.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub trait Reader {
5656
/// Read up to len bytes (or EOF) and put them into bytes (which
5757
/// must be at least len bytes long). Return number of bytes read.
5858
// FIXME (#2982): This should probably return an error.
59-
fn read(&self, bytes: &[mut u8], len: uint) -> uint;
59+
fn read(&self, bytes: &mut [u8], len: uint) -> uint;
6060

6161
/// Read a single byte, returning a negative value for EOF or read error.
6262
fn read_byte(&self) -> int;
@@ -416,7 +416,7 @@ fn convert_whence(whence: SeekStyle) -> i32 {
416416
}
417417

418418
impl *libc::FILE: Reader {
419-
fn read(&self, bytes: &[mut u8], len: uint) -> uint {
419+
fn read(&self, bytes: &mut [u8], len: uint) -> uint {
420420
unsafe {
421421
do vec::as_mut_buf(bytes) |buf_p, buf_len| {
422422
assert buf_len >= len;
@@ -461,7 +461,7 @@ struct Wrapper<T, C> {
461461
// duration of its lifetime.
462462
// FIXME there really should be a better way to do this // #2004
463463
impl<R: Reader, C> Wrapper<R, C>: Reader {
464-
fn read(&self, bytes: &[mut u8], len: uint) -> uint {
464+
fn read(&self, bytes: &mut [u8], len: uint) -> uint {
465465
self.base.read(bytes, len)
466466
}
467467
fn read_byte(&self) -> int { self.base.read_byte() }
@@ -528,7 +528,7 @@ pub struct BytesReader {
528528
}
529529
530530
impl BytesReader: Reader {
531-
fn read(&self, bytes: &[mut u8], len: uint) -> uint {
531+
fn read(&self, bytes: &mut [u8], len: uint) -> uint {
532532
let count = uint::min(len, self.bytes.len() - self.pos);
533533
534534
let view = vec::view(self.bytes, self.pos, self.bytes.len());

src/libcore/os.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub fn as_c_charp<T>(s: &str, f: fn(*c_char) -> T) -> T {
7979

8080
pub fn fill_charp_buf(f: fn(*mut c_char, size_t) -> bool)
8181
-> Option<~str> {
82-
let buf = vec::cast_to_mut(vec::from_elem(TMPBUF_SZ, 0u8 as c_char));
82+
let mut buf = vec::from_elem(TMPBUF_SZ, 0u8 as c_char);
8383
do vec::as_mut_buf(buf) |b, sz| {
8484
if f(b, sz as size_t) {
8585
unsafe {
@@ -108,7 +108,7 @@ pub mod win32 {
108108
let mut res = None;
109109
let mut done = false;
110110
while !done {
111-
let buf = vec::cast_to_mut(vec::from_elem(n as uint, 0u16));
111+
let mut buf = vec::from_elem(n as uint, 0u16);
112112
do vec::as_mut_buf(buf) |b, _sz| {
113113
let k : DWORD = f(b, TMPBUF_SZ as DWORD);
114114
if k == (0 as DWORD) {
@@ -1325,7 +1325,7 @@ mod tests {
13251325
};
13261326
assert (ostream as uint != 0u);
13271327
let s = ~"hello";
1328-
let mut buf = vec::cast_to_mut(str::to_bytes(s) + ~[0 as u8]);
1328+
let mut buf = str::to_bytes(s) + ~[0 as u8];
13291329
do vec::as_mut_buf(buf) |b, _len| {
13301330
assert (libc::fwrite(b as *c_void, 1u as size_t,
13311331
(str::len(s) + 1u) as size_t, ostream)

src/libcore/rand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ impl Rng {
350350
}
351351

352352
/// Shuffle a mutable vec in place
353-
fn shuffle_mut<T>(values: &[mut T]) {
353+
fn shuffle_mut<T>(values: &mut [T]) {
354354
let mut i = values.len();
355355
while i >= 2u {
356356
// invariant: elements with index >= i have been locked in place.

src/libcore/run.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ pub fn start_program(prog: &str, args: &[~str]) -> Program {
290290

291291
fn read_all(rd: io::Reader) -> ~str {
292292
let buf = io::with_bytes_writer(|wr| {
293-
let mut bytes = [mut 0, ..4096];
293+
let mut bytes = [0, ..4096];
294294
while !rd.eof() {
295295
let nread = rd.read(bytes, bytes.len());
296296
wr.write(bytes.view(0, nread));
@@ -391,7 +391,7 @@ pub fn readclose(fd: c_int) -> ~str {
391391
let file = os::fdopen(fd);
392392
let reader = io::FILE_reader(file, false);
393393
let buf = io::with_bytes_writer(|writer| {
394-
let mut bytes = [mut 0, ..4096];
394+
let mut bytes = [0, ..4096];
395395
while !reader.eof() {
396396
let nread = reader.read(bytes, bytes.len());
397397
writer.write(bytes.view(0, nread));

src/libcore/vec.rs

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -558,10 +558,6 @@ pub fn consume<T>(mut v: ~[T], f: fn(uint, v: T)) {
558558
}
559559
}
560560
561-
pub fn consume_mut<T>(v: ~[mut T], f: fn(uint, v: T)) {
562-
consume(vec::cast_from_mut(v), f)
563-
}
564-
565561
/// Remove the last element from a vector and return it
566562
pub fn pop<T>(v: &mut ~[T]) -> T {
567563
let ln = v.len();
@@ -728,11 +724,6 @@ pub pure fn append_one<T>(lhs: ~[T], x: T) -> ~[T] {
728724
v
729725
}
730726

731-
#[inline(always)]
732-
pub pure fn append_mut<T: Copy>(lhs: ~[mut T], rhs: &[const T]) -> ~[mut T] {
733-
cast_to_mut(append(cast_from_mut(lhs), rhs))
734-
}
735-
736727
/**
737728
* Expands a vector in place, initializing the new elements to a given value
738729
*
@@ -1285,12 +1276,12 @@ pub pure fn zip<T, U>(mut v: ~[T], mut u: ~[U]) -> ~[(T, U)] {
12851276
* * a - The index of the first element
12861277
* * b - The index of the second element
12871278
*/
1288-
pub fn swap<T>(v: &[mut T], a: uint, b: uint) {
1279+
pub fn swap<T>(v: &mut [T], a: uint, b: uint) {
12891280
v[a] <-> v[b];
12901281
}
12911282

12921283
/// Reverse the order of elements in a vector, in place
1293-
pub fn reverse<T>(v: &[mut T]) {
1284+
pub fn reverse<T>(v: &mut [T]) {
12941285
let mut i: uint = 0;
12951286
let ln = len::<T>(v);
12961287
while i < ln / 2 { v[i] <-> v[ln - i - 1]; i += 1; }
@@ -1371,7 +1362,7 @@ pub pure fn each<T>(v: &r/[T], f: fn(&r/T) -> bool) {
13711362
/// a vector with mutable contents and you would like
13721363
/// to mutate the contents as you iterate.
13731364
#[inline(always)]
1374-
pub fn each_mut<T>(v: &[mut T], f: fn(elem: &mut T) -> bool) {
1365+
pub fn each_mut<T>(v: &mut [T], f: fn(elem: &mut T) -> bool) {
13751366
let mut i = 0;
13761367
let n = v.len();
13771368
while i < n {
@@ -1541,7 +1532,7 @@ pub pure fn as_const_buf<T,U>(s: &[const T],
15411532

15421533
/// Similar to `as_imm_buf` but passing a `*mut T`
15431534
#[inline(always)]
1544-
pub pure fn as_mut_buf<T,U>(s: &[mut T],
1535+
pub pure fn as_mut_buf<T,U>(s: &mut [T],
15451536
f: fn(*mut T, uint) -> U) -> U {
15461537

15471538
unsafe {
@@ -1653,21 +1644,14 @@ impl<T: Ord> @[T] : Ord {
16531644
pub mod traits {
16541645
use kinds::Copy;
16551646
use ops::Add;
1656-
use vec::{append, append_mut};
1647+
use vec::append;
16571648

16581649
impl<T: Copy> ~[T] : Add<&[const T],~[T]> {
16591650
#[inline(always)]
16601651
pure fn add(&self, rhs: & &self/[const T]) -> ~[T] {
16611652
append(copy *self, (*rhs))
16621653
}
16631654
}
1664-
1665-
impl<T: Copy> ~[mut T] : Add<&[const T],~[mut T]> {
1666-
#[inline(always)]
1667-
pure fn add(&self, rhs: & &self/[const T]) -> ~[mut T] {
1668-
append_mut(copy *self, (*rhs))
1669-
}
1670-
}
16711655
}
16721656

16731657
impl<T> &[const T]: Container {
@@ -2088,7 +2072,7 @@ pub mod raw {
20882072

20892073
/** see `to_ptr()` */
20902074
#[inline(always)]
2091-
pub unsafe fn to_mut_ptr<T>(v: &[mut T]) -> *mut T {
2075+
pub unsafe fn to_mut_ptr<T>(v: &mut [T]) -> *mut T {
20922076
let repr: **SliceRepr = ::cast::transmute(&v);
20932077
return ::cast::reinterpret_cast(&addr_of(&((**repr).data)));
20942078
}
@@ -2121,7 +2105,7 @@ pub mod raw {
21212105
* is newly allocated.
21222106
*/
21232107
#[inline(always)]
2124-
pub unsafe fn init_elem<T>(v: &[mut T], i: uint, val: T) {
2108+
pub unsafe fn init_elem<T>(v: &mut [T], i: uint, val: T) {
21252109
let mut box = Some(val);
21262110
do as_mut_buf(v) |p, _len| {
21272111
let mut box2 = None;
@@ -2155,7 +2139,7 @@ pub mod raw {
21552139
* may overlap.
21562140
*/
21572141
#[inline(always)]
2158-
pub unsafe fn copy_memory<T>(dst: &[mut T], src: &[const T],
2142+
pub unsafe fn copy_memory<T>(dst: &mut [T], src: &[const T],
21592143
count: uint) {
21602144
assert dst.len() >= count;
21612145
assert src.len() >= count;
@@ -2222,7 +2206,7 @@ pub mod bytes {
22222206
* may overlap.
22232207
*/
22242208
#[inline(always)]
2225-
pub fn copy_memory(dst: &[mut u8], src: &[const u8], count: uint) {
2209+
pub fn copy_memory(dst: &mut [u8], src: &[const u8], count: uint) {
22262210
// Bound checks are done at vec::raw::copy_memory.
22272211
unsafe { vec::raw::copy_memory(dst, src, count) }
22282212
}
@@ -3220,7 +3204,7 @@ mod tests {
32203204

32213205
#[test]
32223206
fn reverse_and_reversed() {
3223-
let v: ~[mut int] = ~[mut 10, 20];
3207+
let mut v: ~[int] = ~[10, 20];
32243208
assert (v[0] == 10);
32253209
assert (v[1] == 20);
32263210
reverse(v);
@@ -3235,13 +3219,13 @@ mod tests {
32353219

32363220
let v4 = reversed::<int>(~[]);
32373221
assert (v4 == ~[]);
3238-
let v3: ~[mut int] = ~[mut];
3222+
let mut v3: ~[int] = ~[];
32393223
reverse::<int>(v3);
32403224
}
32413225

32423226
#[test]
32433227
fn reversed_mut() {
3244-
let v2 = reversed::<int>(~[mut 10, 20]);
3228+
let mut v2 = reversed::<int>(~[10, 20]);
32453229
assert (v2[0] == 20);
32463230
assert (v2[1] == 10);
32473231
}
@@ -3625,20 +3609,6 @@ mod tests {
36253609
};
36263610
}
36273611

3628-
#[test]
3629-
#[ignore(windows)]
3630-
#[should_fail]
3631-
fn test_consume_mut_fail() {
3632-
let v = ~[mut (~0, @0), (~0, @0), (~0, @0), (~0, @0)];
3633-
let mut i = 0;
3634-
do consume_mut(v) |_i, _elt| {
3635-
if i == 2 {
3636-
die!()
3637-
}
3638-
i += 1;
3639-
};
3640-
}
3641-
36423612
#[test]
36433613
#[ignore(windows)]
36443614
#[should_fail]
@@ -3657,7 +3627,7 @@ mod tests {
36573627
#[ignore(windows)]
36583628
#[should_fail]
36593629
fn test_map_fail() {
3660-
let v = [mut (~0, @0), (~0, @0), (~0, @0), (~0, @0)];
3630+
let mut v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
36613631
let mut i = 0;
36623632
do map(v) |_elt| {
36633633
if i == 2 {
@@ -3983,7 +3953,7 @@ mod tests {
39833953
#[ignore(cfg(windows))]
39843954
#[should_fail]
39853955
fn test_as_mut_buf_fail() {
3986-
let v = [mut (~0, @0), (~0, @0), (~0, @0), (~0, @0)];
3956+
let mut v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
39873957
do as_mut_buf(v) |_buf, _i| {
39883958
die!()
39893959
}
@@ -3994,7 +3964,7 @@ mod tests {
39943964
#[ignore(cfg(windows))]
39953965
fn test_copy_memory_oob() {
39963966
unsafe {
3997-
let a = [mut 1, 2, 3, 4];
3967+
let mut a = [1, 2, 3, 4];
39983968
let b = [1, 2, 3, 4, 5];
39993969
raw::copy_memory(a, b, 5);
40003970
}

src/librustc/middle/trans/_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ pub fn pick_col(m: &[@Match]) -> uint {
10021002
_ => 0u
10031003
}
10041004
}
1005-
let scores = vec::cast_to_mut(vec::from_elem(m[0].pats.len(), 0u));
1005+
let mut scores = vec::from_elem(m[0].pats.len(), 0u);
10061006
for vec::each(m) |br| {
10071007
let mut i = 0u;
10081008
for vec::each(br.pats) |p| { scores[i] += score(*p); i += 1u; }

src/librustc/middle/trans/cabi_x86_64.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ fn classify_ty(ty: TypeRef) -> ~[x86_64_reg_class] {
127127
}
128128
}
129129
130-
fn all_mem(cls: &[mut x86_64_reg_class]) {
130+
fn all_mem(cls: &mut [x86_64_reg_class]) {
131131
for uint::range(0, cls.len()) |i| {
132132
cls[i] = memory_class;
133133
}
134134
}
135135
136-
fn unify(cls: &[mut x86_64_reg_class],
136+
fn unify(cls: &mut [x86_64_reg_class],
137137
i: uint,
138138
newv: x86_64_reg_class) {
139139
if cls[i] == newv {
@@ -159,7 +159,7 @@ fn classify_ty(ty: TypeRef) -> ~[x86_64_reg_class] {
159159
}
160160
161161
fn classify_struct(tys: &[TypeRef],
162-
cls: &[mut x86_64_reg_class], i: uint,
162+
cls: &mut [x86_64_reg_class], i: uint,
163163
off: uint) {
164164
let mut field_off = off;
165165
for vec::each(tys) |ty| {
@@ -170,7 +170,7 @@ fn classify_ty(ty: TypeRef) -> ~[x86_64_reg_class] {
170170
}
171171
172172
fn classify(ty: TypeRef,
173-
cls: &[mut x86_64_reg_class], ix: uint,
173+
cls: &mut [x86_64_reg_class], ix: uint,
174174
off: uint) {
175175
unsafe {
176176
let t_align = ty_align(ty);
@@ -220,7 +220,7 @@ fn classify_ty(ty: TypeRef) -> ~[x86_64_reg_class] {
220220
}
221221
}
222222
223-
fn fixup(ty: TypeRef, cls: &[mut x86_64_reg_class]) {
223+
fn fixup(ty: TypeRef, cls: &mut [x86_64_reg_class]) {
224224
unsafe {
225225
let mut i = 0u;
226226
let llty = llvm::LLVMGetTypeKind(ty) as int;
@@ -270,14 +270,15 @@ fn classify_ty(ty: TypeRef) -> ~[x86_64_reg_class] {
270270
}
271271
272272
let words = (ty_size(ty) + 7) / 8;
273-
let cls = vec::cast_to_mut(vec::from_elem(words, no_class));
273+
let mut cls = vec::from_elem(words, no_class);
274274
if words > 4 {
275275
all_mem(cls);
276-
return vec::cast_from_mut(move cls);
276+
let cls = cls;
277+
return move cls;
277278
}
278279
classify(ty, cls, 0, 0);
279280
fixup(ty, cls);
280-
return vec::cast_from_mut(move cls);
281+
return move cls;
281282
}
282283
283284
fn llreg_ty(cls: &[x86_64_reg_class]) -> TypeRef {

src/librustc/middle/typeck/check/method.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ pub impl LookupContext {
779779
/*!
780780
*
781781
* In the event that we are invoking a method with a receiver
782-
* of a linear borrowed type like `&mut T` or `&[mut T]`,
782+
* of a linear borrowed type like `&mut T` or `&mut [T]`,
783783
* we will "reborrow" the receiver implicitly. For example, if
784784
* you have a call `r.inc()` and where `r` has type `&mut T`,
785785
* then we treat that like `(&mut *r).inc()`. This avoids

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3131,7 +3131,7 @@ pub fn check_bounds_are_used(ccx: @mut CrateCtxt,
31313131

31323132
// make a vector of booleans initially false, set to true when used
31333133
if tps.len() == 0u { return; }
3134-
let tps_used = vec::cast_to_mut(vec::from_elem(tps.len(), false));
3134+
let mut tps_used = vec::from_elem(tps.len(), false);
31353135

31363136
ty::walk_regions_and_ty(
31373137
ccx.tcx, ty,

0 commit comments

Comments
 (0)