Replace 'mutable?' with 'const'

This commit is contained in:
Brian Anderson 2011-11-16 14:41:32 -08:00
parent 1d361f6806
commit b655fb9ea7
23 changed files with 53 additions and 54 deletions

View file

@ -746,8 +746,7 @@ fn parse_path_and_ty_param_substs(p: parser) -> ast::path {
fn parse_mutability(p: parser) -> ast::mutability { fn parse_mutability(p: parser) -> ast::mutability {
if eat_word(p, "mutable") { if eat_word(p, "mutable") {
if p.peek() == token::QUES { p.bump(); ast::maybe_mut } ast::mut
else { ast::mut }
} else if eat_word(p, "const") { } else if eat_word(p, "const") {
ast::maybe_mut ast::maybe_mut
} else { } else {

View file

@ -1307,7 +1307,7 @@ fn print_op_maybe_parens(s: ps, expr: @ast::expr, outer_prec: int) {
fn print_mutability(s: ps, mut: ast::mutability) { fn print_mutability(s: ps, mut: ast::mutability) {
alt mut { alt mut {
ast::mut. { word_nbsp(s, "mutable"); } ast::mut. { word_nbsp(s, "mutable"); }
ast::maybe_mut. { word_nbsp(s, "mutable?"); } ast::maybe_mut. { word_nbsp(s, "const"); }
ast::imm. {/* nothing */ } ast::imm. {/* nothing */ }
} }
} }

View file

@ -84,7 +84,7 @@ fn ty_to_str(cx: ctxt, typ: t) -> str {
alt m.mut { alt m.mut {
ast::mut. { mstr = "mutable "; } ast::mut. { mstr = "mutable "; }
ast::imm. { mstr = ""; } ast::imm. { mstr = ""; }
ast::maybe_mut. { mstr = "mutable? "; } ast::maybe_mut. { mstr = "const "; }
} }
ret mstr + ty_to_str(cx, m.ty); ret mstr + ty_to_str(cx, m.ty);
} }

View file

@ -188,7 +188,7 @@ fn file_reader(path: str) -> result::t<reader, str> {
// Byte buffer readers // Byte buffer readers
// TODO: mutable? u8, but this fails with rustboot. // TODO: const u8, but this fails with rustboot.
type byte_buf = @{buf: [u8], mutable pos: uint}; type byte_buf = @{buf: [u8], mutable pos: uint};
obj byte_buf_reader(bbuf: byte_buf) { obj byte_buf_reader(bbuf: byte_buf) {

View file

@ -25,7 +25,7 @@ Function: from_vec
Create a list from a vector Create a list from a vector
*/ */
fn from_vec<T>(v: [mutable? T]) -> list<T> { fn from_vec<T>(v: [const T]) -> list<T> {
*vec::foldr({ |h, t| @cons(h, t) }, @nil::<T>, v) *vec::foldr({ |h, t| @cons(h, t) }, @nil::<T>, v)
} }

View file

@ -20,7 +20,7 @@ Merge sort. Returns a new vector containing the sorted list.
Has worst case O(n log n) performance, best case O(n), but Has worst case O(n log n) performance, best case O(n), but
is not space efficient. This is a stable sort. is not space efficient. This is a stable sort.
*/ */
fn merge_sort<T>(le: lteq<T>, v: [mutable? T]) -> [T] { fn merge_sort<T>(le: lteq<T>, v: [const T]) -> [T] {
fn merge<T>(le: lteq<T>, a: [T], b: [T]) -> [T] { fn merge<T>(le: lteq<T>, a: [T], b: [T]) -> [T] {
let rs: [T] = []; let rs: [T] = [];
let a_len: uint = len::<T>(a); let a_len: uint = len::<T>(a);

View file

@ -189,7 +189,7 @@ Function: unsafe_from_bytes
Converts a vector of bytes to a string. Does not verify that the Converts a vector of bytes to a string. Does not verify that the
vector contains valid UTF-8. vector contains valid UTF-8.
*/ */
fn unsafe_from_bytes(v: [mutable? u8]) -> str unsafe { fn unsafe_from_bytes(v: [const u8]) -> str unsafe {
let vcopy: [u8] = v + [0u8]; let vcopy: [u8] = v + [0u8];
let scopy: str = unsafe::reinterpret_cast(vcopy); let scopy: str = unsafe::reinterpret_cast(vcopy);
unsafe::leak(vcopy); unsafe::leak(vcopy);

View file

@ -44,7 +44,7 @@ fn set_count(ufnd: ufind) -> uint { ret vec::len::<node>(ufnd.nodes); }
// Removes all sets with IDs greater than or equal to the given value. // Removes all sets with IDs greater than or equal to the given value.
fn prune(ufnd: ufind, n: uint) { fn prune(ufnd: ufind, n: uint) {
// TODO: Use "slice" once we get rid of "mutable?" // TODO: Use "slice" once we get rid of "const"
let len = vec::len::<node>(ufnd.nodes); let len = vec::len::<node>(ufnd.nodes);
while len != n { vec::pop::<node>(ufnd.nodes); len -= 1u; } while len != n { vec::pop::<node>(ufnd.nodes); len -= 1u; }

View file

@ -8,13 +8,13 @@ import ptr::addr_of;
#[abi = "rust-intrinsic"] #[abi = "rust-intrinsic"]
native mod rusti { native mod rusti {
fn vec_len<T>(&&v: [mutable? T]) -> uint; fn vec_len<T>(&&v: [const T]) -> uint;
} }
#[abi = "cdecl"] #[abi = "cdecl"]
native mod rustrt { native mod rustrt {
fn vec_reserve_shared<T>(t: *sys::type_desc, fn vec_reserve_shared<T>(t: *sys::type_desc,
&v: [mutable? T], &v: [const T],
n: uint); n: uint);
fn vec_from_buf_shared<T>(t: *sys::type_desc, fn vec_from_buf_shared<T>(t: *sys::type_desc,
ptr: *T, ptr: *T,
@ -34,7 +34,7 @@ Predicate: is_empty
Returns true if a vector contains no elements. Returns true if a vector contains no elements.
*/ */
pure fn is_empty<T>(v: [mutable? T]) -> bool { pure fn is_empty<T>(v: [const T]) -> bool {
// FIXME: This would be easier if we could just call len // FIXME: This would be easier if we could just call len
for t: T in v { ret false; } for t: T in v { ret false; }
ret true; ret true;
@ -45,7 +45,7 @@ Predicate: is_not_empty
Returns true if a vector contains some elements. Returns true if a vector contains some elements.
*/ */
pure fn is_not_empty<T>(v: [mutable? T]) -> bool { ret !is_empty(v); } pure fn is_not_empty<T>(v: [const T]) -> bool { ret !is_empty(v); }
/* /*
Predicate: same_length Predicate: same_length
@ -69,7 +69,7 @@ Parameters:
v - A vector v - A vector
n - The number of elements to reserve space for n - The number of elements to reserve space for
*/ */
fn reserve<T>(&v: [mutable? T], n: uint) { fn reserve<T>(&v: [const T], n: uint) {
rustrt::vec_reserve_shared(sys::get_type_desc::<T>(), v, n); rustrt::vec_reserve_shared(sys::get_type_desc::<T>(), v, n);
} }
@ -78,7 +78,7 @@ Function: len
Returns the length of a vector Returns the length of a vector
*/ */
pure fn len<T>(v: [mutable? T]) -> uint { unchecked { rusti::vec_len(v) } } pure fn len<T>(v: [const T]) -> uint { unchecked { rusti::vec_len(v) } }
/* /*
Function: init_fn Function: init_fn
@ -181,7 +181,7 @@ Returns the first element of a vector
Predicates: Predicates:
<is_not_empty> (v) <is_not_empty> (v)
*/ */
fn head<T>(v: [mutable? T]) : is_not_empty(v) -> T { ret v[0]; } fn head<T>(v: [const T]) : is_not_empty(v) -> T { ret v[0]; }
/* /*
Function: tail Function: tail
@ -191,7 +191,7 @@ Returns all but the first element of a vector
Predicates: Predicates:
<is_not_empty> (v) <is_not_empty> (v)
*/ */
fn tail<T>(v: [mutable? T]) : is_not_empty(v) -> [T] { fn tail<T>(v: [const T]) : is_not_empty(v) -> [T] {
ret slice(v, 1u, len(v)); ret slice(v, 1u, len(v));
} }
@ -206,7 +206,7 @@ Returns all but the last elemnt of a vector
Preconditions: Preconditions:
`v` is not empty `v` is not empty
*/ */
fn init<T>(v: [mutable? T]) -> [T] { fn init<T>(v: [const T]) -> [T] {
assert len(v) != 0u; assert len(v) != 0u;
slice(v, 0u, len(v) - 1u) slice(v, 0u, len(v) - 1u)
} }
@ -221,7 +221,7 @@ Returns:
An option containing the last element of `v` if `v` is not empty, or An option containing the last element of `v` if `v` is not empty, or
none if `v` is empty. none if `v` is empty.
*/ */
fn last<T>(v: [mutable? T]) -> option::t<T> { fn last<T>(v: [const T]) -> option::t<T> {
if len(v) == 0u { ret none; } if len(v) == 0u { ret none; }
ret some(v[len(v) - 1u]); ret some(v[len(v) - 1u]);
} }
@ -234,7 +234,7 @@ Returns the last element of a non-empty vector `v`
Predicates: Predicates:
<is_not_empty> (v) <is_not_empty> (v)
*/ */
fn last_total<T>(v: [mutable? T]) : is_not_empty(v) -> T { fn last_total<T>(v: [const T]) : is_not_empty(v) -> T {
ret v[len(v) - 1u]; ret v[len(v) - 1u];
} }
@ -243,7 +243,7 @@ Function: slice
Returns a copy of the elements from [`start`..`end`) from `v`. Returns a copy of the elements from [`start`..`end`) from `v`.
*/ */
fn slice<T>(v: [mutable? T], start: uint, end: uint) -> [T] { fn slice<T>(v: [const T], start: uint, end: uint) -> [T] {
assert (start <= end); assert (start <= end);
assert (end <= len(v)); assert (end <= len(v));
let result = []; let result = [];
@ -259,7 +259,7 @@ Function: slice_mut
Returns a copy of the elements from [`start`..`end`) from `v`. Returns a copy of the elements from [`start`..`end`) from `v`.
*/ */
fn slice_mut<T>(v: [mutable? T], start: uint, end: uint) -> [mutable T] { fn slice_mut<T>(v: [const T], start: uint, end: uint) -> [mutable T] {
assert (start <= end); assert (start <= end);
assert (end <= len(v)); assert (end <= len(v));
let result = [mutable]; let result = [mutable];
@ -277,7 +277,7 @@ Function: shift
Removes the first element from a vector and return it Removes the first element from a vector and return it
*/ */
fn shift<T>(&v: [mutable? T]) -> T { fn shift<T>(&v: [const T]) -> T {
let ln = len::<T>(v); let ln = len::<T>(v);
assert (ln > 0u); assert (ln > 0u);
let e = v[0]; let e = v[0];
@ -291,7 +291,7 @@ Function: pop
Remove the last element from a vector and return it Remove the last element from a vector and return it
*/ */
fn pop<T>(&v: [mutable? T]) -> T { fn pop<T>(&v: [const T]) -> T {
let ln = len(v); let ln = len(v);
assert (ln > 0u); assert (ln > 0u);
ln -= 1u; ln -= 1u;
@ -323,7 +323,7 @@ fn grow<T>(&v: [T], n: uint, initval: T) {
} }
// TODO: Remove me once we have slots. // TODO: Remove me once we have slots.
// FIXME: Can't grow take a [mutable? T] // FIXME: Can't grow take a [const T]
/* /*
Function: grow_mut Function: grow_mut
@ -384,7 +384,7 @@ Function: map
Apply a function to each element of a vector and return the results Apply a function to each element of a vector and return the results
*/ */
fn map<T, U>(f: block(T) -> U, v: [mutable? T]) -> [U] { fn map<T, U>(f: block(T) -> U, v: [const T]) -> [U] {
let result = []; let result = [];
reserve(result, len(v)); reserve(result, len(v));
for elem: T in v { for elem: T in v {
@ -416,7 +416,7 @@ Apply a function to each element of a vector and return the results
If function `f` returns `none` then that element is excluded from If function `f` returns `none` then that element is excluded from
the resulting vector. the resulting vector.
*/ */
fn filter_map<T, U>(f: block(T) -> option::t<U>, v: [mutable? T]) -> [U] { fn filter_map<T, U>(f: block(T) -> option::t<U>, v: [const T]) -> [U] {
let result = []; let result = [];
for elem: T in v { for elem: T in v {
let elem2 = elem; // satisfies alias checker let elem2 = elem; // satisfies alias checker
@ -437,7 +437,7 @@ holds.
Apply function `f` to each element of `v` and return a vector containing Apply function `f` to each element of `v` and return a vector containing
only those elements for which `f` returned true. only those elements for which `f` returned true.
*/ */
fn filter<T>(f: block(T) -> bool, v: [mutable? T]) -> [T] { fn filter<T>(f: block(T) -> bool, v: [const T]) -> [T] {
let result = []; let result = [];
for elem: T in v { for elem: T in v {
let elem2 = elem; // satisfies alias checker let elem2 = elem; // satisfies alias checker
@ -454,7 +454,7 @@ Function: concat
Concatenate a vector of vectors. Flattens a vector of vectors of T into Concatenate a vector of vectors. Flattens a vector of vectors of T into
a single vector of T. a single vector of T.
*/ */
fn concat<T>(v: [mutable? [mutable? T]]) -> [T] { fn concat<T>(v: [const [const T]]) -> [T] {
// FIXME: So much copying // FIXME: So much copying
let new: [T] = []; let new: [T] = [];
for inner: [T] in v { new += inner; } for inner: [T] in v { new += inner; }
@ -466,7 +466,7 @@ Function: foldl
Reduce a vector from left to right Reduce a vector from left to right
*/ */
fn foldl<T, U>(p: block(T, U) -> T, z: T, v: [mutable? U]) -> T { fn foldl<T, U>(p: block(T, U) -> T, z: T, v: [const U]) -> T {
let accum = z; let accum = z;
iter(v) { |elt| iter(v) { |elt|
accum = p(accum, elt); accum = p(accum, elt);
@ -479,7 +479,7 @@ Function: foldr
Reduce a vector from right to left Reduce a vector from right to left
*/ */
fn foldr<T, U>(p: block(T, U) -> U, z: U, v: [mutable? T]) -> U { fn foldr<T, U>(p: block(T, U) -> U, z: U, v: [const T]) -> U {
let accum = z; let accum = z;
riter(v) { |elt| riter(v) { |elt|
accum = p(elt, accum); accum = p(elt, accum);
@ -526,7 +526,7 @@ Function: count
Returns the number of elements that are equal to a given value Returns the number of elements that are equal to a given value
*/ */
fn count<T>(x: T, v: [mutable? T]) -> uint { fn count<T>(x: T, v: [const T]) -> uint {
let cnt = 0u; let cnt = 0u;
for elt: T in v { if x == elt { cnt += 1u; } } for elt: T in v { if x == elt { cnt += 1u; } }
ret cnt; ret cnt;
@ -646,7 +646,7 @@ Function: reversed
Returns a vector with the order of elements reversed Returns a vector with the order of elements reversed
*/ */
fn reversed<T>(v: [mutable? T]) -> [T] { fn reversed<T>(v: [const T]) -> [T] {
let rs: [T] = []; let rs: [T] = [];
let i = len::<T>(v); let i = len::<T>(v);
if i == 0u { ret rs; } else { i -= 1u; } if i == 0u { ret rs; } else { i -= 1u; }
@ -690,7 +690,7 @@ Iterates over vector `v` and, for each element, calls function `f` with the
element's value. element's value.
*/ */
fn iter<T>(v: [mutable? T], f: block(T)) { fn iter<T>(v: [const T], f: block(T)) {
iter2(v) { |_i, v| f(v) } iter2(v) { |_i, v| f(v) }
} }
@ -702,7 +702,7 @@ Iterates over a vector's elements and indexes
Iterates over vector `v` and, for each element, calls function `f` with the Iterates over vector `v` and, for each element, calls function `f` with the
element's value and index. element's value and index.
*/ */
fn iter2<T>(v: [mutable? T], f: block(uint, T)) { fn iter2<T>(v: [const T], f: block(uint, T)) {
let i = 0u; let i = 0u;
for x in v { f(i, x); i += 1u; } for x in v { f(i, x); i += 1u; }
} }
@ -716,7 +716,7 @@ Iterates over vector `v` and, for each element, calls function `f` with the
element's value. element's value.
*/ */
fn riter<T>(v: [mutable? T], f: block(T)) { fn riter<T>(v: [const T], f: block(T)) {
riter2(v) { |_i, v| f(v) } riter2(v) { |_i, v| f(v) }
} }
@ -728,7 +728,7 @@ Iterates over a vector's elements and indexes in reverse
Iterates over vector `v` and, for each element, calls function `f` with the Iterates over vector `v` and, for each element, calls function `f` with the
element's value and index. element's value and index.
*/ */
fn riter2<T>(v: [mutable? T], f: block(uint, T)) { fn riter2<T>(v: [const T], f: block(uint, T)) {
let i = len(v); let i = len(v);
while 0u < i { while 0u < i {
i -= 1u; i -= 1u;
@ -746,7 +746,7 @@ is sorted then the permutations are lexicographically sorted).
The total number of permutations produced is `len(v)!`. If `v` contains The total number of permutations produced is `len(v)!`. If `v` contains
repeated elements, then some permutations are repeated. repeated elements, then some permutations are repeated.
*/ */
fn permute<T>(v: [mutable? T], put: block([T])) { fn permute<T>(v: [const T], put: block([T])) {
let ln = len(v); let ln = len(v);
if ln == 0u { if ln == 0u {
put([]); put([]);
@ -798,7 +798,7 @@ mod unsafe {
modifing its buffers, so it is up to the caller to ensure that modifing its buffers, so it is up to the caller to ensure that
the vector is actually the specified size. the vector is actually the specified size.
*/ */
unsafe fn set_len<T>(&v: [mutable? T], new_len: uint) { unsafe fn set_len<T>(&v: [const T], new_len: uint) {
let repr: **vec_repr = ::unsafe::reinterpret_cast(addr_of(v)); let repr: **vec_repr = ::unsafe::reinterpret_cast(addr_of(v));
(**repr).fill = new_len * sys::size_of::<T>(); (**repr).fill = new_len * sys::size_of::<T>();
} }
@ -814,7 +814,7 @@ mod unsafe {
Modifying the vector may cause its buffer to be reallocated, which Modifying the vector may cause its buffer to be reallocated, which
would also make any pointers to it invalid. would also make any pointers to it invalid.
*/ */
unsafe fn to_ptr<T>(v: [mutable? T]) -> *T { unsafe fn to_ptr<T>(v: [const T]) -> *T {
let repr: **vec_repr = ::unsafe::reinterpret_cast(addr_of(v)); let repr: **vec_repr = ::unsafe::reinterpret_cast(addr_of(v));
ret ::unsafe::reinterpret_cast(addr_of((**repr).data)); ret ::unsafe::reinterpret_cast(addr_of((**repr).data));
} }

View file

@ -1,7 +1,7 @@
// error-pattern: assigning to immutable box // error-pattern: assigning to immutable box
fn main() { fn main() {
fn f(&&v: @mutable? int) { fn f(&&v: @const int) {
// This shouldn't be possible // This shouldn't be possible
*v = 1 *v = 1
} }

View file

@ -1,7 +1,7 @@
// error-pattern: assigning to immutable field // error-pattern: assigning to immutable field
fn main() { fn main() {
fn f(&&v: {mutable? field: int}) { fn f(&&v: {const field: int}) {
// This shouldn't be possible // This shouldn't be possible
v.field = 1 v.field = 1
} }

View file

@ -3,7 +3,7 @@
use std; use std;
fn main() { fn main() {
unsafe fn f(&&v: *mutable? int) { unsafe fn f(&&v: *const int) {
// This shouldn't be possible // This shouldn't be possible
*v = 1 *v = 1
} }

View file

@ -1,7 +1,7 @@
// error-pattern: assigning to immutable box // error-pattern: assigning to immutable box
fn main() { fn main() {
fn f(&&v: ~mutable? int) { fn f(&&v: ~const int) {
// This shouldn't be possible // This shouldn't be possible
*v = 1 *v = 1
} }

View file

@ -3,7 +3,7 @@
fn main() { fn main() {
let v = @mutable [0]; let v = @mutable [0];
fn f(&&v: @mutable [mutable? int]) { fn f(&&v: @mutable [const int]) {
*v = [mutable 3] *v = [mutable 3]
} }

View file

@ -3,7 +3,7 @@
fn main() { fn main() {
let v = [mutable @mutable ~mutable [0]]; let v = [mutable @mutable ~mutable [0]];
fn f(&&v: [mutable @mutable ~mutable [mutable? int]]) { fn f(&&v: [mutable @mutable ~mutable [const int]]) {
} }
f(v); f(v);

View file

@ -6,7 +6,7 @@ fn main() {
let a = [0]; let a = [0];
let v: *mutable [int] = std::ptr::mut_addr_of(a); let v: *mutable [int] = std::ptr::mut_addr_of(a);
fn f(&&v: *mutable [mutable? int]) { fn f(&&v: *mutable [const int]) {
unsafe { unsafe {
*v = [mutable 3] *v = [mutable 3]
} }

View file

@ -3,7 +3,7 @@
fn main() { fn main() {
let v = {mutable g: [0]}; let v = {mutable g: [0]};
fn f(&&v: {mutable g: [mutable? int]}) { fn f(&&v: {mutable g: [const int]}) {
v.g = [mutable 3] v.g = [mutable 3]
} }

View file

@ -3,7 +3,7 @@
fn main() { fn main() {
let v = ~mutable [0]; let v = ~mutable [0];
fn f(&&v: ~mutable [mutable? int]) { fn f(&&v: ~mutable [const int]) {
*v = [mutable 3] *v = [mutable 3]
} }

View file

@ -3,7 +3,7 @@
fn main() { fn main() {
let v = [mutable [0]]; let v = [mutable [0]];
fn f(&&v: [mutable [mutable? int]]) { fn f(&&v: [mutable [const int]]) {
v[0] = [mutable 3] v[0] = [mutable 3]
} }

View file

@ -3,7 +3,7 @@
fn main() { fn main() {
let v = [mutable [mutable 0]]; let v = [mutable [mutable 0]];
fn f(&&v: [mutable [mutable? int]]) { fn f(&&v: [mutable [const int]]) {
v[0] = [3] v[0] = [3]
} }

View file

@ -3,7 +3,7 @@
fn main() { fn main() {
let v = [mutable [mutable [0]]]; let v = [mutable [mutable [0]]];
fn f(&&v: [mutable [mutable [mutable? int]]]) { fn f(&&v: [mutable [mutable [const int]]]) {
v[0][1] = [mutable 3] v[0][1] = [mutable 3]
} }

View file

@ -1,7 +1,7 @@
// error-pattern: assigning to immutable vec content // error-pattern: assigning to immutable vec content
fn main() { fn main() {
fn f(&&v: [mutable? int]) { fn f(&&v: [const int]) {
// This shouldn't be possible // This shouldn't be possible
v[0] = 1 v[0] = 1
} }

View file

@ -1,5 +1,5 @@
fn push<T>(&v: [mutable? T], t: T) { v += [t]; } fn push<T>(&v: [const T], t: T) { v += [t]; }
fn main() { let v = [1, 2, 3]; push(v, 1); } fn main() { let v = [1, 2, 3]; push(v, 1); }