Skip to content

Commit 19cff91

Browse files
committed
auto merge of #11188 : brson/rust/noderef, r=brson
This removes the feature where newtype structs can be dereferenced like pointers, and likewise where certain enums can be dereferenced (which I imagine nobody realized still existed). This ad-hoc behavior is to be replaced by a more general overloadable dereference trait in the future. I've been nursing this patch for two months and think it's about rebased up to master. @nikomatsakis this makes a bunch of your type checking code noticeably uglier.
2 parents 55d4923 + 01af682 commit 19cff91

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+615
-764
lines changed

doc/tutorial.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -793,15 +793,6 @@ synonym for an existing type but is rather its own distinct type.
793793
struct GizmoId(int);
794794
~~~~
795795

796-
For convenience, you can extract the contents of such a struct with the
797-
dereference (`*`) unary operator:
798-
799-
~~~~
800-
# struct GizmoId(int);
801-
let my_gizmo_id: GizmoId = GizmoId(10);
802-
let id_int: int = *my_gizmo_id;
803-
~~~~
804-
805796
Types like this can be useful to differentiate between data that have
806797
the same underlying type but must be used in different ways.
807798

@@ -811,7 +802,16 @@ struct Centimeters(int);
811802
~~~~
812803

813804
The above definitions allow for a simple way for programs to avoid
814-
confusing numbers that correspond to different units.
805+
confusing numbers that correspond to different units. Their integer
806+
values can be extracted with pattern matching:
807+
808+
~~~
809+
# struct Inches(int);
810+
811+
let length_with_unit = Inches(10);
812+
let Inches(integer_length) = length_with_unit;
813+
println!("length is {} inches", integer_length);
814+
~~~
815815

816816
# Functions
817817

