libsyntax: Forbid mutable vectors. rs=demuting

This commit is contained in:
Patrick Walton 2013-02-26 14:50:09 -08:00
parent 8d7e6ef772
commit 573a31dfa7
9 changed files with 31 additions and 61 deletions

View File

@ -133,18 +133,6 @@ impl<A> DVec<A> {
self.check_out(|v| self.give_back(f(v)))
}
/**
* Swaps out the current vector and hands it off to a user-provided
* function `f`. The function should transform it however is desired
* and return a new vector to replace it with.
*/
#[inline(always)]
fn swap_mut(f: &fn(v: ~[mut A]) -> ~[mut A]) {
do self.swap |v| {
vec::cast_from_mut(f(vec::cast_to_mut(v)))
}
}
/// Returns the number of elements currently in the dvec
#[inline(always)]
pure fn len() -> uint {
@ -217,7 +205,7 @@ impl<A> DVec<A> {
}
/// Gives access to the vector as a slice with mutable contents
fn borrow_mut<R>(op: fn(x: &[mut A]) -> R) -> R {
fn borrow_mut<R>(op: &fn(x: &mut [A]) -> R) -> R {
do self.check_out |v| {
let mut v = v;
let result = op(v);

View File

@ -109,7 +109,7 @@ pub mod win32 {
let mut done = false;
while !done {
let mut k: DWORD = 0;
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| {
k = f(b, TMPBUF_SZ as DWORD);
if k == (0 as DWORD) {

View File

@ -209,16 +209,6 @@ pub pure fn build_sized_opt<A>(size: Option<uint>,
build_sized(size.get_or_default(4), builder)
}
/// Produces a mut vector from an immutable vector.
pub pure fn cast_to_mut<T>(v: ~[T]) -> ~[mut T] {
unsafe { ::cast::transmute(v) }
}
/// Produces an immutable vector from a mut vector.
pub pure fn cast_from_mut<T>(v: ~[mut T]) -> ~[T] {
unsafe { ::cast::transmute(v) }
}
// Accessors
/// Returns the first element of a vector
@ -274,9 +264,10 @@ pub pure fn slice<T>(v: &r/[T], start: uint, end: uint) -> &r/[T] {
/// Return a slice that points into another slice.
#[inline(always)]
pub pure fn mut_slice<T>(v: &r/[mut T], start: uint,
end: uint) -> &r/[mut T] {
pub pure fn mut_slice<T>(v: &r/mut [T],
start: uint,
end: uint)
-> &r/mut [T] {
assert (start <= end);
assert (end <= len(v));
do as_mut_buf(v) |p, _len| {
@ -290,8 +281,10 @@ pub pure fn mut_slice<T>(v: &r/[mut T], start: uint,
/// Return a slice that points into another slice.
#[inline(always)]
pub pure fn const_slice<T>(v: &r/[const T], start: uint,
end: uint) -> &r/[const T] {
pub pure fn const_slice<T>(v: &r/[const T],
start: uint,
end: uint)
-> &r/[const T] {
assert (start <= end);
assert (end <= len(v));
do as_const_buf(v) |p, _len| {
@ -3337,28 +3330,6 @@ mod tests {
let _x = windowed (0u, ~[1u,2u,3u,4u,5u,6u]);
}
#[test]
fn cast_to_mut_no_copy() {
unsafe {
let x = ~[1, 2, 3];
let addr = raw::to_ptr(x);
let x_mut = cast_to_mut(x);
let addr_mut = raw::to_ptr(x_mut);
assert addr == addr_mut;
}
}
#[test]
fn cast_from_mut_no_copy() {
unsafe {
let x = ~[mut 1, 2, 3];
let addr = raw::to_ptr(x);
let x_imm = cast_from_mut(x);
let addr_imm = raw::to_ptr(x_imm);
assert addr == addr_imm;
}
}
#[test]
fn test_unshift() {
let mut x = ~[1, 2, 3];

View File

@ -455,8 +455,7 @@ impl<T:Copy + Ord> MergeState<T> {
base2: uint, len2: uint) {
assert len1 != 0 && len2 != 0 && base1+len1 == base2;
let tmp = vec::cast_to_mut(
vec::slice(array, base1, base1+len1).to_vec());
let mut tmp = vec::slice(array, base1, base1+len1).to_vec();
let mut c1 = 0;
let mut c2 = base2;
@ -559,8 +558,7 @@ impl<T:Copy + Ord> MergeState<T> {
base2: uint, len2: uint) {
assert len1 != 1 && len2 != 0 && base1 + len1 == base2;
let tmp = vec::cast_to_mut(
vec::slice(array, base2, base2+len2).to_vec());
let mut tmp = vec::slice(array, base2, base2+len2).to_vec();
let mut c1 = base1 + len1 - 1;
let mut c2 = len2 - 1;

View File

@ -52,7 +52,7 @@ impl Stats for &[f64] {
fn median(self) -> f64 {
assert self.len() != 0;
let tmp = vec::cast_to_mut(vec::from_slice(self));
let mut tmp = vec::from_slice(self);
sort::tim_sort(tmp);
if tmp.len() & 1 == 0 {
let m = tmp.len() / 2;

View File

@ -377,7 +377,7 @@ pub fn run_tests_console(opts: &TestOpts,
fn print_failures(st: @ConsoleTestState) {
st.out.write_line(~"\nfailures:");
let failures = vec::cast_to_mut(st.failures.map(|t| t.name.to_str()));
let mut failures = st.failures.map(|t| t.name.to_str());
sort::tim_sort(failures);
for vec::each(failures) |name| {
st.out.write_line(fmt!(" %s", name.to_str()));

View File

@ -49,6 +49,7 @@ pub enum ObsoleteSyntax {
ObsoleteImplSyntax,
ObsoleteTraitBoundSeparator,
ObsoleteMutOwnedPointer,
ObsoleteMutVector,
}
pub impl to_bytes::IterBytes for ObsoleteSyntax {
@ -133,6 +134,12 @@ pub impl Parser {
in a mutable location, like a mutable local variable or an \
`@mut` box"
),
ObsoleteMutVector => (
"const or mutable vector",
"mutability inherits through `~` pointers; place the vector \
in a mutable location, like a mutable local variable or an \
`@mut` box"
),
};
self.report(sp, kind, kind_str, desc);

View File

@ -76,6 +76,7 @@ use parse::obsolete::{ObsoleteStructCtor, ObsoleteWith};
use parse::obsolete::{ObsoleteSyntax, ObsoleteLowerCaseKindBounds};
use parse::obsolete::{ObsoleteUnsafeBlock, ObsoleteImplSyntax};
use parse::obsolete::{ObsoleteTraitBoundSeparator, ObsoleteMutOwnedPointer};
use parse::obsolete::{ObsoleteMutVector};
use parse::prec::{as_prec, token_to_binop};
use parse::token::{can_begin_expr, is_ident, is_ident_or_path};
use parse::token::{is_plain_ident, INTERPOLATED, special_idents};
@ -624,6 +625,9 @@ pub impl Parser {
} else if *self.token == token::LBRACKET {
self.expect(token::LBRACKET);
let mt = self.parse_mt();
if mt.mutbl == m_mutbl { // `m_const` too after snapshot
self.obsolete(*self.last_span, ObsoleteMutVector);
}
// Parse the `* 3` in `[ int * 3 ]`
let t = match self.maybe_parse_fixed_vstore_with_star() {
@ -1134,6 +1138,10 @@ pub impl Parser {
} else if *self.token == token::LBRACKET {
self.bump();
let mutbl = self.parse_mutability();
if mutbl == m_mutbl { // `m_const` too after snapshot
self.obsolete(*self.last_span, ObsoleteMutVector);
}
if *self.token == token::RBRACKET {
// Empty vector.
self.bump();

View File

@ -9,10 +9,8 @@
// except according to those terms.
pub fn main() {
// Once cast_to_mut is removed, pick a better function to import
// for this test!
use vec::cast_to_mut;
log(debug, vec::len(cast_to_mut(~[1, 2])));
use vec::from_fn;
log(debug, vec::len(from_fn(2, |i| i)));
{
use vec::*;
log(debug, len(~[2]));