From b29c36867418ea551b23c767f45454eea4623d79 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 23 Jun 2013 20:44:11 -0700 Subject: [PATCH 1/2] Removing a lot of usage of '&const' --- src/libextra/arc.rs | 4 +- src/libextra/bitv.rs | 4 +- src/libextra/deque.rs | 4 +- src/libextra/flate.rs | 8 +-- src/libextra/treemap.rs | 8 +-- src/librustc/middle/lang_items.rs | 76 +++++++++++----------- src/librustc/middle/ty.rs | 4 +- src/librustc/middle/typeck/check/vtable.rs | 2 +- src/libstd/at_vec.rs | 6 +- src/libstd/hashmap.rs | 8 +-- src/libstd/io.rs | 6 +- src/libstd/option.rs | 4 +- src/libstd/ptr.rs | 18 ++--- src/libstd/str.rs | 8 +-- src/libstd/task/spawn.rs | 2 +- src/libstd/trie.rs | 8 +-- src/libstd/vec.rs | 72 ++++++-------------- 17 files changed, 105 insertions(+), 137 deletions(-) diff --git a/src/libextra/arc.rs b/src/libextra/arc.rs index 30067c92300c..13d159d8fed3 100644 --- a/src/libextra/arc.rs +++ b/src/libextra/arc.rs @@ -436,8 +436,8 @@ impl RWARC { // lock it. This wraps the unsafety, with the justification that the 'lock' // field is never overwritten; only 'failed' and 'data'. #[doc(hidden)] -fn borrow_rwlock(state: *const RWARCInner) -> *RWlock { - unsafe { cast::transmute(&const (*state).lock) } +fn borrow_rwlock(state: *mut RWARCInner) -> *RWlock { + unsafe { cast::transmute(&(*state).lock) } } /// The "write permission" token used for RWARC.write_downgrade(). diff --git a/src/libextra/bitv.rs b/src/libextra/bitv.rs index 4fe7761bf18d..92ee3fb43c68 100644 --- a/src/libextra/bitv.rs +++ b/src/libextra/bitv.rs @@ -705,8 +705,8 @@ impl cmp::Eq for BitvSet { } impl Container for BitvSet { - fn len(&const self) -> uint { self.size } - fn is_empty(&const self) -> bool { self.size == 0 } + fn len(&self) -> uint { self.size } + fn is_empty(&self) -> bool { self.size == 0 } } impl Mutable for BitvSet { diff --git a/src/libextra/deque.rs b/src/libextra/deque.rs index c70c87b6ea13..cf7b188cb1d9 100644 --- a/src/libextra/deque.rs +++ b/src/libextra/deque.rs @@ -28,10 +28,10 @@ pub struct Deque { impl Container for Deque { /// Return the number of elements in the deque - fn len(&const self) -> uint { self.nelts } + fn len(&self) -> uint { self.nelts } /// Return true if the deque contains no elements - fn is_empty(&const self) -> bool { self.len() == 0 } + fn is_empty(&self) -> bool { self.len() == 0 } } impl Mutable for Deque { diff --git a/src/libextra/flate.rs b/src/libextra/flate.rs index 0fde03b69cbf..fe8f4bee75e7 100644 --- a/src/libextra/flate.rs +++ b/src/libextra/flate.rs @@ -44,8 +44,8 @@ static lz_fast : c_int = 0x1; // LZ with only one probe static lz_norm : c_int = 0x80; // LZ with 128 probes, "normal" static lz_best : c_int = 0xfff; // LZ with 4095 probes, "best" -pub fn deflate_bytes(bytes: &const [u8]) -> ~[u8] { - do vec::as_const_buf(bytes) |b, len| { +pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] { + do vec::as_imm_buf(bytes) |b, len| { unsafe { let mut outsz : size_t = 0; let res = @@ -62,8 +62,8 @@ pub fn deflate_bytes(bytes: &const [u8]) -> ~[u8] { } } -pub fn inflate_bytes(bytes: &const [u8]) -> ~[u8] { - do vec::as_const_buf(bytes) |b, len| { +pub fn inflate_bytes(bytes: &[u8]) -> ~[u8] { + do vec::as_imm_buf(bytes) |b, len| { unsafe { let mut outsz : size_t = 0; let res = diff --git a/src/libextra/treemap.rs b/src/libextra/treemap.rs index d546b48f8175..fcee3a2b7968 100644 --- a/src/libextra/treemap.rs +++ b/src/libextra/treemap.rs @@ -87,10 +87,10 @@ impl Ord for TreeMap { impl Container for TreeMap { /// Return the number of elements in the map - fn len(&const self) -> uint { self.length } + fn len(&self) -> uint { self.length } /// Return true if the map contains no elements - fn is_empty(&const self) -> bool { self.root.is_none() } + fn is_empty(&self) -> bool { self.root.is_none() } } impl Mutable for TreeMap { @@ -265,11 +265,11 @@ impl Ord for TreeSet { impl Container for TreeSet { /// Return the number of elements in the set #[inline] - fn len(&const self) -> uint { self.map.len() } + fn len(&self) -> uint { self.map.len() } /// Return true if the set contains no elements #[inline] - fn is_empty(&const self) -> bool { self.map.is_empty() } + fn is_empty(&self) -> bool { self.map.is_empty() } } impl Mutable for TreeSet { diff --git a/src/librustc/middle/lang_items.rs b/src/librustc/middle/lang_items.rs index 08e55df5b368..b167a22992cf 100644 --- a/src/librustc/middle/lang_items.rs +++ b/src/librustc/middle/lang_items.rs @@ -152,122 +152,122 @@ impl LanguageItems { // FIXME #4621: Method macros sure would be nice here. - pub fn freeze_trait(&const self) -> def_id { + pub fn freeze_trait(&self) -> def_id { self.items[FreezeTraitLangItem as uint].get() } - pub fn copy_trait(&const self) -> def_id { + pub fn copy_trait(&self) -> def_id { self.items[CopyTraitLangItem as uint].get() } - pub fn send_trait(&const self) -> def_id { + pub fn send_trait(&self) -> def_id { self.items[SendTraitLangItem as uint].get() } - pub fn sized_trait(&const self) -> def_id { + pub fn sized_trait(&self) -> def_id { self.items[SizedTraitLangItem as uint].get() } - pub fn drop_trait(&const self) -> def_id { + pub fn drop_trait(&self) -> def_id { self.items[DropTraitLangItem as uint].get() } - pub fn add_trait(&const self) -> def_id { + pub fn add_trait(&self) -> def_id { self.items[AddTraitLangItem as uint].get() } - pub fn sub_trait(&const self) -> def_id { + pub fn sub_trait(&self) -> def_id { self.items[SubTraitLangItem as uint].get() } - pub fn mul_trait(&const self) -> def_id { + pub fn mul_trait(&self) -> def_id { self.items[MulTraitLangItem as uint].get() } - pub fn div_trait(&const self) -> def_id { + pub fn div_trait(&self) -> def_id { self.items[DivTraitLangItem as uint].get() } - pub fn rem_trait(&const self) -> def_id { + pub fn rem_trait(&self) -> def_id { self.items[RemTraitLangItem as uint].get() } - pub fn neg_trait(&const self) -> def_id { + pub fn neg_trait(&self) -> def_id { self.items[NegTraitLangItem as uint].get() } - pub fn not_trait(&const self) -> def_id { + pub fn not_trait(&self) -> def_id { self.items[NotTraitLangItem as uint].get() } - pub fn bitxor_trait(&const self) -> def_id { + pub fn bitxor_trait(&self) -> def_id { self.items[BitXorTraitLangItem as uint].get() } - pub fn bitand_trait(&const self) -> def_id { + pub fn bitand_trait(&self) -> def_id { self.items[BitAndTraitLangItem as uint].get() } - pub fn bitor_trait(&const self) -> def_id { + pub fn bitor_trait(&self) -> def_id { self.items[BitOrTraitLangItem as uint].get() } - pub fn shl_trait(&const self) -> def_id { + pub fn shl_trait(&self) -> def_id { self.items[ShlTraitLangItem as uint].get() } - pub fn shr_trait(&const self) -> def_id { + pub fn shr_trait(&self) -> def_id { self.items[ShrTraitLangItem as uint].get() } - pub fn index_trait(&const self) -> def_id { + pub fn index_trait(&self) -> def_id { self.items[IndexTraitLangItem as uint].get() } - pub fn eq_trait(&const self) -> def_id { + pub fn eq_trait(&self) -> def_id { self.items[EqTraitLangItem as uint].get() } - pub fn ord_trait(&const self) -> def_id { + pub fn ord_trait(&self) -> def_id { self.items[OrdTraitLangItem as uint].get() } - pub fn str_eq_fn(&const self) -> def_id { + pub fn str_eq_fn(&self) -> def_id { self.items[StrEqFnLangItem as uint].get() } - pub fn uniq_str_eq_fn(&const self) -> def_id { + pub fn uniq_str_eq_fn(&self) -> def_id { self.items[UniqStrEqFnLangItem as uint].get() } - pub fn annihilate_fn(&const self) -> def_id { + pub fn annihilate_fn(&self) -> def_id { self.items[AnnihilateFnLangItem as uint].get() } - pub fn log_type_fn(&const self) -> def_id { + pub fn log_type_fn(&self) -> def_id { self.items[LogTypeFnLangItem as uint].get() } - pub fn fail_fn(&const self) -> def_id { + pub fn fail_fn(&self) -> def_id { self.items[FailFnLangItem as uint].get() } - pub fn fail_bounds_check_fn(&const self) -> def_id { + pub fn fail_bounds_check_fn(&self) -> def_id { self.items[FailBoundsCheckFnLangItem as uint].get() } - pub fn exchange_malloc_fn(&const self) -> def_id { + pub fn exchange_malloc_fn(&self) -> def_id { self.items[ExchangeMallocFnLangItem as uint].get() } - pub fn exchange_free_fn(&const self) -> def_id { + pub fn exchange_free_fn(&self) -> def_id { self.items[ExchangeFreeFnLangItem as uint].get() } - pub fn malloc_fn(&const self) -> def_id { + pub fn malloc_fn(&self) -> def_id { self.items[MallocFnLangItem as uint].get() } - pub fn free_fn(&const self) -> def_id { + pub fn free_fn(&self) -> def_id { self.items[FreeFnLangItem as uint].get() } - pub fn borrow_as_imm_fn(&const self) -> def_id { + pub fn borrow_as_imm_fn(&self) -> def_id { self.items[BorrowAsImmFnLangItem as uint].get() } - pub fn borrow_as_mut_fn(&const self) -> def_id { + pub fn borrow_as_mut_fn(&self) -> def_id { self.items[BorrowAsMutFnLangItem as uint].get() } - pub fn return_to_mut_fn(&const self) -> def_id { + pub fn return_to_mut_fn(&self) -> def_id { self.items[ReturnToMutFnLangItem as uint].get() } - pub fn check_not_borrowed_fn(&const self) -> def_id { + pub fn check_not_borrowed_fn(&self) -> def_id { self.items[CheckNotBorrowedFnLangItem as uint].get() } - pub fn strdup_uniq_fn(&const self) -> def_id { + pub fn strdup_uniq_fn(&self) -> def_id { self.items[StrDupUniqFnLangItem as uint].get() } - pub fn record_borrow_fn(&const self) -> def_id { + pub fn record_borrow_fn(&self) -> def_id { self.items[RecordBorrowFnLangItem as uint].get() } - pub fn unrecord_borrow_fn(&const self) -> def_id { + pub fn unrecord_borrow_fn(&self) -> def_id { self.items[UnrecordBorrowFnLangItem as uint].get() } - pub fn start_fn(&const self) -> def_id { + pub fn start_fn(&self) -> def_id { self.items[StartFnLangItem as uint].get() } pub fn ty_desc(&const self) -> def_id { diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index b8ee1eee26e1..48e841353c08 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -3696,14 +3696,14 @@ pub enum DtorKind { } impl DtorKind { - pub fn is_not_present(&const self) -> bool { + pub fn is_not_present(&self) -> bool { match *self { NoDtor => true, _ => false } } - pub fn is_present(&const self) -> bool { + pub fn is_present(&self) -> bool { !self.is_not_present() } diff --git a/src/librustc/middle/typeck/check/vtable.rs b/src/librustc/middle/typeck/check/vtable.rs index 0bf20f9fbcb9..a46d2f28b139 100644 --- a/src/librustc/middle/typeck/check/vtable.rs +++ b/src/librustc/middle/typeck/check/vtable.rs @@ -64,7 +64,7 @@ pub struct VtableContext { } impl VtableContext { - pub fn tcx(&const self) -> ty::ctxt { self.ccx.tcx } + pub fn tcx(&self) -> ty::ctxt { self.ccx.tcx } } fn has_trait_bounds(type_param_defs: &[ty::TypeParameterDef]) -> bool { diff --git a/src/libstd/at_vec.rs b/src/libstd/at_vec.rs index 5a2f948600a8..325ce097cd5a 100644 --- a/src/libstd/at_vec.rs +++ b/src/libstd/at_vec.rs @@ -108,7 +108,7 @@ pub fn build_sized_opt(size: Option, /// Iterates over the `rhs` vector, copying each element and appending it to the /// `lhs`. Afterwards, the `lhs` is then returned for use again. #[inline] -pub fn append(lhs: @[T], rhs: &const [T]) -> @[T] { +pub fn append(lhs: @[T], rhs: &[T]) -> @[T] { do build_sized(lhs.len() + rhs.len()) |push| { for lhs.iter().advance |x| { push(copy *x); } for uint::range(0, rhs.len()) |i| { push(copy rhs[i]); } @@ -180,9 +180,9 @@ pub mod traits { use kinds::Copy; use ops::Add; - impl<'self,T:Copy> Add<&'self const [T],@[T]> for @[T] { + impl<'self,T:Copy> Add<&'self [T],@[T]> for @[T] { #[inline] - fn add(&self, rhs: & &'self const [T]) -> @[T] { + fn add(&self, rhs: & &'self [T]) -> @[T] { append(*self, (*rhs)) } } diff --git a/src/libstd/hashmap.rs b/src/libstd/hashmap.rs index 7f9fb6ad9380..35db229b65d3 100644 --- a/src/libstd/hashmap.rs +++ b/src/libstd/hashmap.rs @@ -282,10 +282,10 @@ impl HashMap { impl Container for HashMap { /// Return the number of elements in the map - fn len(&const self) -> uint { self.size } + fn len(&self) -> uint { self.size } /// Return true if the map contains no elements - fn is_empty(&const self) -> bool { self.len() == 0 } + fn is_empty(&self) -> bool { self.len() == 0 } } impl Mutable for HashMap { @@ -623,10 +623,10 @@ impl Eq for HashSet { impl Container for HashSet { /// Return the number of elements in the set - fn len(&const self) -> uint { self.map.len() } + fn len(&self) -> uint { self.map.len() } /// Return true if the set contains no elements - fn is_empty(&const self) -> bool { self.map.is_empty() } + fn is_empty(&self) -> bool { self.map.is_empty() } } impl Mutable for HashSet { diff --git a/src/libstd/io.rs b/src/libstd/io.rs index 59ac58a514f3..40793ff1af7b 100644 --- a/src/libstd/io.rs +++ b/src/libstd/io.rs @@ -1152,7 +1152,7 @@ impl Writer for Wrapper { impl Writer for *libc::FILE { fn write(&self, v: &[u8]) { unsafe { - do vec::as_const_buf(v) |vbuf, len| { + do vec::as_imm_buf(v) |vbuf, len| { let nout = libc::fwrite(vbuf as *c_void, 1, len as size_t, @@ -1203,9 +1203,9 @@ impl Writer for fd_t { fn write(&self, v: &[u8]) { unsafe { let mut count = 0u; - do vec::as_const_buf(v) |vbuf, len| { + do vec::as_imm_buf(v) |vbuf, len| { while count < len { - let vb = ptr::const_offset(vbuf, count) as *c_void; + let vb = ptr::offset(vbuf, count) as *c_void; let nout = libc::write(*self, vb, len as size_t); if nout < 0 as ssize_t { error!("error writing buffer"); diff --git a/src/libstd/option.rs b/src/libstd/option.rs index 12ae2abd4a1f..643812312582 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -121,13 +121,13 @@ impl Option { /// Returns true if the option equals `none` #[inline] - pub fn is_none(&const self) -> bool { + pub fn is_none(&self) -> bool { match *self { None => true, Some(_) => false } } /// Returns true if the option contains some value #[inline] - pub fn is_some(&const self) -> bool { !self.is_none() } + pub fn is_some(&self) -> bool { !self.is_none() } /// Update an optional value by optionally running its content through a /// function that returns an option. diff --git a/src/libstd/ptr.rs b/src/libstd/ptr.rs index 7f89d454be11..473f56ddd798 100644 --- a/src/libstd/ptr.rs +++ b/src/libstd/ptr.rs @@ -232,9 +232,9 @@ pub unsafe fn array_each(arr: **T, cb: &fn(*T)) { #[allow(missing_doc)] pub trait RawPtr { - fn is_null(&const self) -> bool; - fn is_not_null(&const self) -> bool; - unsafe fn to_option(&const self) -> Option<&T>; + fn is_null(&self) -> bool; + fn is_not_null(&self) -> bool; + unsafe fn to_option(&self) -> Option<&T>; fn offset(&self, count: uint) -> Self; } @@ -242,11 +242,11 @@ pub trait RawPtr { impl RawPtr for *T { /// Returns true if the pointer is equal to the null pointer. #[inline] - fn is_null(&const self) -> bool { is_null(*self) } + fn is_null(&self) -> bool { is_null(*self) } /// Returns true if the pointer is not equal to the null pointer. #[inline] - fn is_not_null(&const self) -> bool { is_not_null(*self) } + fn is_not_null(&self) -> bool { is_not_null(*self) } /// /// Returns `None` if the pointer is null, or else returns the value wrapped @@ -259,7 +259,7 @@ impl RawPtr for *T { /// be pointing to invalid memory. /// #[inline] - unsafe fn to_option(&const self) -> Option<&T> { + unsafe fn to_option(&self) -> Option<&T> { if self.is_null() { None } else { Some(cast::transmute(*self)) } @@ -274,11 +274,11 @@ impl RawPtr for *T { impl RawPtr for *mut T { /// Returns true if the pointer is equal to the null pointer. #[inline] - fn is_null(&const self) -> bool { is_null(*self) } + fn is_null(&self) -> bool { is_null(*self) } /// Returns true if the pointer is not equal to the null pointer. #[inline] - fn is_not_null(&const self) -> bool { is_not_null(*self) } + fn is_not_null(&self) -> bool { is_not_null(*self) } /// /// Returns `None` if the pointer is null, or else returns the value wrapped @@ -291,7 +291,7 @@ impl RawPtr for *mut T { /// be pointing to invalid memory. /// #[inline] - unsafe fn to_option(&const self) -> Option<&T> { + unsafe fn to_option(&self) -> Option<&T> { if self.is_null() { None } else { Some(cast::transmute(*self)) } diff --git a/src/libstd/str.rs b/src/libstd/str.rs index e47800d70c6b..a06d858e4243 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -569,7 +569,7 @@ Section: Misc */ /// Determines if a vector of bytes contains valid UTF-8 -pub fn is_utf8(v: &const [u8]) -> bool { +pub fn is_utf8(v: &[u8]) -> bool { let mut i = 0u; let total = v.len(); while i < total { @@ -815,7 +815,7 @@ pub mod raw { } /// Create a Rust string from a *u8 buffer of the given length - pub unsafe fn from_buf_len(buf: *const u8, len: uint) -> ~str { + pub unsafe fn from_buf_len(buf: *u8, len: uint) -> ~str { let mut v: ~[u8] = vec::with_capacity(len + 1); vec::as_mut_buf(v, |vbuf, _len| { ptr::copy_memory(vbuf, buf as *u8, len) @@ -838,8 +838,8 @@ pub mod raw { } /// Converts a vector of bytes to a new owned string. - pub unsafe fn from_bytes(v: &const [u8]) -> ~str { - do vec::as_const_buf(v) |buf, len| { + pub unsafe fn from_bytes(v: &[u8]) -> ~str { + do vec::as_imm_buf(v) |buf, len| { from_buf_len(buf, len) } } diff --git a/src/libstd/task/spawn.rs b/src/libstd/task/spawn.rs index c932a9660c2f..190485a720aa 100644 --- a/src/libstd/task/spawn.rs +++ b/src/libstd/task/spawn.rs @@ -129,7 +129,7 @@ type TaskGroupInner<'self> = &'self mut Option; // A taskgroup is 'dead' when nothing can cause it to fail; only members can. fn taskgroup_is_dead(tg: &TaskGroupData) -> bool { - (&const tg.members).is_empty() + tg.members.is_empty() } // A list-like structure by which taskgroups keep track of all ancestor groups diff --git a/src/libstd/trie.rs b/src/libstd/trie.rs index b9b03ea56619..8ce02d59ab15 100644 --- a/src/libstd/trie.rs +++ b/src/libstd/trie.rs @@ -35,11 +35,11 @@ pub struct TrieMap { impl Container for TrieMap { /// Return the number of elements in the map #[inline] - fn len(&const self) -> uint { self.length } + fn len(&self) -> uint { self.length } /// Return true if the map contains no elements #[inline] - fn is_empty(&const self) -> bool { self.len() == 0 } + fn is_empty(&self) -> bool { self.len() == 0 } } impl Mutable for TrieMap { @@ -179,11 +179,11 @@ pub struct TrieSet { impl Container for TrieSet { /// Return the number of elements in the set #[inline] - fn len(&const self) -> uint { self.map.len() } + fn len(&self) -> uint { self.map.len() } /// Return true if the set contains no elements #[inline] - fn is_empty(&const self) -> bool { self.map.is_empty() } + fn is_empty(&self) -> bool { self.map.is_empty() } } impl Mutable for TrieSet { diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 4e7943f7cfd2..4196fbac0beb 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -64,7 +64,7 @@ pub mod rustrt { } /// Returns true if two vectors have the same length -pub fn same_length(xs: &const [T], ys: &const [U]) -> bool { +pub fn same_length(xs: &[T], ys: &[U]) -> bool { xs.len() == ys.len() } @@ -350,10 +350,7 @@ pub fn dedup(v: &mut ~[T]) { if v.len() < 1 { return; } let mut last_written = 0; let mut next_to_read = 1; - do as_const_buf(*v) |p, ln| { - // We have a mutable reference to v, so we can make arbitrary - // changes. (cf. push and pop) - let p = p as *mut T; + do as_mut_buf(*v) |p, ln| { // last_written < next_to_read <= ln while next_to_read < ln { // last_written < next_to_read < ln @@ -384,7 +381,7 @@ pub fn dedup(v: &mut ~[T]) { /// Iterates over the `rhs` vector, copying each element and appending it to the /// `lhs`. Afterwards, the `lhs` is then returned for use again. #[inline] -pub fn append(lhs: ~[T], rhs: &const [T]) -> ~[T] { +pub fn append(lhs: ~[T], rhs: &[T]) -> ~[T] { let mut v = lhs; v.push_all(rhs); v @@ -831,7 +828,7 @@ pub fn unzip(v: ~[(T, U)]) -> (~[T], ~[U]) { /** * Convert two vectors to a vector of pairs, by reference. As zip(). */ -pub fn zip_slice(v: &const [T], u: &const [U]) +pub fn zip_slice(v: &[T], u: &[U]) -> ~[(T, U)] { let mut zipped = ~[]; let sz = v.len(); @@ -893,7 +890,7 @@ pub fn reverse(v: &mut [T]) { } /// Returns a vector with the order of elements reversed -pub fn reversed(v: &const [T]) -> ~[T] { +pub fn reversed(v: &[T]) -> ~[T] { let mut rs: ~[T] = ~[]; let mut i = v.len(); if i == 0 { return (rs); } else { i -= 1; } @@ -1003,16 +1000,6 @@ pub fn as_imm_buf(s: &[T], } } -/// Similar to `as_imm_buf` but passing a `*const T` -#[inline] -pub fn as_const_buf(s: &const [T], f: &fn(*const T, uint) -> U) -> U { - unsafe { - let v : *(*const T,uint) = transmute(&s); - let (buf,len) = *v; - f(buf, len / sys::nonzero_size_of::()) - } -} - /// Similar to `as_imm_buf` but passing a `*mut T` #[inline] pub fn as_mut_buf(s: &mut [T], f: &fn(*mut T, uint) -> U) -> U { @@ -1198,25 +1185,25 @@ pub mod traits { use ops::Add; use vec::append; - impl<'self,T:Copy> Add<&'self const [T],~[T]> for ~[T] { + impl<'self,T:Copy> Add<&'self [T],~[T]> for ~[T] { #[inline] - fn add(&self, rhs: & &'self const [T]) -> ~[T] { + fn add(&self, rhs: & &'self [T]) -> ~[T] { append(copy *self, (*rhs)) } } } -impl<'self, T> Container for &'self const [T] { +impl<'self, T> Container for &'self [T] { /// Returns true if a vector contains no elements #[inline] fn is_empty(&self) -> bool { - as_const_buf(*self, |_p, len| len == 0u) + as_imm_buf(*self, |_p, len| len == 0u) } /// Returns the length of a vector #[inline] fn len(&self) -> uint { - as_const_buf(*self, |_p, len| len) + as_imm_buf(*self, |_p, len| len) } } @@ -1224,13 +1211,13 @@ impl Container for ~[T] { /// Returns true if a vector contains no elements #[inline] fn is_empty(&self) -> bool { - as_const_buf(*self, |_p, len| len == 0u) + as_imm_buf(*self, |_p, len| len == 0u) } /// Returns the length of a vector #[inline] fn len(&self) -> uint { - as_const_buf(*self, |_p, len| len) + as_imm_buf(*self, |_p, len| len) } } @@ -1843,7 +1830,7 @@ impl Mutable for ~[T] { #[allow(missing_doc)] pub trait OwnedCopyableVector { - fn push_all(&mut self, rhs: &const [T]); + fn push_all(&mut self, rhs: &[T]); fn grow(&mut self, n: uint, initval: &T); fn grow_set(&mut self, index: uint, initval: &T, val: T); } @@ -1860,7 +1847,7 @@ impl OwnedCopyableVector for ~[T] { /// assert!(a == ~[1, 2, 3, 4]); /// ~~~ #[inline] - fn push_all(&mut self, rhs: &const [T]) { + fn push_all(&mut self, rhs: &[T]) { let new_len = self.len() + rhs.len(); self.reserve(new_len); @@ -2017,7 +2004,7 @@ pub mod raw { use ptr; use sys; use unstable::intrinsics; - use vec::{UnboxedVecRepr, as_const_buf, as_mut_buf, with_capacity}; + use vec::{UnboxedVecRepr, as_imm_buf, as_mut_buf, with_capacity}; use util; /// The internal representation of a (boxed) vector @@ -2065,15 +2052,6 @@ pub mod raw { } } - /** see `to_ptr()` */ - #[inline] - pub fn to_const_ptr(v: &const [T]) -> *const T { - unsafe { - let repr: **SliceRepr = transmute(&v); - transmute(&((**repr).data)) - } - } - /** see `to_ptr()` */ #[inline] pub fn to_mut_ptr(v: &mut [T]) -> *mut T { @@ -2113,8 +2091,8 @@ pub mod raw { * Unchecked vector indexing. */ #[inline] - pub unsafe fn get(v: &const [T], i: uint) -> T { - as_const_buf(v, |p, _len| copy *ptr::const_offset(p, i)) + pub unsafe fn get(v: &[T], i: uint) -> T { + as_imm_buf(v, |p, _len| copy *ptr::offset(p, i)) } /** @@ -2156,13 +2134,13 @@ pub mod raw { * may overlap. */ #[inline] - pub unsafe fn copy_memory(dst: &mut [T], src: &const [T], + pub unsafe fn copy_memory(dst: &mut [T], src: &[T], count: uint) { assert!(dst.len() >= count); assert!(src.len() >= count); do as_mut_buf(dst) |p_dst, _len_dst| { - do as_const_buf(src) |p_src, _len_src| { + do as_imm_buf(src) |p_src, _len_src| { ptr::copy_memory(p_dst, p_src, count) } } @@ -2238,7 +2216,7 @@ pub mod bytes { * may overlap. */ #[inline] - pub fn copy_memory(dst: &mut [u8], src: &const [u8], count: uint) { + pub fn copy_memory(dst: &mut [u8], src: &[u8], count: uint) { // Bound checks are done at vec::raw::copy_memory. unsafe { vec::raw::copy_memory(dst, src, count) } } @@ -3690,16 +3668,6 @@ mod tests { } } - #[test] - #[ignore(windows)] - #[should_fail] - fn test_as_const_buf_fail() { - let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)]; - do as_const_buf(v) |_buf, _i| { - fail!() - } - } - #[test] #[ignore(cfg(windows))] #[should_fail] From 3bad7129ebb13d7a4c0a7965aeb5bd536cc0f5f0 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 23 Jun 2013 21:12:17 -0700 Subject: [PATCH 2/2] Remove mutability from unique boxes in the AST --- src/librustc/metadata/decoder.rs | 2 +- src/librustc/metadata/encoder.rs | 3 +-- src/librustc/middle/check_const.rs | 2 +- src/librustc/middle/liveness.rs | 2 +- src/librustc/middle/trans/consts.rs | 2 +- src/librustc/middle/trans/expr.rs | 2 +- src/librustc/middle/trans/meth.rs | 4 ++-- src/librustc/middle/trans/type_use.rs | 2 +- src/librustc/middle/typeck/astconv.rs | 4 ++-- src/librustc/middle/typeck/check/method.rs | 4 ++-- src/librustc/middle/typeck/check/mod.rs | 7 ++++--- src/libsyntax/ast.rs | 4 ++-- src/libsyntax/ast_util.rs | 2 +- src/libsyntax/ext/deriving/ty.rs | 2 +- src/libsyntax/parse/parser.rs | 12 ++++++++---- src/libsyntax/print/pprust.rs | 4 +--- 16 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs index 69faf519bc28..b08186349319 100644 --- a/src/librustc/metadata/decoder.rs +++ b/src/librustc/metadata/decoder.rs @@ -786,7 +786,7 @@ fn get_explicit_self(item: ebml::Doc) -> ast::explicit_self_ { 's' => { return ast::sty_static; } 'v' => { return ast::sty_value; } '@' => { return ast::sty_box(get_mutability(string[1])); } - '~' => { return ast::sty_uniq(get_mutability(string[1])); } + '~' => { return ast::sty_uniq; } '&' => { // FIXME(#4846) expl. region return ast::sty_region(None, get_mutability(string[1])); diff --git a/src/librustc/metadata/encoder.rs b/src/librustc/metadata/encoder.rs index e394c8dcf92f..7735a7e39561 100644 --- a/src/librustc/metadata/encoder.rs +++ b/src/librustc/metadata/encoder.rs @@ -630,9 +630,8 @@ fn encode_explicit_self(ebml_w: &mut writer::Encoder, explicit_self: ast::explic ebml_w.writer.write(&[ '@' as u8 ]); encode_mutability(ebml_w, m); } - sty_uniq(m) => { + sty_uniq => { ebml_w.writer.write(&[ '~' as u8 ]); - encode_mutability(ebml_w, m); } } diff --git a/src/librustc/middle/check_const.rs b/src/librustc/middle/check_const.rs index 2c6ba79e96e4..8ad61fe1f61c 100644 --- a/src/librustc/middle/check_const.rs +++ b/src/librustc/middle/check_const.rs @@ -92,7 +92,7 @@ pub fn check_expr(sess: Session, if is_const { match e.node { expr_unary(_, deref, _) => { } - expr_unary(_, box(_), _) | expr_unary(_, uniq(_), _) => { + expr_unary(_, box(_), _) | expr_unary(_, uniq, _) => { sess.span_err(e.span, "disallowed operator in constant expression"); return; diff --git a/src/librustc/middle/liveness.rs b/src/librustc/middle/liveness.rs index 86b8b2943198..e431784f1e63 100644 --- a/src/librustc/middle/liveness.rs +++ b/src/librustc/middle/liveness.rs @@ -368,7 +368,7 @@ fn visit_fn(fk: &visit::fn_kind, match *fk { fk_method(_, _, method) => { match method.explicit_self.node { - sty_value | sty_region(*) | sty_box(_) | sty_uniq(_) => { + sty_value | sty_region(*) | sty_box(_) | sty_uniq => { fn_maps.add_variable(Arg(method.self_id, special_idents::self_)); } diff --git a/src/librustc/middle/trans/consts.rs b/src/librustc/middle/trans/consts.rs index d7b6e9cf5a37..6780c51e5062 100644 --- a/src/librustc/middle/trans/consts.rs +++ b/src/librustc/middle/trans/consts.rs @@ -340,7 +340,7 @@ fn const_expr_unadjusted(cx: @mut CrateContext, e: &ast::expr) -> ValueRef { let is_float = ty::type_is_fp(ty); return match u { ast::box(_) | - ast::uniq(_) | + ast::uniq | ast::deref => { let (dv, _dt) = const_deref(cx, te, ty, true); dv diff --git a/src/librustc/middle/trans/expr.rs b/src/librustc/middle/trans/expr.rs index 353227307560..297a466c7cb0 100644 --- a/src/librustc/middle/trans/expr.rs +++ b/src/librustc/middle/trans/expr.rs @@ -1314,7 +1314,7 @@ fn trans_unary_datum(bcx: block, trans_boxed_expr(bcx, un_ty, sub_expr, sub_ty, heap_managed) } - ast::uniq(_) => { + ast::uniq => { let heap = heap_for_unique(bcx, un_ty); trans_boxed_expr(bcx, un_ty, sub_expr, sub_ty, heap) } diff --git a/src/librustc/middle/trans/meth.rs b/src/librustc/middle/trans/meth.rs index 0b051662781e..75e9dbe48619 100644 --- a/src/librustc/middle/trans/meth.rs +++ b/src/librustc/middle/trans/meth.rs @@ -532,7 +532,7 @@ pub fn trans_trait_callee(bcx: block, let llpair = match explicit_self { ast::sty_region(*) => Load(bcx, llpair), ast::sty_static | ast::sty_value | - ast::sty_box(_) | ast::sty_uniq(_) => llpair + ast::sty_box(_) | ast::sty_uniq => llpair }; let callee_ty = node_id_type(bcx, callee_id); @@ -622,7 +622,7 @@ pub fn trans_trait_callee_from_llval(bcx: block, self_mode = ty::ByRef; } - ast::sty_uniq(_) => { + ast::sty_uniq => { // Pass the unique pointer. match store { ty::UniqTraitStore => llself = llbox, diff --git a/src/librustc/middle/trans/type_use.rs b/src/librustc/middle/trans/type_use.rs index c636b7f48761..d8d539764620 100644 --- a/src/librustc/middle/trans/type_use.rs +++ b/src/librustc/middle/trans/type_use.rs @@ -278,7 +278,7 @@ pub fn mark_for_method_call(cx: &Context, e_id: node_id, callee_id: node_id) { pub fn mark_for_expr(cx: &Context, e: &expr) { match e.node { expr_vstore(_, _) | expr_vec(_, _) | expr_struct(*) | expr_tup(_) | - expr_unary(_, box(_), _) | expr_unary(_, uniq(_), _) | + expr_unary(_, box(_), _) | expr_unary(_, uniq, _) | expr_binary(_, add, _, _) | expr_copy(_) | expr_repeat(*) => { node_type_needs(cx, use_repr, e.id); } diff --git a/src/librustc/middle/typeck/astconv.rs b/src/librustc/middle/typeck/astconv.rs index 07fb23fea48a..1e236c620a0e 100644 --- a/src/librustc/middle/typeck/astconv.rs +++ b/src/librustc/middle/typeck/astconv.rs @@ -662,10 +662,10 @@ fn ty_of_method_or_bare_fn( ty::mt {ty: self_info.untransformed_self_ty, mutbl: mutability})) } - ast::sty_uniq(mutability) => { + ast::sty_uniq => { Some(ty::mk_uniq(this.tcx(), ty::mt {ty: self_info.untransformed_self_ty, - mutbl: mutability})) + mutbl: ast::m_imm})) } } } diff --git a/src/librustc/middle/typeck/check/method.rs b/src/librustc/middle/typeck/check/method.rs index 4bebca3c9a8b..7489882ab768 100644 --- a/src/librustc/middle/typeck/check/method.rs +++ b/src/librustc/middle/typeck/check/method.rs @@ -1123,11 +1123,11 @@ impl<'self> LookupContext<'self> { } } - sty_uniq(m) => { + sty_uniq => { debug!("(is relevant?) explicit self is a unique pointer"); match ty::get(rcvr_ty).sty { ty::ty_uniq(mt) => { - mutability_matches(mt.mutbl, m) && + mutability_matches(mt.mutbl, ast::m_imm) && self.fcx.can_mk_subty(mt.ty, candidate.rcvr_ty).is_ok() } diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs index 23266767124c..0cd9aac37989 100644 --- a/src/librustc/middle/typeck/check/mod.rs +++ b/src/librustc/middle/typeck/check/mod.rs @@ -2301,7 +2301,7 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt, ast::expr_unary(callee_id, unop, oprnd) => { let exp_inner = do unpack_expected(fcx, expected) |sty| { match unop { - ast::box(_) | ast::uniq(_) => match *sty { + ast::box(_) | ast::uniq => match *sty { ty::ty_box(ref mt) | ty::ty_uniq(ref mt) => Some(mt.ty), _ => None }, @@ -2318,9 +2318,10 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt, oprnd_t = ty::mk_box(tcx, ty::mt {ty: oprnd_t, mutbl: mutbl}); } - ast::uniq(mutbl) => { + ast::uniq => { oprnd_t = ty::mk_uniq(tcx, - ty::mt {ty: oprnd_t, mutbl: mutbl}); + ty::mt {ty: oprnd_t, + mutbl: ast::m_imm}); } ast::deref => { let sty = structure_of(fcx, expr.span, oprnd_t); diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index bc432c4c7b0b..2603cbb2dd7c 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -333,7 +333,7 @@ pub enum binop { #[deriving(Eq, Encodable, Decodable,IterBytes)] pub enum unop { box(mutability), - uniq(mutability), + uniq, deref, not, neg @@ -805,7 +805,7 @@ pub enum explicit_self_ { sty_value, // `self` sty_region(Option<@Lifetime>, mutability), // `&'lt self` sty_box(mutability), // `@self` - sty_uniq(mutability) // `~self` + sty_uniq // `~self` } pub type explicit_self = spanned; diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index 9439f45be21b..ee7c7180f8db 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -135,7 +135,7 @@ pub fn is_shift_binop(b: binop) -> bool { pub fn unop_to_str(op: unop) -> ~str { match op { box(mt) => if mt == m_mutbl { ~"@mut " } else { ~"@" }, - uniq(mt) => if mt == m_mutbl { ~"~mut " } else { ~"~" }, + uniq => ~"~", deref => ~"*", not => ~"!", neg => ~"-" diff --git a/src/libsyntax/ext/deriving/ty.rs b/src/libsyntax/ext/deriving/ty.rs index a2f9aa58d99a..e210853bfb4d 100644 --- a/src/libsyntax/ext/deriving/ty.rs +++ b/src/libsyntax/ext/deriving/ty.rs @@ -248,7 +248,7 @@ pub fn get_explicit_self(cx: @ExtCtxt, span: span, self_ptr: &Option) let self_ty = respan( span, match *ptr { - Send => ast::sty_uniq(ast::m_imm), + Send => ast::sty_uniq, Managed(mutbl) => ast::sty_box(mutbl), Borrowed(ref lt, mutbl) => { let lt = lt.map(|s| @cx.lifetime(span, diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index f1b5c4d16be9..cc0baa28e20d 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2071,9 +2071,8 @@ impl Parser { ex = match e.node { expr_vec(*) | expr_lit(@codemap::spanned { node: lit_str(_), span: _}) | - expr_repeat(*) - if m == m_imm => expr_vstore(e, expr_vstore_uniq), - _ => self.mk_unary(uniq(m), e) + expr_repeat(*) => expr_vstore(e, expr_vstore_uniq), + _ => self.mk_unary(uniq, e) }; } _ => return self.parse_dot_or_call_expr() @@ -3366,7 +3365,12 @@ impl Parser { maybe_parse_explicit_self(sty_box, self) } token::TILDE => { - maybe_parse_explicit_self(sty_uniq, self) + maybe_parse_explicit_self(|mutability| { + if mutability != m_imm { + self.obsolete(*self.last_span, ObsoleteMutOwnedPointer); + } + sty_uniq + }, self) } token::IDENT(*) if self.is_self_ident() => { self.bump(); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 9a1682bf063d..978561eaa67c 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1653,6 +1653,7 @@ pub fn print_explicit_self(s: @ps, explicit_self: ast::explicit_self_) -> bool { match explicit_self { ast::sty_static => { return false; } ast::sty_value => { word(s.s, "self"); } + ast::sty_uniq => { word(s.s, "~self"); } ast::sty_region(lt, m) => { word(s.s, "&"); print_opt_lifetime(s, lt); @@ -1662,9 +1663,6 @@ pub fn print_explicit_self(s: @ps, explicit_self: ast::explicit_self_) -> bool { ast::sty_box(m) => { word(s.s, "@"); print_mutability(s, m); word(s.s, "self"); } - ast::sty_uniq(m) => { - word(s.s, "~"); print_mutability(s, m); word(s.s, "self"); - } } return true; }