@@ -2595,7 +2595,7 @@ the `priv` keyword:
25952595
mod farm {
25962596
# pub type Chicken = int;
25972597
# struct Human(int);
2598-
# impl Human { fn rest(&self) { } }
2598+
# impl Human { pub fn rest(&self) { } }
25992599
# pub fn make_me_a_farm() -> Farm { Farm { chickens: ~[], farmer: Human(0) } }
26002600
pub struct Farm {
26012601
priv chickens: ~[Chicken],

src/libextra/sync.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ impl<Q:Send> Sem<Q> {
107107
pub fn acquire(&self) {
108108
unsafe {
109109
let mut waiter_nobe = None;
110-
(**self).with(|state| {
110+
let Sem(ref lock) = *self;
111+
lock.with(|state| {
111112
state.count -= 1;
112113
if state.count < 0 {
113114
// Create waiter nobe, enqueue ourself, and tell
@@ -126,7 +127,8 @@ impl<Q:Send> Sem<Q> {
126127

127128
pub fn release(&self) {
128129
unsafe {
129-
(**self).with(|state| {
130+
let Sem(ref lock) = *self;
131+
lock.with(|state| {
130132
state.count += 1;
131133
if state.count <= 0 {
132134
state.waiters.signal();
@@ -206,7 +208,8 @@ impl<'a> Condvar<'a> {
206208
let mut out_of_bounds = None;
207209
// Release lock, 'atomically' enqueuing ourselves in so doing.
208210
unsafe {
209-
(**self.sem).with(|state| {
211+
let Sem(ref queue) = *self.sem;
212+
queue.with(|state| {
210213
if condvar_id < state.blocked.len() {
211214
// Drop the lock.
212215
state.count += 1;
@@ -248,7 +251,8 @@ impl<'a> Condvar<'a> {
248251
unsafe {
249252
let mut out_of_bounds = None;
250253
let mut result = false;
251-
(**self.sem).with(|state| {
254+
let Sem(ref lock) = *self.sem;
255+
lock.with(|state| {
252256
if condvar_id < state.blocked.len() {
253257
result = state.blocked[condvar_id].signal();
254258
} else {
@@ -270,7 +274,8 @@ impl<'a> Condvar<'a> {
270274
let mut out_of_bounds = None;
271275
let mut queue = None;
272276
unsafe {
273-
(**self.sem).with(|state| {
277+
let Sem(ref lock) = *self.sem;
278+
lock.with(|state| {
274279
if condvar_id < state.blocked.len() {
275280
// To avoid :broadcast_heavy, we make a new waitqueue,
276281
// swap it out with the old one, and broadcast on the
@@ -336,7 +341,8 @@ pub struct Semaphore { priv sem: Sem<()> }
336341
impl Clone for Semaphore {
337342
/// Create a new handle to the semaphore.
338343
fn clone(&self) -> Semaphore {
339-
Semaphore { sem: Sem((*self.sem).clone()) }
344+
let Sem(ref lock) = self.sem;
345+
Semaphore { sem: Sem(lock.clone()) }
340346
}
341347
}
342348

@@ -378,7 +384,9 @@ impl Semaphore {
378384
pub struct Mutex { priv sem: Sem<~[WaitQueue]> }
379385
impl Clone for Mutex {
380386
/// Create a new handle to the mutex.
381-
fn clone(&self) -> Mutex { Mutex { sem: Sem((*self.sem).clone()) } }
387+
fn clone(&self) -> Mutex {
388+
let Sem(ref queue) = self.sem;
389+
Mutex { sem: Sem(queue.clone()) } }
382390
}
383391

384392
impl Mutex {
@@ -467,8 +475,9 @@ impl RWLock {
467475

468476
/// Create a new handle to the rwlock.
469477
pub fn clone(&self) -> RWLock {
478+
let Sem(ref access_lock_queue) = self.access_lock;
470479
RWLock { order_lock: (&(self.order_lock)).clone(),
471-
access_lock: Sem((*self.access_lock).clone()),
480+
access_lock: Sem(access_lock_queue.clone()),
472481
state: self.state.clone() }
473482
}
474483

src/libextra/test.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ pub struct MetricMap(TreeMap<~str,Metric>);
137137

138138
impl Clone for MetricMap {
139139
fn clone(&self) -> MetricMap {
140-
MetricMap((**self).clone())
140+
let MetricMap(ref map) = *self;
141+
MetricMap(map.clone())
141142
}
142143
}
143144

@@ -584,6 +585,7 @@ impl<T: Writer> ConsoleTestState<T> {
584585
}
585586

586587
pub fn fmt_metrics(mm: &MetricMap) -> ~str {
588+
let MetricMap(ref mm) = *mm;
587589
let v : ~[~str] = mm.iter()
588590
.map(|(k,v)| format!("{}: {} (+/- {})",
589591
*k,
@@ -622,6 +624,7 @@ pub fn run_tests_console(opts: &TestOpts,
622624
TrIgnored => st.ignored += 1,
623625
TrMetrics(mm) => {
624626
let tname = test.name.to_str();
627+
let MetricMap(mm) = mm;
625628
for (k,v) in mm.iter() {
626629
st.metrics.insert_metric(tname + "." + *k,
627630
v.value, v.noise);
@@ -950,7 +953,8 @@ impl MetricMap {
950953
/// Write MetricDiff to a file.
951954
pub fn save(&self, p: &Path) {
952955
let mut file = File::create(p);
953-
self.to_json().to_pretty_writer(&mut file)
956+
let MetricMap(ref map) = *self;
957+
map.to_json().to_pretty_writer(&mut file)
954958
}
955959
956960
/// Compare against another MetricMap. Optionally compare all
@@ -962,8 +966,10 @@ impl MetricMap {
962966
pub fn compare_to_old(&self, old: &MetricMap,
963967
noise_pct: Option<f64>) -> MetricDiff {
964968
let mut diff : MetricDiff = TreeMap::new();
969+
let MetricMap(ref selfmap) = *self;
970+
let MetricMap(ref old) = *old;
965971
for (k, vold) in old.iter() {
966-
let r = match self.find(k) {
972+
let r = match selfmap.find(k) {
967973
None => MetricRemoved,
968974
Some(v) => {
969975
let delta = (v.value - vold.value);
@@ -999,7 +1005,8 @@ impl MetricMap {
9991005
};
10001006
diff.insert((*k).clone(), r);
10011007
}
1002-
for (k, _) in self.iter() {
1008+
let MetricMap(ref map) = *self;
1009+
for (k, _) in map.iter() {
10031010
if !diff.contains_key(k) {
10041011
diff.insert((*k).clone(), MetricAdded);
10051012
}
@@ -1025,7 +1032,8 @@ impl MetricMap {
10251032
value: value,
10261033
noise: noise
10271034
};
1028-
self.insert(name.to_owned(), m);
1035+
let MetricMap(ref mut map) = *self;
1036+
map.insert(name.to_owned(), m);
10291037
}
10301038
10311039
/// Attempt to "ratchet" an external metric file. This involves loading
@@ -1464,6 +1472,7 @@ mod tests {
14641472
14651473
// Check that it was not rewritten.
14661474
let m3 = MetricMap::load(&pth);
1475+
let MetricMap(m3) = m3;
14671476
assert_eq!(m3.len(), 2);
14681477
assert_eq!(*(m3.find(&~"runtime").unwrap()), Metric { value: 1000.0, noise: 2.0 });
14691478
assert_eq!(*(m3.find(&~"throughput").unwrap()), Metric { value: 50.0, noise: 2.0 });
@@ -1478,6 +1487,7 @@ mod tests {
14781487
14791488
// Check that it was rewritten.
14801489
let m4 = MetricMap::load(&pth);
1490+
let MetricMap(m4) = m4;
14811491
assert_eq!(m4.len(), 2);
14821492
assert_eq!(*(m4.find(&~"runtime").unwrap()), Metric { value: 1100.0, noise: 2.0 });
14831493
assert_eq!(*(m4.find(&~"throughput").unwrap()), Metric { value: 50.0, noise: 2.0 });

src/libextra/workcache.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,14 @@ impl WorkMap {
116116

117117
fn insert_work_key(&mut self, k: WorkKey, val: ~str) {
118118
let WorkKey { kind, name } = k;
119-
match self.find_mut(&name) {
119+
let WorkMap(ref mut map) = *self;
120+
match map.find_mut(&name) {
120121
Some(&KindMap(ref mut m)) => { m.insert(kind, val); return; }
121122
None => ()
122123
}
123124
let mut new_map = TreeMap::new();
124125
new_map.insert(kind, val);
125-
self.insert(name, KindMap(new_map));
126+
map.insert(name, KindMap(new_map));
126127
}
127128
}
128129

@@ -328,8 +329,10 @@ impl Exec {
328329
// returns pairs of (kind, name)
329330
pub fn lookup_discovered_inputs(&self) -> ~[(~str, ~str)] {
330331
let mut rs = ~[];
331-
for (k, v) in self.discovered_inputs.iter() {
332-
for (k1, _) in v.iter() {
332+
let WorkMap(ref discovered_inputs) = self.discovered_inputs;
333+
for (k, v) in discovered_inputs.iter() {
334+
let KindMap(ref vmap) = *v;
335+
for (k1, _) in vmap.iter() {
333336
rs.push((k1.clone(), k.clone()));
334337
}
335338
}
@@ -348,8 +351,10 @@ impl<'a> Prep<'a> {
348351

349352
pub fn lookup_declared_inputs(&self) -> ~[~str] {
350353
let mut rs = ~[];
351-
for (_, v) in self.declared_inputs.iter() {
352-
for (inp, _) in v.iter() {
354+
let WorkMap(ref declared_inputs) = self.declared_inputs;
355+
for (_, v) in declared_inputs.iter() {
356+
let KindMap(ref vmap) = *v;
357+
for (inp, _) in vmap.iter() {
353358
rs.push(inp.clone());
354359
}
355360
}
@@ -386,8 +391,10 @@ impl<'a> Prep<'a> {
386391
}
387392

388393
fn all_fresh(&self, cat: &str, map: &WorkMap) -> bool {
394+
let WorkMap(ref map) = *map;
389395
for (k_name, kindmap) in map.iter() {
390-
for (k_kind, v) in kindmap.iter() {
396+
let KindMap(ref kindmap_) = *kindmap;
397+
for (k_kind, v) in kindmap_.iter() {
391398
if ! self.is_fresh(cat, *k_kind, *k_name, *v) {
392399
return false;
393400
}

src/libgreen/stack.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ pub struct StackPool(());
6161
impl StackPool {
6262
pub fn new() -> StackPool { StackPool(()) }
6363

64-
fn take_segment(&self, min_size: uint) -> StackSegment {
64+
pub fn take_segment(&self, min_size: uint) -> StackSegment {
6565
StackSegment::new(min_size)
6666
}
6767

68-
fn give_segment(&self, _stack: StackSegment) {
68+
pub fn give_segment(&self, _stack: StackSegment) {
6969
}
7070
}
7171

src/librustc/middle/borrowck/move_data.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,15 @@ pub struct FlowedMoveData {
7272
#[deriving(Eq)]
7373
pub struct MovePathIndex(uint);
7474

75+
impl MovePathIndex {
76+
fn get(&self) -> uint {
77+
let MovePathIndex(v) = *self; v
78+
}
79+
}
80+
7581
impl Clone for MovePathIndex {
7682
fn clone(&self) -> MovePathIndex {
77-
MovePathIndex(**self)
83+
MovePathIndex(self.get())
7884
}
7985
}
8086

@@ -85,6 +91,12 @@ static InvalidMovePathIndex: MovePathIndex =
8591
#[deriving(Eq)]
8692
pub struct MoveIndex(uint);
8793

94+
impl MoveIndex {
95+
fn get(&self) -> uint {
96+
let MoveIndex(v) = *self; v
97+
}
98+
}
99+
88100
static InvalidMoveIndex: MoveIndex =
89101
MoveIndex(uint::max_value);
90102

@@ -177,47 +189,47 @@ impl MoveData {
177189

178190
fn path_loan_path(&self, index: MovePathIndex) -> @LoanPath {
179191
let paths = self.paths.borrow();
180-
paths.get()[*index].loan_path
192+
paths.get()[index.get()].loan_path
181193
}
182194

183195
fn path_parent(&self, index: MovePathIndex) -> MovePathIndex {
184196
let paths = self.paths.borrow();
185-
paths.get()[*index].parent
197+
paths.get()[index.get()].parent
186198
}
187199

188200
fn path_first_move(&self, index: MovePathIndex) -> MoveIndex {
189201
let paths = self.paths.borrow();
190-
paths.get()[*index].first_move
202+
paths.get()[index.get()].first_move
191203
}
192204

193205
fn path_first_child(&self, index: MovePathIndex) -> MovePathIndex {
194206
let paths = self.paths.borrow();
195-
paths.get()[*index].first_child
207+
paths.get()[index.get()].first_child
196208
}
197209

198210
fn path_next_sibling(&self, index: MovePathIndex) -> MovePathIndex {
199211
let paths = self.paths.borrow();
200-
paths.get()[*index].next_sibling
212+
paths.get()[index.get()].next_sibling
201213
}
202214

203215
fn set_path_first_move(&self,
204216
index: MovePathIndex,
205217
first_move: MoveIndex) {
206218
let mut paths = self.paths.borrow_mut();
207-
paths.get()[*index].first_move = first_move
219+
paths.get()[index.get()].first_move = first_move
208220
}
209221

210222
fn set_path_first_child(&self,
211223
index: MovePathIndex,
212224
first_child: MovePathIndex) {
213225
let mut paths = self.paths.borrow_mut();
214-
paths.get()[*index].first_child = first_child
226+
paths.get()[index.get()].first_child = first_child
215227
}
216228

217229
fn move_next_move(&self, index: MoveIndex) -> MoveIndex {
218230
//! Type safe indexing operator
219231
let moves = self.moves.borrow();
220-
moves.get()[*index].next_move
232+
moves.get()[index.get()].next_move
221233
}
222234

223235
fn is_var_path(&self, index: MovePathIndex) -> bool {
@@ -291,7 +303,7 @@ impl MoveData {
291303
index);
292304

293305
let paths = self.paths.borrow();
294-
assert_eq!(*index, paths.get().len() - 1);
306+
assert_eq!(index.get(), paths.get().len() - 1);
295307

296308
let mut path_map = self.path_map.borrow_mut();
297309
path_map.get().insert(lp, index);
@@ -549,7 +561,7 @@ impl MoveData {
549561
kill_id: ast::NodeId,
550562
dfcx_moves: &mut MoveDataFlow) {
551563
self.each_applicable_move(path, |move_index| {
552-
dfcx_moves.add_kill(kill_id, *move_index);
564+
dfcx_moves.add_kill(kill_id, move_index.get());
553565
true
554566
});
555567
}

0 commit comments

Comments
 (0)