Skip to content

Commit 4c08a8d

Browse files
committed
Removing more unnecessary unsafe blocks throughout
1 parent c089a17 commit 4c08a8d

File tree

8 files changed

+83
-97
lines changed

8 files changed

+83
-97
lines changed

src/libcore/gc.rs

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -231,66 +231,64 @@ unsafe fn walk_gc_roots(mem: Memory, sentinel: **Word, visitor: Visitor) {
231231
// the stack.
232232
let mut reached_sentinel = ptr::is_null(sentinel);
233233
for stackwalk::walk_stack |frame| {
234-
unsafe {
235-
let pc = last_ret;
236-
let Segment {segment: next_segment, boundary: boundary} =
237-
find_segment_for_frame(frame.fp, segment);
238-
segment = next_segment;
239-
// Each stack segment is bounded by a morestack frame. The
240-
// morestack frame includes two return addresses, one for
241-
// morestack itself, at the normal offset from the frame
242-
// pointer, and then a second return address for the
243-
// function prologue (which called morestack after
244-
// determining that it had hit the end of the stack).
245-
// Since morestack itself takes two parameters, the offset
246-
// for this second return address is 3 greater than the
247-
// return address for morestack.
248-
let ret_offset = if boundary { 4 } else { 1 };
249-
last_ret = *ptr::offset(frame.fp, ret_offset) as *Word;
250-
251-
if ptr::is_null(pc) {
252-
loop;
253-
}
234+
let pc = last_ret;
235+
let Segment {segment: next_segment, boundary: boundary} =
236+
find_segment_for_frame(frame.fp, segment);
237+
segment = next_segment;
238+
// Each stack segment is bounded by a morestack frame. The
239+
// morestack frame includes two return addresses, one for
240+
// morestack itself, at the normal offset from the frame
241+
// pointer, and then a second return address for the
242+
// function prologue (which called morestack after
243+
// determining that it had hit the end of the stack).
244+
// Since morestack itself takes two parameters, the offset
245+
// for this second return address is 3 greater than the
246+
// return address for morestack.
247+
let ret_offset = if boundary { 4 } else { 1 };
248+
last_ret = *ptr::offset(frame.fp, ret_offset) as *Word;
249+
250+
if ptr::is_null(pc) {
251+
loop;
252+
}
254253

255-
let mut delay_reached_sentinel = reached_sentinel;
256-
let sp = is_safe_point(pc);
257-
match sp {
258-
Some(sp_info) => {
259-
for walk_safe_point(frame.fp, sp_info) |root, tydesc| {
260-
// Skip roots until we see the sentinel.
261-
if !reached_sentinel {
262-
if root == sentinel {
263-
delay_reached_sentinel = true;
264-
}
265-
loop;
254+
let mut delay_reached_sentinel = reached_sentinel;
255+
let sp = is_safe_point(pc);
256+
match sp {
257+
Some(sp_info) => {
258+
for walk_safe_point(frame.fp, sp_info) |root, tydesc| {
259+
// Skip roots until we see the sentinel.
260+
if !reached_sentinel {
261+
if root == sentinel {
262+
delay_reached_sentinel = true;
266263
}
264+
loop;
265+
}
267266

268-
// Skip null pointers, which can occur when a
269-
// unique pointer has already been freed.
270-
if ptr::is_null(*root) {
271-
loop;
272-
}
267+
// Skip null pointers, which can occur when a
268+
// unique pointer has already been freed.
269+
if ptr::is_null(*root) {
270+
loop;
271+
}
273272

274-
if ptr::is_null(tydesc) {
275-
// Root is a generic box.
276-
let refcount = **root;
277-
if mem | task_local_heap != 0 && refcount != -1 {
278-
if !visitor(root, tydesc) { return; }
279-
} else if mem | exchange_heap != 0 && refcount == -1 {
280-
if !visitor(root, tydesc) { return; }
281-
}
282-
} else {
283-
// Root is a non-immediate.
284-
if mem | stack != 0 {
285-
if !visitor(root, tydesc) { return; }
286-
}
273+
if ptr::is_null(tydesc) {
274+
// Root is a generic box.
275+
let refcount = **root;
276+
if mem | task_local_heap != 0 && refcount != -1 {
277+
if !visitor(root, tydesc) { return; }
278+
} else if mem | exchange_heap != 0 && refcount == -1 {
279+
if !visitor(root, tydesc) { return; }
280+
}
281+
} else {
282+
// Root is a non-immediate.
283+
if mem | stack != 0 {
284+
if !visitor(root, tydesc) { return; }
287285
}
288286
}
289-
}
290-
None => ()
291287
}
292-
reached_sentinel = delay_reached_sentinel;
288+
}
289+
None => ()
293290
}
291+
reached_sentinel = delay_reached_sentinel;
294292
}
295293
}
296294

src/libcore/pipes.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,7 @@ pub impl PacketHeader {
156156
unsafe fn unblock(&self) {
157157
let old_task = swap_task(&mut self.blocked_task, ptr::null());
158158
if !old_task.is_null() {
159-
unsafe {
160-
rustrt::rust_task_deref(old_task)
161-
}
159+
rustrt::rust_task_deref(old_task)
162160
}
163161
match swap_state_acq(&mut self.state, Empty) {
164162
Empty | Blocked => (),

src/libcore/rt/sched/local_sched.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,8 @@ pub unsafe fn unsafe_borrow() -> &mut Scheduler {
8080
}
8181

8282
pub unsafe fn unsafe_borrow_io() -> &mut IoFactoryObject {
83-
unsafe {
84-
let sched = unsafe_borrow();
85-
return sched.event_loop.io().unwrap();
86-
}
83+
let sched = unsafe_borrow();
84+
return sched.event_loop.io().unwrap();
8785
}
8886

8987
fn tls_key() -> tls::Key {

src/libcore/rt/uvll.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub enum uv_req_type {
9898

9999
pub unsafe fn malloc_handle(handle: uv_handle_type) -> *c_void {
100100
assert!(handle != UV_UNKNOWN_HANDLE && handle != UV_HANDLE_TYPE_MAX);
101-
let size = unsafe { rust_uv_handle_size(handle as uint) };
101+
let size = rust_uv_handle_size(handle as uint);
102102
let p = malloc(size);
103103
assert!(p.is_not_null());
104104
return p;
@@ -110,7 +110,7 @@ pub unsafe fn free_handle(v: *c_void) {
110110

111111
pub unsafe fn malloc_req(req: uv_req_type) -> *c_void {
112112
assert!(req != UV_UNKNOWN_REQ && req != UV_REQ_TYPE_MAX);
113-
let size = unsafe { rust_uv_req_size(req as uint) };
113+
let size = rust_uv_req_size(req as uint);
114114
let p = malloc(size);
115115
assert!(p.is_not_null());
116116
return p;

src/libcore/unstable.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -262,18 +262,16 @@ pub impl<T:Owned> Exclusive<T> {
262262
// the exclusive. Supporting that is a work in progress.
263263
#[inline(always)]
264264
unsafe fn with<U>(&self, f: &fn(x: &mut T) -> U) -> U {
265-
unsafe {
266-
let rec = get_shared_mutable_state(&self.x);
267-
do (*rec).lock.lock {
268-
if (*rec).failed {
269-
fail!(
270-
~"Poisoned exclusive - another task failed inside!");
271-
}
272-
(*rec).failed = true;
273-
let result = f(&mut (*rec).data);
274-
(*rec).failed = false;
275-
result
265+
let rec = get_shared_mutable_state(&self.x);
266+
do (*rec).lock.lock {
267+
if (*rec).failed {
268+
fail!(
269+
~"Poisoned exclusive - another task failed inside!");
276270
}
271+
(*rec).failed = true;
272+
let result = f(&mut (*rec).data);
273+
(*rec).failed = false;
274+
result
277275
}
278276
}
279277

src/libcore/unstable/weak_task.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ pub unsafe fn weaken_task(f: &fn(Port<ShutdownMsg>)) {
4343
let task = get_task_id();
4444
// Expect the weak task service to be alive
4545
assert!(service.try_send(RegisterWeakTask(task, shutdown_chan)));
46-
unsafe { rust_dec_kernel_live_count(); }
46+
rust_dec_kernel_live_count();
4747
do (|| {
4848
f(shutdown_port.take())
4949
}).finally || {
50-
unsafe { rust_inc_kernel_live_count(); }
50+
rust_inc_kernel_live_count();
5151
// Service my have already exited
5252
service.send(UnregisterWeakTask(task));
5353
}

src/librustc/middle/trans/base.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,13 +2628,11 @@ pub fn get_item_val(ccx: @CrateContext, id: ast::node_id) -> ValueRef {
26282628
let class_ty = ty::lookup_item_type(tcx, parent_id).ty;
26292629
// This code shouldn't be reached if the class is generic
26302630
assert!(!ty::type_has_params(class_ty));
2631-
let lldty = unsafe {
2632-
T_fn(~[
2631+
let lldty = T_fn(~[
26332632
T_ptr(T_i8()),
26342633
T_ptr(type_of(ccx, class_ty))
26352634
],
2636-
T_nil())
2637-
};
2635+
T_nil());
26382636
let s = get_dtor_symbol(ccx, /*bad*/copy *pt, dt.node.id, None);
26392637
26402638
/* Make the declaration for the dtor */

src/libstd/arc.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,13 @@ pub impl<T:Owned> MutexARC<T> {
177177
*/
178178
#[inline(always)]
179179
unsafe fn access<U>(&self, blk: &fn(x: &mut T) -> U) -> U {
180-
unsafe {
181-
let state = get_shared_mutable_state(&self.x);
182-
// Borrowck would complain about this if the function were
183-
// not already unsafe. See borrow_rwlock, far below.
184-
do (&(*state).lock).lock {
185-
check_poison(true, (*state).failed);
186-
let _z = PoisonOnFail(&mut (*state).failed);
187-
blk(&mut (*state).data)
188-
}
180+
let state = get_shared_mutable_state(&self.x);
181+
// Borrowck would complain about this if the function were
182+
// not already unsafe. See borrow_rwlock, far below.
183+
do (&(*state).lock).lock {
184+
check_poison(true, (*state).failed);
185+
let _z = PoisonOnFail(&mut (*state).failed);
186+
blk(&mut (*state).data)
189187
}
190188
}
191189

@@ -195,16 +193,14 @@ pub impl<T:Owned> MutexARC<T> {
195193
&self,
196194
blk: &fn(x: &'x mut T, c: &'c Condvar) -> U) -> U
197195
{
198-
unsafe {
199-
let state = get_shared_mutable_state(&self.x);
200-
do (&(*state).lock).lock_cond |cond| {
201-
check_poison(true, (*state).failed);
202-
let _z = PoisonOnFail(&mut (*state).failed);
203-
blk(&mut (*state).data,
204-
&Condvar {is_mutex: true,
205-
failed: &mut (*state).failed,
206-
cond: cond })
207-
}
196+
let state = get_shared_mutable_state(&self.x);
197+
do (&(*state).lock).lock_cond |cond| {
198+
check_poison(true, (*state).failed);
199+
let _z = PoisonOnFail(&mut (*state).failed);
200+
blk(&mut (*state).data,
201+
&Condvar {is_mutex: true,
202+
failed: &mut (*state).failed,
203+
cond: cond })
208204
}
209205
}
210206
}

0 commit comments

Comments
 (0)