Skip to content

Commit c705440

Browse files
committed
auto merge of #5155 : bstrie/rust/dedrop, r=pcwalton
This removes all but 6 uses of `drop {}` from the entire codebase. Removing any of the remaining uses causes various non-trivial bugs; I'll start reporting them once this gets merged.
2 parents fcd4af1 + 43d43ad commit c705440

22 files changed

+117
-29
lines changed

src/libcore/io.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,10 @@ impl<R:Reader,C> Reader for Wrapper<R, C> {
474474

475475
pub struct FILERes {
476476
f: *libc::FILE,
477-
drop {
477+
}
478+
479+
impl Drop for FILERes {
480+
fn finalize(&self) {
478481
unsafe {
479482
libc::fclose(self.f);
480483
}
@@ -683,7 +686,10 @@ impl Writer for fd_t {
683686

684687
pub struct FdRes {
685688
fd: fd_t,
686-
drop {
689+
}
690+
691+
impl Drop for FdRes {
692+
fn finalize(&self) {
687693
unsafe {
688694
libc::close(self.fd);
689695
}

src/libcore/option.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,10 @@ fn test_unwrap_str() {
450450
fn test_unwrap_resource() {
451451
struct R {
452452
i: @mut int,
453-
drop { *(self.i) += 1; }
453+
}
454+
455+
impl ::ops::Drop for R {
456+
fn finalize(&self) { *(self.i) += 1; }
454457
}
455458

456459
fn R(i: @mut int) -> R {

src/libcore/pipes.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,10 @@ pub unsafe fn get_buffer<T>(p: *PacketHeader) -> ~Buffer<T> {
346346
struct BufferResource<T> {
347347
buffer: ~Buffer<T>,
348348
349-
drop {
349+
}
350+
351+
impl<T> ::ops::Drop for BufferResource<T> {
352+
fn finalize(&self) {
350353
unsafe {
351354
let b = move_it!(self.buffer);
352355
//let p = ptr::addr_of(*b);

src/libcore/private.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ struct ArcData<T> {
126126

127127
struct ArcDestruct<T> {
128128
mut data: *libc::c_void,
129-
drop {
129+
}
130+
131+
impl<T> Drop for ArcDestruct<T>{
132+
fn finalize(&self) {
130133
unsafe {
131134
if self.data.is_null() {
132135
return; // Happens when destructing an unwrapper's handle.
@@ -178,7 +181,10 @@ pub unsafe fn unwrap_shared_mutable_state<T:Owned>(rc: SharedMutableState<T>)
178181
struct DeathThroes<T> {
179182
mut ptr: Option<~ArcData<T>>,
180183
mut response: Option<comm::ChanOne<bool>>,
181-
drop {
184+
}
185+
186+
impl<T> Drop for DeathThroes<T>{
187+
fn finalize(&self) {
182188
unsafe {
183189
let response = option::swap_unwrap(&mut self.response);
184190
// In case we get killed early, we need to tell the person who
@@ -311,7 +317,10 @@ type rust_little_lock = *libc::c_void;
311317
312318
struct LittleLock {
313319
l: rust_little_lock,
314-
drop {
320+
}
321+
322+
impl Drop for LittleLock {
323+
fn finalize(&self) {
315324
unsafe {
316325
rustrt::rust_destroy_little_lock(self.l);
317326
}

src/libcore/rand.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,10 @@ impl Rng {
365365

366366
struct RandRes {
367367
rng: *rust_rng,
368-
drop {
368+
}
369+
370+
impl Drop for RandRes {
371+
fn finalize(&self) {
369372
unsafe {
370373
rustrt::rand_free(self.rng);
371374
}

src/libcore/run.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,10 @@ pub fn start_program(prog: &str, args: &[~str]) -> Program {
248248
}
249249
struct ProgRes {
250250
r: ProgRepr,
251-
drop {
251+
}
252+
253+
impl Drop for ProgRes {
254+
fn finalize(&self) {
252255
unsafe {
253256
// FIXME #4943: This is bad.
254257
destroy_repr(cast::transmute(&self.r));

src/libcore/task/spawn.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,11 @@ struct TCB {
308308
mut ancestors: AncestorList,
309309
is_main: bool,
310310
notifier: Option<AutoNotify>,
311+
}
312+
313+
impl Drop for TCB {
311314
// Runs on task exit.
312-
drop {
315+
fn finalize(&self) {
313316
unsafe {
314317
// If we are failing, the whole taskgroup needs to die.
315318
if rt::rust_task_is_unwinding(self.me) {
@@ -353,7 +356,10 @@ fn TCB(me: *rust_task, tasks: TaskGroupArc, ancestors: AncestorList,
353356
struct AutoNotify {
354357
notify_chan: Chan<TaskResult>,
355358
mut failed: bool,
356-
drop {
359+
}
360+
361+
impl Drop for AutoNotify {
362+
fn finalize(&self) {
357363
let result = if self.failed { Failure } else { Success };
358364
self.notify_chan.send(result);
359365
}

src/libcore/util.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ pub fn replace<T>(dest: &mut T, src: T) -> T {
6666
/// A non-copyable dummy type.
6767
pub struct NonCopyable {
6868
i: (),
69-
drop { }
69+
}
70+
71+
impl Drop for NonCopyable {
72+
fn finalize(&self) { }
7073
}
7174

7275
pub fn NonCopyable() -> NonCopyable { NonCopyable { i: () } }

src/librustc/lib/llvm.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -1458,7 +1458,10 @@ pub fn struct_tys(struct_ty: TypeRef) -> ~[TypeRef] {
14581458

14591459
pub struct target_data_res {
14601460
TD: TargetDataRef,
1461-
drop {
1461+
}
1462+
1463+
impl Drop for target_data_res {
1464+
fn finalize(&self) {
14621465
unsafe {
14631466
llvm::LLVMDisposeTargetData(self.TD);
14641467
}
@@ -1492,7 +1495,10 @@ pub fn mk_target_data(string_rep: ~str) -> TargetData {
14921495

14931496
pub struct pass_manager_res {
14941497
PM: PassManagerRef,
1495-
drop {
1498+
}
1499+
1500+
impl Drop for pass_manager_res {
1501+
fn finalize(&self) {
14961502
unsafe {
14971503
llvm::LLVMDisposePassManager(self.PM);
14981504
}
@@ -1525,7 +1531,10 @@ pub fn mk_pass_manager() -> PassManager {
15251531

15261532
pub struct object_file_res {
15271533
ObjectFile: ObjectFileRef,
1528-
drop {
1534+
}
1535+
1536+
impl Drop for object_file_res {
1537+
fn finalize(&self) {
15291538
unsafe {
15301539
llvm::LLVMDisposeObjectFile(self.ObjectFile);
15311540
}
@@ -1559,7 +1568,10 @@ pub fn mk_object_file(llmb: MemoryBufferRef) -> Option<ObjectFile> {
15591568

15601569
pub struct section_iter_res {
15611570
SI: SectionIteratorRef,
1562-
drop {
1571+
}
1572+
1573+
impl Drop for section_iter_res {
1574+
fn finalize(&self) {
15631575
unsafe {
15641576
llvm::LLVMDisposeSectionIterator(self.SI);
15651577
}

src/librustc/middle/trans/base.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ use syntax::{ast, ast_util, codemap, ast_map};
9090

9191
pub struct icx_popper {
9292
ccx: @CrateContext,
93-
drop {
93+
}
94+
95+
impl Drop for icx_popper {
96+
fn finalize(&self) {
9497
if self.ccx.sess.count_llvm_insns() {
9598
self.ccx.stats.llvm_insn_ctxt.pop();
9699
}

src/librustc/middle/trans/common.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ pub struct Stats {
141141

142142
pub struct BuilderRef_res {
143143
B: BuilderRef,
144-
drop {
144+
}
145+
146+
impl Drop for BuilderRef_res {
147+
fn finalize(&self) {
145148
unsafe {
146149
llvm::LLVMDisposeBuilder(self.B);
147150
}

src/librustc/rustc.rc

+4-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,10 @@ pub fn monitor(+f: fn~(diagnostic::Emitter)) {
336336

337337
struct finally {
338338
ch: SharedChan<monitor_msg>,
339-
drop { self.ch.send(done); }
339+
}
340+
341+
impl Drop for finally {
342+
fn finalize(&self) { self.ch.send(done); }
340343
}
341344

342345
let _finally = finally { ch: ch };

src/librustc/util/common.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ pub fn indent<R>(op: fn() -> R) -> R {
3232

3333
pub struct _indenter {
3434
_i: (),
35-
drop { debug!("<<"); }
35+
}
36+
37+
impl Drop for _indenter {
38+
fn finalize(&self) { debug!("<<"); }
3639
}
3740

3841
pub fn _indenter(_i: ()) -> _indenter {

src/librustdoc/demo.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ mod blade_runner {
125125
*/
126126
struct Bored {
127127
bored: bool,
128-
drop { log(error, self.bored); }
128+
}
129+
130+
impl Drop for Bored {
131+
fn finalize(&self) { log(error, self.bored); }
129132
}
130133

131134
/**

src/libstd/task_pool.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ pub struct TaskPool<T> {
2828
channels: ~[Chan<Msg<T>>],
2929
mut next_index: uint,
3030

31-
drop {
31+
}
32+
33+
impl<T> Drop for TaskPool<T> {
34+
fn finalize(&self) {
3235
for self.channels.each |channel| {
3336
channel.send(Quit);
3437
}

src/libsyntax/parse/parser.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,11 @@ pub struct Parser {
245245
/// Used to determine the path to externally loaded source files
246246
mod_path_stack: @mut ~[~str],
247247

248-
drop {} /* do not copy the parser; its state is tied to outside state */
248+
}
249+
250+
impl Drop for Parser {
251+
/* do not copy the parser; its state is tied to outside state */
252+
fn finalize(&self) {}
249253
}
250254

251255
pub impl Parser {

src/test/auxiliary/moves_based_on_type_lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212

1313
pub struct S {
1414
x: int,
15-
drop {
15+
}
16+
17+
impl Drop for S {
18+
fn finalize(&self) {
1619
io::println("goodbye");
1720
}
1821
}

src/test/compile-fail/use-after-move-self-based-on-type.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
struct S {
22
x: int,
3-
drop {}
3+
}
4+
5+
impl Drop for S {
6+
fn finalize(&self) {}
47
}
58

69
impl S {

src/test/run-fail/too-much-recursion-unwinding.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ fn recurse() {
2121

2222
struct r {
2323
recursed: *mut bool,
24-
drop {
24+
}
25+
26+
impl Drop for r {
27+
fn finalize(&self) {
2528
unsafe {
2629
if !*(self.recursed) {
2730
*(self.recursed) = true;

src/test/run-fail/unwind-resource-fail.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212

1313
struct r {
1414
i: int,
15-
drop { fail!(~"squirrel") }
15+
}
16+
17+
impl Drop for r {
18+
fn finalize(&self) { fail!(~"squirrel") }
1619
}
1720

1821
fn r(i: int) -> r { r { i: i } }

src/test/run-fail/unwind-resource-fail2.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313

1414
struct r {
1515
i: int,
16-
drop { fail!(~"wombat") }
16+
}
17+
18+
impl Drop for r {
19+
fn finalize(&self) { fail!(~"wombat") }
1720
}
1821

1922
fn r(i: int) -> r { r { i: i } }

src/test/run-pass/issue-3563-3.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ struct AsciiArt
5454

5555
// This struct can be quite large so we'll disable copying: developers need
5656
// to either pass these structs around via borrowed pointers or move them.
57-
drop {}
57+
}
58+
59+
impl Drop for AsciiArt {
60+
fn finalize(&self) {}
5861
}
5962

6063
// It's common to define a constructor sort of function to create struct instances.

0 commit comments

Comments
 (0)