From 8d7e6ef7725f8a11de940892a74398fc1911dfc7 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Tue, 26 Feb 2013 11:32:00 -0800 Subject: [PATCH] libsyntax: Forbid `~mut` and `~const`. rs=demuting --- doc/tutorial.md | 2 +- src/libcore/owned.rs | 16 ++++++++-------- src/libstd/arc.rs | 7 ++++--- src/libstd/future.rs | 11 +++++------ src/libstd/sync.rs | 11 ++++++----- src/libstd/workcache.rs | 5 +++-- src/libsyntax/parse/obsolete.rs | 2 +- src/libsyntax/parse/parser.rs | 6 +++++- .../compile-fail/mutable-huh-unique-assign.rs | 19 ------------------- 9 files changed, 33 insertions(+), 46 deletions(-) delete mode 100644 src/test/compile-fail/mutable-huh-unique-assign.rs diff --git a/doc/tutorial.md b/doc/tutorial.md index 98ec9d1f580..f9408761877 100644 --- a/doc/tutorial.md +++ b/doc/tutorial.md @@ -1126,7 +1126,7 @@ points to. ~~~ let managed = @mut 10; -let owned = ~mut 20; +let mut owned = ~20; let mut value = 30; let borrowed = &mut value; diff --git a/src/libcore/owned.rs b/src/libcore/owned.rs index 230386655e0..3b839e5a9e0 100644 --- a/src/libcore/owned.rs +++ b/src/libcore/owned.rs @@ -13,22 +13,22 @@ use cmp::{Eq, Ord}; #[cfg(notest)] -impl Eq for ~const T { +impl Eq for ~T { #[inline(always)] - pure fn eq(&self, other: &~const T) -> bool { *(*self) == *(*other) } + pure fn eq(&self, other: &~T) -> bool { *(*self) == *(*other) } #[inline(always)] - pure fn ne(&self, other: &~const T) -> bool { *(*self) != *(*other) } + pure fn ne(&self, other: &~T) -> bool { *(*self) != *(*other) } } #[cfg(notest)] -impl Ord for ~const T { +impl Ord for ~T { #[inline(always)] - pure fn lt(&self, other: &~const T) -> bool { *(*self) < *(*other) } + pure fn lt(&self, other: &~T) -> bool { *(*self) < *(*other) } #[inline(always)] - pure fn le(&self, other: &~const T) -> bool { *(*self) <= *(*other) } + pure fn le(&self, other: &~T) -> bool { *(*self) <= *(*other) } #[inline(always)] - pure fn ge(&self, other: &~const T) -> bool { *(*self) >= *(*other) } + pure fn ge(&self, other: &~T) -> bool { *(*self) >= *(*other) } #[inline(always)] - pure fn gt(&self, other: &~const T) -> bool { *(*self) > *(*other) } + pure fn gt(&self, other: &~T) -> bool { *(*self) > *(*other) } } diff --git a/src/libstd/arc.rs b/src/libstd/arc.rs index 61b5ffd845f..f258e649122 100644 --- a/src/libstd/arc.rs +++ b/src/libstd/arc.rs @@ -17,6 +17,7 @@ use sync; use sync::{Mutex, mutex_with_condvars, RWlock, rwlock_with_condvars}; use core::cast; +use core::cell::Cell; use core::pipes; use core::prelude::*; use core::private::{SharedMutableState, shared_mutable_state}; @@ -532,17 +533,17 @@ mod tests { let arc = ~MutexARC(false); let arc2 = ~arc.clone(); let (p,c) = comm::oneshot(); - let (c,p) = (~mut Some(c), ~mut Some(p)); + let (c,p) = (Cell(c), Cell(p)); do task::spawn || { // wait until parent gets in - comm::recv_one(option::swap_unwrap(p)); + comm::recv_one(p.take()); do arc2.access_cond |state, cond| { *state = true; cond.signal(); } } do arc.access_cond |state, cond| { - comm::send_one(option::swap_unwrap(c), ()); + comm::send_one(c.take(), ()); assert !*state; while !*state { cond.wait(); diff --git a/src/libstd/future.rs b/src/libstd/future.rs index b6b001727a4..7f48466ed0a 100644 --- a/src/libstd/future.rs +++ b/src/libstd/future.rs @@ -23,6 +23,7 @@ use core::cast::copy_lifetime; use core::cast; +use core::cell::Cell; use core::either::Either; use core::option; use core::comm::{oneshot, ChanOne, PortOne, send_one, recv_one}; @@ -103,11 +104,9 @@ pub fn from_port(port: PortOne) -> * waiting for the result to be received on the port. */ - let port = ~mut Some(port); + let port = Cell(port); do from_fn || { - let mut port_ = None; - port_ <-> *port; - let port = option::unwrap(port_); + let port = port.take(); match recv(port) { oneshot::send(data) => data } @@ -136,9 +135,9 @@ pub fn spawn(blk: fn~() -> A) -> Future { let (chan, port) = oneshot::init(); - let chan = ~mut Some(chan); + let chan = Cell(chan); do task::spawn || { - let chan = option::swap_unwrap(&mut *chan); + let chan = chan.take(); send_one(chan, blk()); } diff --git a/src/libstd/sync.rs b/src/libstd/sync.rs index 016847a5bfd..1ff51e8bff0 100644 --- a/src/libstd/sync.rs +++ b/src/libstd/sync.rs @@ -15,6 +15,7 @@ * in std. */ +use core::cell::Cell; use core::option; use core::pipes; use core::prelude::*; @@ -799,9 +800,9 @@ mod tests { let s = ~semaphore(1); let s2 = ~s.clone(); let (p,c) = comm::stream(); - let child_data = ~mut Some((s2, c)); + let child_data = Cell((s2, c)); do s.access { - let (s2,c) = option::swap_unwrap(child_data); + let (s2, c) = child_data.take(); do task::spawn || { c.send(()); do s2.access { } @@ -976,13 +977,13 @@ mod tests { let mut sibling_convos = ~[]; for 2.times { let (p,c) = comm::stream(); - let c = ~mut Some(c); + let c = Cell(c); sibling_convos.push(p); let mi = ~m2.clone(); // spawn sibling task - do task::spawn || { // linked + do task::spawn { // linked do mi.lock_cond |cond| { - let c = option::swap_unwrap(c); + let c = c.take(); c.send(()); // tell sibling to go ahead let _z = SendOnFailure(c); cond.wait(); // block forever diff --git a/src/libstd/workcache.rs b/src/libstd/workcache.rs index 8ce68a41f81..d7ca766f183 100644 --- a/src/libstd/workcache.rs +++ b/src/libstd/workcache.rs @@ -15,6 +15,7 @@ use sha1; use serialize::{Encoder, Encodable, Decoder, Decodable}; use sort; +use core::cell::Cell; use core::cmp; use core::either::{Either, Left, Right}; use core::io; @@ -339,11 +340,11 @@ impl TPrep for @Mut { let mut blk = None; blk <-> bo; let blk = blk.unwrap(); - let chan = ~mut Some(chan); + let chan = Cell(chan); do task::spawn || { let exe = Exec{discovered_inputs: LinearMap::new(), discovered_outputs: LinearMap::new()}; - let chan = option::swap_unwrap(&mut *chan); + let chan = chan.take(); let v = blk(&exe); send_one(chan, (exe, v)); } diff --git a/src/libsyntax/parse/obsolete.rs b/src/libsyntax/parse/obsolete.rs index 7b3030124b7..0c5c0f3d513 100644 --- a/src/libsyntax/parse/obsolete.rs +++ b/src/libsyntax/parse/obsolete.rs @@ -128,7 +128,7 @@ pub impl Parser { "write `+` between trait bounds" ), ObsoleteMutOwnedPointer => ( - "mutable owned pointer", + "const or mutable owned pointer", "mutability inherits through `~` pointers; place the `~` box in a mutable location, like a mutable local variable or an \ `@mut` box" diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index af25a4f6e58..c9102cbb86b 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -678,7 +678,7 @@ pub impl Parser { // reflected in the AST type. let mt = self.parse_mt(); - if mt.mutbl == m_mutbl && sigil == OwnedSigil { + if mt.mutbl != m_imm && sigil == OwnedSigil { self.obsolete(*self.last_span, ObsoleteMutOwnedPointer); } @@ -1574,6 +1574,10 @@ pub impl Parser { token::TILDE => { self.bump(); let m = self.parse_mutability(); + if m != m_imm { + self.obsolete(*self.last_span, ObsoleteMutOwnedPointer); + } + let e = self.parse_prefix_expr(); hi = e.span.hi; // HACK: turn ~[...] into a ~-evec diff --git a/src/test/compile-fail/mutable-huh-unique-assign.rs b/src/test/compile-fail/mutable-huh-unique-assign.rs deleted file mode 100644 index 8b59879acd9..00000000000 --- a/src/test/compile-fail/mutable-huh-unique-assign.rs +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -fn main() { - fn f(&&v: ~const int) { - *v = 1 //~ ERROR assigning to dereference of const ~ pointer - } - - let v = ~0; - - f(v); -}