Skip to content

Commit 4cfb92f

Browse files
committed
Use region variance to remove a bunch of unsafety in sync/arc (#2282)
1 parent 8d00603 commit 4cfb92f

File tree

2 files changed

+15
-54
lines changed

2 files changed

+15
-54
lines changed

src/libstd/arc.rs

+12-45
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,9 @@ impl<T: send> &mutex_arc<T> {
182182
do (&state.lock).lock_cond |cond| {
183183
check_poison(true, state.failed);
184184
let _z = poison_on_fail(&mut state.failed);
185-
/*
186185
blk(&mut state.data,
187186
&condvar { is_mutex: true, failed: &mut state.failed,
188187
cond: cond })
189-
*/
190-
// FIXME(#2282) region variance
191-
let fref =
192-
unsafe { unsafe::transmute_mut_region(&mut state.failed) };
193-
let cvar = condvar { is_mutex: true, failed: fref, cond: cond };
194-
blk(&mut state.data, unsafe { unsafe::transmute_region(&cvar) } )
195188
}
196189
}
197190
}
@@ -302,17 +295,9 @@ impl<T: const send> &rw_arc<T> {
302295
do borrow_rwlock(state).write_cond |cond| {
303296
check_poison(false, state.failed);
304297
let _z = poison_on_fail(&mut state.failed);
305-
/*
306298
blk(&mut state.data,
307299
&condvar { is_mutex: false, failed: &mut state.failed,
308300
cond: cond })
309-
*/
310-
// FIXME(#2282): Need region variance to use the commented-out
311-
// code above instead of this casting mess
312-
let fref =
313-
unsafe { unsafe::transmute_mut_region(&mut state.failed) };
314-
let cvar = condvar { is_mutex: false, failed: fref, cond: cond };
315-
blk(&mut state.data, unsafe { unsafe::transmute_region(&cvar) } )
316301
}
317302
}
318303
/**
@@ -353,11 +338,8 @@ impl<T: const send> &rw_arc<T> {
353338
let state = unsafe { get_shared_mutable_state(&self.x) };
354339
do borrow_rwlock(state).write_downgrade |write_mode| {
355340
check_poison(false, state.failed);
356-
// FIXME(#2282) need region variance to avoid having to cast here
357-
let (data,failed) =
358-
unsafe { (unsafe::transmute_mut_region(&mut state.data),
359-
unsafe::transmute_mut_region(&mut state.failed)) };
360-
blk(rw_write_mode((data, write_mode, poison_on_fail(failed))))
341+
blk(rw_write_mode((&mut state.data, write_mode,
342+
poison_on_fail(&mut state.failed))))
361343
}
362344
}
363345
@@ -401,8 +383,8 @@ fn unwrap_rw_arc<T: const send>(+arc: rw_arc<T>) -> T {
401383
// lock it. This wraps the unsafety, with the justification that the 'lock'
402384
// field is never overwritten; only 'failed' and 'data'.
403385
#[doc(hidden)]
404-
fn borrow_rwlock<T: const send>(state: &mut rw_arc_inner<T>) -> &rwlock {
405-
unsafe { unsafe::reinterpret_cast(&state.lock) }
386+
fn borrow_rwlock<T: const send>(state: &r/mut rw_arc_inner<T>) -> &r/rwlock {
387+
unsafe { unsafe::transmute_immut(&mut state.lock) }
406388
}
407389
408390
// FIXME (#3154) ice with struct/&<T> prevents these from being structs.
@@ -418,9 +400,7 @@ impl<T: const send> &rw_write_mode<T> {
418400
fn write<U>(blk: fn(x: &mut T) -> U) -> U {
419401
match *self {
420402
rw_write_mode((data, ref token, _)) => {
421-
// FIXME(#2282) cast to avoid region invariance
422-
let mode = unsafe { unsafe::transmute_region(token) };
423-
do mode.write {
403+
do token.write {
424404
blk(data)
425405
}
426406
}
@@ -430,13 +410,10 @@ impl<T: const send> &rw_write_mode<T> {
430410
fn write_cond<U>(blk: fn(x: &x/mut T, c: &c/condvar) -> U) -> U {
431411
match *self {
432412
rw_write_mode((data, ref token, ref poison)) => {
433-
// FIXME(#2282) cast to avoid region invariance
434-
let mode = unsafe { unsafe::transmute_region(token) };
435-
do mode.write_cond |cond| {
413+
do token.write_cond |cond| {
436414
let cvar = condvar {
437415
is_mutex: false, failed: poison.failed,
438-
cond: unsafe { unsafe::reinterpret_cast(cond) } };
439-
// FIXME(#2282) region variance would avoid having to cast
416+
cond: cond };
440417
blk(data, &cvar)
441418
}
442419
}
@@ -449,9 +426,7 @@ impl<T: const send> &rw_read_mode<T> {
449426
fn read<U>(blk: fn(x: &T) -> U) -> U {
450427
match *self {
451428
rw_read_mode((data, ref token)) => {
452-
// FIXME(#2282) cast to avoid region invariance
453-
let mode = unsafe { unsafe::transmute_region(token) };
454-
do mode.read { blk(data) }
429+
do token.read { blk(data) }
455430
}
456431
}
457432
}
@@ -593,9 +568,7 @@ mod tests {
593568
let arc2 = ~arc.clone();
594569
do task::try {
595570
do arc2.write_downgrade |write_mode| {
596-
// FIXME(#2282)
597-
let mode = unsafe { unsafe::transmute_region(&write_mode) };
598-
do mode.write |one| {
571+
do (&write_mode).write |one| {
599572
assert *one == 2;
600573
}
601574
}
@@ -637,9 +610,7 @@ mod tests {
637610
do task::try {
638611
do arc2.write_downgrade |write_mode| {
639612
let read_mode = arc2.downgrade(write_mode);
640-
// FIXME(#2282)
641-
let mode = unsafe { unsafe::transmute_region(&read_mode) };
642-
do mode.read |one| {
613+
do (&read_mode).read |one| {
643614
assert *one == 2;
644615
}
645616
}
@@ -728,9 +699,7 @@ mod tests {
728699

729700
// Downgrader (us)
730701
do arc.write_downgrade |write_mode| {
731-
// FIXME(#2282)
732-
let mode = unsafe { unsafe::transmute_region(&write_mode) };
733-
do mode.write_cond |state, cond| {
702+
do (&write_mode).write_cond |state, cond| {
734703
wc1.send(()); // send to another writer who will wake us up
735704
while *state == 0 {
736705
cond.wait();
@@ -745,9 +714,7 @@ mod tests {
745714
}
746715
}
747716
let read_mode = arc.downgrade(write_mode);
748-
// FIXME(#2282)
749-
let mode = unsafe { unsafe::transmute_region(&read_mode) };
750-
do mode.read |state| {
717+
do (&read_mode).read |state| {
751718
// complete handshake with other readers
752719
for vec::each(reader_convos) |x| {
753720
match x {

src/libstd/sync.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -998,16 +998,12 @@ mod tests {
998998
write => x.write(blk),
999999
downgrade =>
10001000
do x.write_downgrade |mode| {
1001-
// FIXME(#2282)
1002-
let mode = unsafe { unsafe::transmute_region(&mode) };
1003-
mode.write(blk);
1001+
(&mode).write(blk);
10041002
},
10051003
downgrade_read =>
10061004
do x.write_downgrade |mode| {
10071005
let mode = x.downgrade(mode);
1008-
// FIXME(#2282)
1009-
let mode = unsafe { unsafe::transmute_region(&mode) };
1010-
mode.read(blk);
1006+
(&mode).read(blk);
10111007
},
10121008
}
10131009
}
@@ -1152,9 +1148,7 @@ mod tests {
11521148
fn lock_cond(x: &rwlock, downgrade: bool, blk: fn(c: &condvar)) {
11531149
if downgrade {
11541150
do x.write_downgrade |mode| {
1155-
// FIXME(#2282)
1156-
let mode = unsafe { unsafe::transmute_region(&mode) };
1157-
mode.write_cond(blk)
1151+
(&mode).write_cond(blk)
11581152
}
11591153
} else {
11601154
x.write_cond(blk)

0 commit comments

Comments
 (0)