Skip to content

Commit 706096b

Browse files
committed
auto merge of rust-lang#6038 : alexcrichton/rust/more-unsafe, r=pcwalton
Because unsafe functions are never warned about, then all `unsafe` blocks in unsafe functions should definitely be warned about (no need to be redundant). This fixes this case, adds tests, cleans up remaining cases, and then fixes a few other import warnings being spit out.
2 parents 8708e0c + 0c2ab66 commit 706096b

File tree

15 files changed

+160
-144
lines changed

15 files changed

+160
-144
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/uvio.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
use option::*;
1212
use result::*;
1313

14-
use super::io::net::ip::{IpAddr, Ipv4}; // n.b. Ipv4 is used only in tests
14+
use super::io::net::ip::IpAddr;
1515
use super::uv::*;
1616
use super::rtio::*;
1717
use ops::Drop;
1818
use cell::{Cell, empty_cell};
1919
use cast::transmute;
2020
use super::sched::{Scheduler, local_sched};
2121

22+
#[cfg(test)] use super::io::net::ip::Ipv4;
2223
#[cfg(test)] use super::sched::Task;
2324
#[cfg(test)] use unstable::run_in_bare_thread;
2425
#[cfg(test)] use uint;

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/str/ascii.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ impl ToStrConsume for ~[Ascii] {
196196
}
197197
}
198198

199+
#[cfg(test)]
199200
mod tests {
200201
use super::*;
201202

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/back/link.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use core::char;
2626
use core::hash::Streaming;
2727
use core::hash;
2828
use core::io::WriterUtil;
29-
use core::libc::{c_int, c_uint, c_char};
29+
use core::libc::{c_int, c_uint};
3030
use core::os::consts::{macos, freebsd, linux, android, win32};
3131
use core::os;
3232
use core::ptr;

src/librustc/middle/borrowck/check_loans.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use core::prelude::*;
2121

2222
use middle::moves;
23+
use middle::typeck::check::PurityState;
2324
use middle::borrowck::{Loan, bckerr, BorrowckCtxt, inherent_mutability};
2425
use middle::borrowck::{ReqMaps, root_map_key, save_and_restore_managed};
2526
use middle::borrowck::{MoveError, MoveOk, MoveFromIllegalCmt};
@@ -41,11 +42,6 @@ use syntax::codemap::span;
4142
use syntax::print::pprust;
4243
use syntax::visit;
4344

44-
struct PurityState {
45-
def: ast::node_id,
46-
purity: ast::purity
47-
}
48-
4945
struct CheckLoanCtxt {
5046
bccx: @BorrowckCtxt,
5147
req_maps: ReqMaps,
@@ -85,8 +81,7 @@ pub fn check_loans(bccx: @BorrowckCtxt,
8581
bccx: bccx,
8682
req_maps: req_maps,
8783
reported: HashSet::new(),
88-
declared_purity: @mut PurityState { purity: ast::impure_fn,
89-
def: 0 },
84+
declared_purity: @mut PurityState::function(ast::impure_fn, 0),
9085
fn_args: @mut @~[]
9186
};
9287
let vt = visit::mk_vt(@visit::Visitor {visit_expr: check_loans_in_expr,
@@ -658,9 +653,7 @@ fn check_loans_in_fn(fk: &visit::fn_kind,
658653
debug!("purity on entry=%?", copy self.declared_purity);
659654
do save_and_restore_managed(self.declared_purity) {
660655
do save_and_restore_managed(self.fn_args) {
661-
self.declared_purity = @mut PurityState {
662-
purity: declared_purity, def: src
663-
};
656+
self.declared_purity = @mut PurityState::function(declared_purity, src);
664657
665658
match *fk {
666659
visit::fk_anon(*) |
@@ -810,17 +803,7 @@ fn check_loans_in_block(blk: &ast::blk,
810803
do save_and_restore_managed(self.declared_purity) {
811804
self.check_for_conflicting_loans(blk.node.id);
812805

813-
match blk.node.rules {
814-
ast::default_blk => {
815-
}
816-
ast::unsafe_blk => {
817-
*self.declared_purity = PurityState {
818-
purity: ast::unsafe_fn,
819-
def: blk.node.id,
820-
};
821-
}
822-
}
823-
806+
*self.declared_purity = self.declared_purity.recurse(blk);
824807
visit::visit_block(blk, self, vt);
825808
}
826809
}

src/librustc/middle/trans/base.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use core::hash;
7070
use core::hashmap::{HashMap, HashSet};
7171
use core::int;
7272
use core::io;
73-
use core::libc::{c_uint, c_ulonglong};
73+
use core::libc::c_uint;
7474
use core::uint;
7575
use std::time;
7676
use syntax::ast::ident;
@@ -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/librustc/middle/ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ pub struct substs {
514514
}
515515

516516
mod primitives {
517-
use super::{sty, t_box_};
517+
use super::t_box_;
518518

519519
use syntax::ast;
520520

0 commit comments

Comments
 (0)