Skip to content

Commit 9e1c9be

Browse files
committed
librustc: Make the Drop trait use explicit self
1 parent 5a282ec commit 9e1c9be

File tree

91 files changed

+166
-123
lines changed

Some content is hidden

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

91 files changed

+166
-123
lines changed

src/libcore/ops.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub trait Owned {
2525

2626
#[lang="drop"]
2727
pub trait Drop {
28-
fn finalize(); // XXX: Rename to "drop"? --pcwalton
28+
fn finalize(&self); // XXX: Rename to "drop"? --pcwalton
2929
}
3030

3131
#[lang="add"]

src/librustc/middle/trans/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ fn with_field_tys<R>(tcx: ty::ctxt,
888888
}
889889

890890
ty::ty_class(did, ref substs) => {
891-
let has_dtor = ty::ty_dtor(tcx, did).is_some();
891+
let has_dtor = ty::ty_dtor(tcx, did).is_present();
892892
op(has_dtor, class_items_as_mutable_fields(tcx, did, substs))
893893
}
894894

src/librustc/middle/trans/glue.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,14 @@ fn make_free_glue(bcx: block, v: ValueRef, t: ty::t) {
397397
}
398398
ty::ty_class(did, ref substs) => {
399399
// Call the dtor if there is one
400-
do option::map_default(&ty::ty_dtor(bcx.tcx(), did), bcx) |dt_id| {
401-
trans_class_drop(bcx, v, *dt_id, did, substs)
400+
match ty::ty_dtor(bcx.tcx(), did) {
401+
ty::NoDtor => bcx,
402+
ty::LegacyDtor(ref dt_id) => {
403+
trans_class_drop(bcx, v, *dt_id, did, substs, false)
404+
}
405+
ty::TraitDtor(ref dt_id) => {
406+
trans_class_drop(bcx, v, *dt_id, did, substs, true)
407+
}
402408
}
403409
}
404410
_ => bcx
@@ -410,7 +416,8 @@ fn trans_class_drop(bcx: block,
410416
v0: ValueRef,
411417
dtor_did: ast::def_id,
412418
class_did: ast::def_id,
413-
substs: &ty::substs) -> block {
419+
substs: &ty::substs,
420+
take_ref: bool) -> block {
414421
let drop_flag = GEPi(bcx, v0, struct_dtor());
415422
do with_cond(bcx, IsNotNull(bcx, Load(bcx, drop_flag))) |cx| {
416423
let mut bcx = cx;
@@ -427,7 +434,18 @@ fn trans_class_drop(bcx: block,
427434
// just consist of the output pointer and the environment
428435
// (self)
429436
assert(params.len() == 2);
430-
let self_arg = PointerCast(bcx, v0, params[1]);
437+
438+
// If we need to take a reference to the class (because it's using
439+
// the Drop trait), do so now.
440+
let llval;
441+
if take_ref {
442+
llval = alloca(bcx, val_ty(v0));
443+
Store(bcx, v0, llval);
444+
} else {
445+
llval = v0;
446+
}
447+
448+
let self_arg = PointerCast(bcx, llval, params[1]);
431449
let args = ~[bcx.fcx.llretptr, self_arg];
432450
Call(bcx, dtor_addr, args);
433451

@@ -465,10 +483,13 @@ fn make_drop_glue(bcx: block, v0: ValueRef, t: ty::t) {
465483
ty::ty_class(did, ref substs) => {
466484
let tcx = bcx.tcx();
467485
match ty::ty_dtor(tcx, did) {
468-
Some(dtor) => {
469-
trans_class_drop(bcx, v0, dtor, did, substs)
486+
ty::TraitDtor(dtor) => {
487+
trans_class_drop(bcx, v0, dtor, did, substs, true)
470488
}
471-
None => {
489+
ty::LegacyDtor(dtor) => {
490+
trans_class_drop(bcx, v0, dtor, did, substs, false)
491+
}
492+
ty::NoDtor => {
472493
// No dtor? Just the default case
473494
iter_structural_ty(bcx, v0, t, drop_ty)
474495
}

src/librustc/middle/trans/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn simplify_type(tcx: ty::ctxt, typ: ty::t) -> ty::t {
2727
// Reduce a class type to a record type in which all the fields are
2828
// simplified
2929
ty::ty_class(did, ref substs) => {
30-
let simpl_fields = (if ty::ty_dtor(tcx, did).is_some() {
30+
let simpl_fields = (if ty::ty_dtor(tcx, did).is_present() {
3131
// remember the drop flag
3232
~[{ident: syntax::parse::token::special_idents::dtor,
3333
mt: {ty: ty::mk_u8(tcx),

src/librustc/middle/trans/type_of.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ fn type_of(cx: @crate_ctxt, t: ty::t) -> TypeRef {
199199

200200
// include a byte flag if there is a dtor so that we know when we've
201201
// been dropped
202-
if ty::ty_dtor(cx.tcx, did) != None {
202+
if ty::ty_dtor(cx.tcx, did).is_present() {
203203
common::set_struct_body(llty, ~[T_struct(tys), T_i8()]);
204204
} else {
205205
common::set_struct_body(llty, ~[T_struct(tys)]);

src/librustc/middle/ty.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export enum_variants, substd_enum_variants, enum_is_univariant;
7575
export trait_methods, store_trait_methods, impl_traits;
7676
export enum_variant_with_id;
7777
export ty_dtor;
78+
export DtorKind, NoDtor, LegacyDtor, TraitDtor;
7879
export ty_param_bounds_and_ty;
7980
export ty_param_substs_and_ty;
8081
export ty_bool, mk_bool, type_is_bool;
@@ -1868,7 +1869,7 @@ fn type_needs_drop(cx: ctxt, ty: t) -> bool {
18681869
}
18691870
ty_class(did, ref substs) => {
18701871
// Any class with a dtor needs a drop
1871-
ty_dtor(cx, did).is_some() || {
1872+
ty_dtor(cx, did).is_present() || {
18721873
for vec::each(ty::class_items_as_fields(cx, did, substs)) |f| {
18731874
if type_needs_drop(cx, f.mt.ty) { accum = true; }
18741875
}
@@ -3954,11 +3955,29 @@ fn item_path_str(cx: ctxt, id: ast::def_id) -> ~str {
39543955
ast_map::path_to_str(item_path(cx, id), cx.sess.parse_sess.interner)
39553956
}
39563957

3958+
enum DtorKind {
3959+
NoDtor,
3960+
LegacyDtor(def_id),
3961+
TraitDtor(def_id)
3962+
}
3963+
3964+
impl DtorKind {
3965+
pure fn is_not_present(&const self) -> bool {
3966+
match *self {
3967+
NoDtor => true,
3968+
_ => false
3969+
}
3970+
}
3971+
pure fn is_present(&const self) -> bool {
3972+
!self.is_not_present()
3973+
}
3974+
}
3975+
39573976
/* If class_id names a class with a dtor, return Some(the dtor's id).
39583977
Otherwise return none. */
3959-
fn ty_dtor(cx: ctxt, class_id: def_id) -> Option<def_id> {
3978+
fn ty_dtor(cx: ctxt, class_id: def_id) -> DtorKind {
39603979
match cx.destructor_for_type.find(class_id) {
3961-
Some(method_def_id) => return Some(method_def_id),
3980+
Some(method_def_id) => return TraitDtor(method_def_id),
39623981
None => {} // Continue.
39633982
}
39643983

@@ -3968,18 +3987,21 @@ fn ty_dtor(cx: ctxt, class_id: def_id) -> Option<def_id> {
39683987
node: ast::item_class(@{ dtor: Some(dtor), _ }, _),
39693988
_
39703989
}, _)) =>
3971-
Some(local_def(dtor.node.id)),
3990+
LegacyDtor(local_def(dtor.node.id)),
39723991
_ =>
3973-
None
3992+
NoDtor
39743993
}
39753994
}
39763995
else {
3977-
csearch::class_dtor(cx.sess.cstore, class_id)
3996+
match csearch::class_dtor(cx.sess.cstore, class_id) {
3997+
None => NoDtor,
3998+
Some(did) => LegacyDtor(did),
3999+
}
39784000
}
39794001
}
39804002

39814003
fn has_dtor(cx: ctxt, class_id: def_id) -> bool {
3982-
ty_dtor(cx, class_id).is_some()
4004+
ty_dtor(cx, class_id).is_present()
39834005
}
39844006

39854007
fn item_path(cx: ctxt, id: ast::def_id) -> ast_map::path {

src/libstd/arc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ struct PoisonOnFail {
229229
}
230230
231231
impl PoisonOnFail : Drop {
232-
fn finalize() {
232+
fn finalize(&self) {
233233
/* assert !*self.failed; -- might be false in case of cond.wait() */
234234
if task::failing() { *self.failed = true; }
235235
}

src/libstd/arena.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub struct Arena {
5858
}
5959

6060
impl Arena : Drop {
61-
fn finalize() {
61+
fn finalize(&self) {
6262
unsafe {
6363
destroy_chunk(&self.head);
6464
for list::each(self.chunks) |chunk| {

src/libstd/c_vec.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ struct DtorRes {
4242
}
4343

4444
impl DtorRes : Drop {
45-
fn finalize() {
46-
match self.dtor {
47-
option::None => (),
48-
option::Some(f) => f()
49-
}
45+
fn finalize(&self) {
46+
match self.dtor {
47+
option::None => (),
48+
option::Some(f) => f()
49+
}
5050
}
5151
}
5252

src/libstd/future.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub struct Future<A> {
2828
// FIXME(#2829) -- futures should not be copyable, because they close
2929
// over fn~'s that have pipes and so forth within!
3030
impl<A> Future<A> : Drop {
31-
fn finalize() {}
31+
fn finalize(&self) {}
3232
}
3333

3434
priv enum FutureState<A> {

src/libstd/net_tcp.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ struct TcpSocket {
3030
}
3131

3232
impl TcpSocket : Drop {
33-
fn finalize() {
34-
unsafe {
35-
tear_down_socket_data(self.socket_data)
36-
}
33+
fn finalize(&self) {
34+
unsafe {
35+
tear_down_socket_data(self.socket_data)
36+
}
3737
}
3838
}
3939

src/libstd/sort.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ mod big_tests {
11371137
}
11381138

11391139
impl LVal : Drop {
1140-
fn finalize() {
1140+
fn finalize(&self) {
11411141
let x = unsafe { task::local_data::local_data_get(self.key) };
11421142
match x {
11431143
Some(@y) => {

src/libstd/sync.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ struct SemRelease {
153153
}
154154

155155
impl SemRelease : Drop {
156-
fn finalize() {
156+
fn finalize(&self) {
157157
self.sem.release();
158158
}
159159
}
@@ -170,7 +170,7 @@ struct SemAndSignalRelease {
170170
}
171171

172172
impl SemAndSignalRelease : Drop {
173-
fn finalize() {
173+
fn finalize(&self) {
174174
self.sem.release();
175175
}
176176
}
@@ -185,7 +185,7 @@ fn SemAndSignalRelease(sem: &r/Sem<~[mut Waitqueue]>)
185185
/// A mechanism for atomic-unlock-and-deschedule blocking and signalling.
186186
pub struct Condvar { priv sem: &Sem<~[mut Waitqueue]> }
187187

188-
impl Condvar : Drop { fn finalize() {} }
188+
impl Condvar : Drop { fn finalize(&self) {} }
189189

190190
impl &Condvar {
191191
/**
@@ -257,7 +257,7 @@ impl &Condvar {
257257
}
258258

259259
impl SemAndSignalReacquire : Drop {
260-
fn finalize() {
260+
fn finalize(&self) {
261261
unsafe {
262262
// Needs to succeed, instead of itself dying.
263263
do task::unkillable {
@@ -607,7 +607,7 @@ struct RWlockReleaseRead {
607607
}
608608
609609
impl RWlockReleaseRead : Drop {
610-
fn finalize() {
610+
fn finalize(&self) {
611611
unsafe {
612612
do task::unkillable {
613613
let mut last_reader = false;
@@ -641,7 +641,7 @@ struct RWlockReleaseDowngrade {
641641
}
642642
643643
impl RWlockReleaseDowngrade : Drop {
644-
fn finalize() {
644+
fn finalize(&self) {
645645
unsafe {
646646
do task::unkillable {
647647
let mut writer_or_last_reader = false;
@@ -678,10 +678,10 @@ fn RWlockReleaseDowngrade(lock: &r/RWlock) -> RWlockReleaseDowngrade/&r {
678678
679679
/// The "write permission" token used for rwlock.write_downgrade().
680680
pub struct RWlockWriteMode { /* priv */ lock: &RWlock }
681-
impl RWlockWriteMode : Drop { fn finalize() {} }
681+
impl RWlockWriteMode : Drop { fn finalize(&self) {} }
682682
/// The "read permission" token used for rwlock.write_downgrade().
683683
pub struct RWlockReadMode { priv lock: &RWlock }
684-
impl RWlockReadMode : Drop { fn finalize() {} }
684+
impl RWlockReadMode : Drop { fn finalize(&self) {} }
685685

686686
impl &RWlockWriteMode {
687687
/// Access the pre-downgrade rwlock in write mode.
@@ -993,7 +993,7 @@ mod tests {
993993
}
994994

995995
impl SendOnFailure : Drop {
996-
fn finalize() {
996+
fn finalize(&self) {
997997
self.c.send(());
998998
}
999999
}

src/test/auxiliary/issue-2526.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct arc_destruct<T:Const> {
1212
}
1313

1414
impl<T:Const> arc_destruct<T> : Drop {
15-
fn finalize() {}
15+
fn finalize(&self) {}
1616
}
1717

1818
fn arc_destruct<T: Const>(data: int) -> arc_destruct<T> {
@@ -34,7 +34,7 @@ struct context_res {
3434
}
3535

3636
impl context_res : Drop {
37-
fn finalize() {}
37+
fn finalize(&self) {}
3838
}
3939

4040
fn context_res() -> context_res {

src/test/auxiliary/issue-3012-1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct socket_handle {
1212
}
1313

1414
impl socket_handle : Drop {
15-
fn finalize() {
15+
fn finalize(&self) {
1616
/* c::close(self.sockfd); */
1717
}
1818
}

src/test/auxiliary/issue2170lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct rsrc {
88
}
99

1010
impl rsrc : Drop {
11-
fn finalize() {
11+
fn finalize(&self) {
1212
foo(self.x);
1313
}
1414
}

src/test/auxiliary/test_comm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct port_ptr<T:Send> {
3232
}
3333

3434
impl<T:Send> port_ptr<T> : Drop {
35-
fn finalize() {
35+
fn finalize(&self) {
3636
unsafe {
3737
debug!("in the port_ptr destructor");
3838
do task::unkillable {

src/test/bench/task-perf-alloc-unwind.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct r {
4545
}
4646

4747
impl r : Drop {
48-
fn finalize() {}
48+
fn finalize(&self) {}
4949
}
5050

5151
fn r(l: @nillist) -> r {

src/test/compile-fail/bind-by-move-neither-can-live-while-the-other-survives-1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
struct X { x: () }
22

33
impl X : Drop {
4-
fn finalize() {
4+
fn finalize(&self) {
55
error!("destructor runs");
66
}
77
}

src/test/compile-fail/bind-by-move-neither-can-live-while-the-other-survives-2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
struct X { x: (), }
22

33
impl X : Drop {
4-
fn finalize() {
4+
fn finalize(&self) {
55
error!("destructor runs");
66
}
77
}

src/test/compile-fail/bind-by-move-neither-can-live-while-the-other-survives-3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
struct X { x: (), }
22

33
impl X : Drop {
4-
fn finalize() {
4+
fn finalize(&self) {
55
error!("destructor runs");
66
}
77
}

0 commit comments

Comments
 (0)