Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto: Remove interior mutability on vectors, round 2 #4908

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ fn check_expected_errors(expected_errors: ~[errors::ExpectedError],
ProcRes: ProcRes) {

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

if ProcRes.status == 0 {
fatal(~"process did not return an error status");
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ struct SipState {
mut v1: u64,
mut v2: u64,
mut v3: u64,
tail: [mut u8 * 8], // unprocessed bytes
mut tail: [u8 * 8], // unprocessed bytes
mut ntail: uint, // how many bytes in tail are valid
}

Expand All @@ -179,7 +179,7 @@ fn SipState(key0: u64, key1: u64) -> SipState {
mut v1 : 0u64,
mut v2 : 0u64,
mut v3 : 0u64,
tail : [mut 0u8,0,0,0,0,0,0,0],
mut tail : [0u8,0,0,0,0,0,0,0],
mut ntail : 0u,
};
(&state).reset();
Expand Down
8 changes: 4 additions & 4 deletions src/libcore/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub trait Reader {
/// Read up to len bytes (or EOF) and put them into bytes (which
/// must be at least len bytes long). Return number of bytes read.
// FIXME (#2982): This should probably return an error.
fn read(&self, bytes: &[mut u8], len: uint) -> uint;
fn read(&self, bytes: &mut [u8], len: uint) -> uint;

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

impl *libc::FILE: Reader {
fn read(&self, bytes: &[mut u8], len: uint) -> uint {
fn read(&self, bytes: &mut [u8], len: uint) -> uint {
unsafe {
do vec::as_mut_buf(bytes) |buf_p, buf_len| {
assert buf_len >= len;
Expand Down Expand Up @@ -461,7 +461,7 @@ struct Wrapper<T, C> {
// duration of its lifetime.
// FIXME there really should be a better way to do this // #2004
impl<R: Reader, C> Wrapper<R, C>: Reader {
fn read(&self, bytes: &[mut u8], len: uint) -> uint {
fn read(&self, bytes: &mut [u8], len: uint) -> uint {
self.base.read(bytes, len)
}
fn read_byte(&self) -> int { self.base.read_byte() }
Expand Down Expand Up @@ -528,7 +528,7 @@ pub struct BytesReader {
}

impl BytesReader: Reader {
fn read(&self, bytes: &[mut u8], len: uint) -> uint {
fn read(&self, bytes: &mut [u8], len: uint) -> uint {
let count = uint::min(len, self.bytes.len() - self.pos);

let view = vec::view(self.bytes, self.pos, self.bytes.len());
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub fn as_c_charp<T>(s: &str, f: fn(*c_char) -> T) -> T {

pub fn fill_charp_buf(f: fn(*mut c_char, size_t) -> bool)
-> Option<~str> {
let buf = vec::cast_to_mut(vec::from_elem(TMPBUF_SZ, 0u8 as c_char));
let mut buf = vec::from_elem(TMPBUF_SZ, 0u8 as c_char);
do vec::as_mut_buf(buf) |b, sz| {
if f(b, sz as size_t) {
unsafe {
Expand Down Expand Up @@ -108,7 +108,7 @@ pub mod win32 {
let mut res = None;
let mut done = false;
while !done {
let buf = vec::cast_to_mut(vec::from_elem(n as uint, 0u16));
let mut buf = vec::from_elem(n as uint, 0u16);
do vec::as_mut_buf(buf) |b, _sz| {
let k : DWORD = f(b, TMPBUF_SZ as DWORD);
if k == (0 as DWORD) {
Expand Down Expand Up @@ -1325,7 +1325,7 @@ mod tests {
};
assert (ostream as uint != 0u);
let s = ~"hello";
let mut buf = vec::cast_to_mut(str::to_bytes(s) + ~[0 as u8]);
let mut buf = str::to_bytes(s) + ~[0 as u8];
do vec::as_mut_buf(buf) |b, _len| {
assert (libc::fwrite(b as *c_void, 1u as size_t,
(str::len(s) + 1u) as size_t, ostream)
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ impl Rng {
}

/// Shuffle a mutable vec in place
fn shuffle_mut<T>(values: &[mut T]) {
fn shuffle_mut<T>(values: &mut [T]) {
let mut i = values.len();
while i >= 2u {
// invariant: elements with index >= i have been locked in place.
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ pub fn start_program(prog: &str, args: &[~str]) -> Program {

fn read_all(rd: io::Reader) -> ~str {
let buf = io::with_bytes_writer(|wr| {
let mut bytes = [mut 0, ..4096];
let mut bytes = [0, ..4096];
while !rd.eof() {
let nread = rd.read(bytes, bytes.len());
wr.write(bytes.view(0, nread));
Expand Down Expand Up @@ -391,7 +391,7 @@ pub fn readclose(fd: c_int) -> ~str {
let file = os::fdopen(fd);
let reader = io::FILE_reader(file, false);
let buf = io::with_bytes_writer(|writer| {
let mut bytes = [mut 0, ..4096];
let mut bytes = [0, ..4096];
while !reader.eof() {
let nread = reader.read(bytes, bytes.len());
writer.write(bytes.view(0, nread));
Expand Down
60 changes: 15 additions & 45 deletions src/libcore/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,10 +558,6 @@ pub fn consume<T>(mut v: ~[T], f: fn(uint, v: T)) {
}
}

pub fn consume_mut<T>(v: ~[mut T], f: fn(uint, v: T)) {
consume(vec::cast_from_mut(v), f)
}

/// Remove the last element from a vector and return it
pub fn pop<T>(v: &mut ~[T]) -> T {
let ln = v.len();
Expand Down Expand Up @@ -728,11 +724,6 @@ pub pure fn append_one<T>(lhs: ~[T], x: T) -> ~[T] {
v
}

#[inline(always)]
pub pure fn append_mut<T: Copy>(lhs: ~[mut T], rhs: &[const T]) -> ~[mut T] {
cast_to_mut(append(cast_from_mut(lhs), rhs))
}

/**
* Expands a vector in place, initializing the new elements to a given value
*
Expand Down Expand Up @@ -1285,12 +1276,12 @@ pub pure fn zip<T, U>(mut v: ~[T], mut u: ~[U]) -> ~[(T, U)] {
* * a - The index of the first element
* * b - The index of the second element
*/
pub fn swap<T>(v: &[mut T], a: uint, b: uint) {
pub fn swap<T>(v: &mut [T], a: uint, b: uint) {
v[a] <-> v[b];
}

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

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

unsafe {
Expand Down Expand Up @@ -1653,21 +1644,14 @@ impl<T: Ord> @[T] : Ord {
pub mod traits {
use kinds::Copy;
use ops::Add;
use vec::{append, append_mut};
use vec::append;

impl<T: Copy> ~[T] : Add<&[const T],~[T]> {
#[inline(always)]
pure fn add(&self, rhs: & &self/[const T]) -> ~[T] {
append(copy *self, (*rhs))
}
}

impl<T: Copy> ~[mut T] : Add<&[const T],~[mut T]> {
#[inline(always)]
pure fn add(&self, rhs: & &self/[const T]) -> ~[mut T] {
append_mut(copy *self, (*rhs))
}
}
}

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

/** see `to_ptr()` */
#[inline(always)]
pub unsafe fn to_mut_ptr<T>(v: &[mut T]) -> *mut T {
pub unsafe fn to_mut_ptr<T>(v: &mut [T]) -> *mut T {
let repr: **SliceRepr = ::cast::transmute(&v);
return ::cast::reinterpret_cast(&addr_of(&((**repr).data)));
}
Expand Down Expand Up @@ -2121,7 +2105,7 @@ pub mod raw {
* is newly allocated.
*/
#[inline(always)]
pub unsafe fn init_elem<T>(v: &[mut T], i: uint, val: T) {
pub unsafe fn init_elem<T>(v: &mut [T], i: uint, val: T) {
let mut box = Some(val);
do as_mut_buf(v) |p, _len| {
let mut box2 = None;
Expand Down Expand Up @@ -2155,7 +2139,7 @@ pub mod raw {
* may overlap.
*/
#[inline(always)]
pub unsafe fn copy_memory<T>(dst: &[mut T], src: &[const T],
pub unsafe fn copy_memory<T>(dst: &mut [T], src: &[const T],
count: uint) {
assert dst.len() >= count;
assert src.len() >= count;
Expand Down Expand Up @@ -2222,7 +2206,7 @@ pub mod bytes {
* may overlap.
*/
#[inline(always)]
pub fn copy_memory(dst: &[mut u8], src: &[const u8], count: uint) {
pub fn copy_memory(dst: &mut [u8], src: &[const u8], count: uint) {
// Bound checks are done at vec::raw::copy_memory.
unsafe { vec::raw::copy_memory(dst, src, count) }
}
Expand Down Expand Up @@ -3220,7 +3204,7 @@ mod tests {

#[test]
fn reverse_and_reversed() {
let v: ~[mut int] = ~[mut 10, 20];
let mut v: ~[int] = ~[10, 20];
assert (v[0] == 10);
assert (v[1] == 20);
reverse(v);
Expand All @@ -3235,13 +3219,13 @@ mod tests {

let v4 = reversed::<int>(~[]);
assert (v4 == ~[]);
let v3: ~[mut int] = ~[mut];
let mut v3: ~[int] = ~[];
reverse::<int>(v3);
}

#[test]
fn reversed_mut() {
let v2 = reversed::<int>(~[mut 10, 20]);
let mut v2 = reversed::<int>(~[10, 20]);
assert (v2[0] == 20);
assert (v2[1] == 10);
}
Expand Down Expand Up @@ -3625,20 +3609,6 @@ mod tests {
};
}

#[test]
#[ignore(windows)]
#[should_fail]
fn test_consume_mut_fail() {
let v = ~[mut (~0, @0), (~0, @0), (~0, @0), (~0, @0)];
let mut i = 0;
do consume_mut(v) |_i, _elt| {
if i == 2 {
die!()
}
i += 1;
};
}

#[test]
#[ignore(windows)]
#[should_fail]
Expand All @@ -3657,7 +3627,7 @@ mod tests {
#[ignore(windows)]
#[should_fail]
fn test_map_fail() {
let v = [mut (~0, @0), (~0, @0), (~0, @0), (~0, @0)];
let mut v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
let mut i = 0;
do map(v) |_elt| {
if i == 2 {
Expand Down Expand Up @@ -3983,7 +3953,7 @@ mod tests {
#[ignore(cfg(windows))]
#[should_fail]
fn test_as_mut_buf_fail() {
let v = [mut (~0, @0), (~0, @0), (~0, @0), (~0, @0)];
let mut v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
do as_mut_buf(v) |_buf, _i| {
die!()
}
Expand All @@ -3994,7 +3964,7 @@ mod tests {
#[ignore(cfg(windows))]
fn test_copy_memory_oob() {
unsafe {
let a = [mut 1, 2, 3, 4];
let mut a = [1, 2, 3, 4];
let b = [1, 2, 3, 4, 5];
raw::copy_memory(a, b, 5);
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ pub fn pick_col(m: &[@Match]) -> uint {
_ => 0u
}
}
let scores = vec::cast_to_mut(vec::from_elem(m[0].pats.len(), 0u));
let mut scores = vec::from_elem(m[0].pats.len(), 0u);
for vec::each(m) |br| {
let mut i = 0u;
for vec::each(br.pats) |p| { scores[i] += score(*p); i += 1u; }
Expand Down
17 changes: 9 additions & 8 deletions src/librustc/middle/trans/cabi_x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ fn classify_ty(ty: TypeRef) -> ~[x86_64_reg_class] {
}
}

fn all_mem(cls: &[mut x86_64_reg_class]) {
fn all_mem(cls: &mut [x86_64_reg_class]) {
for uint::range(0, cls.len()) |i| {
cls[i] = memory_class;
}
}

fn unify(cls: &[mut x86_64_reg_class],
fn unify(cls: &mut [x86_64_reg_class],
i: uint,
newv: x86_64_reg_class) {
if cls[i] == newv {
Expand All @@ -159,7 +159,7 @@ fn classify_ty(ty: TypeRef) -> ~[x86_64_reg_class] {
}

fn classify_struct(tys: &[TypeRef],
cls: &[mut x86_64_reg_class], i: uint,
cls: &mut [x86_64_reg_class], i: uint,
off: uint) {
let mut field_off = off;
for vec::each(tys) |ty| {
Expand All @@ -170,7 +170,7 @@ fn classify_ty(ty: TypeRef) -> ~[x86_64_reg_class] {
}

fn classify(ty: TypeRef,
cls: &[mut x86_64_reg_class], ix: uint,
cls: &mut [x86_64_reg_class], ix: uint,
off: uint) {
unsafe {
let t_align = ty_align(ty);
Expand Down Expand Up @@ -220,7 +220,7 @@ fn classify_ty(ty: TypeRef) -> ~[x86_64_reg_class] {
}
}

fn fixup(ty: TypeRef, cls: &[mut x86_64_reg_class]) {
fn fixup(ty: TypeRef, cls: &mut [x86_64_reg_class]) {
unsafe {
let mut i = 0u;
let llty = llvm::LLVMGetTypeKind(ty) as int;
Expand Down Expand Up @@ -270,14 +270,15 @@ fn classify_ty(ty: TypeRef) -> ~[x86_64_reg_class] {
}

let words = (ty_size(ty) + 7) / 8;
let cls = vec::cast_to_mut(vec::from_elem(words, no_class));
let mut cls = vec::from_elem(words, no_class);
if words > 4 {
all_mem(cls);
return vec::cast_from_mut(move cls);
let cls = cls;
return move cls;
}
classify(ty, cls, 0, 0);
fixup(ty, cls);
return vec::cast_from_mut(move cls);
return move cls;
}

fn llreg_ty(cls: &[x86_64_reg_class]) -> TypeRef {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/typeck/check/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ pub impl LookupContext {
/*!
*
* In the event that we are invoking a method with a receiver
* of a linear borrowed type like `&mut T` or `&[mut T]`,
* of a linear borrowed type like `&mut T` or `&mut [T]`,
* we will "reborrow" the receiver implicitly. For example, if
* you have a call `r.inc()` and where `r` has type `&mut T`,
* then we treat that like `(&mut *r).inc()`. This avoids
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3131,7 +3131,7 @@ pub fn check_bounds_are_used(ccx: @mut CrateCtxt,

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

ty::walk_regions_and_ty(
ccx.tcx, ty,
Expand Down
Loading