Skip to content

librustc: Make the Drop trait use explicit self #4058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libcore/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub trait Owned {

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

#[lang="add"]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ fn with_field_tys<R>(tcx: ty::ctxt,
}

ty::ty_class(did, ref substs) => {
let has_dtor = ty::ty_dtor(tcx, did).is_some();
let has_dtor = ty::ty_dtor(tcx, did).is_present();
op(has_dtor, class_items_as_mutable_fields(tcx, did, substs))
}

Expand Down
35 changes: 28 additions & 7 deletions src/librustc/middle/trans/glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,14 @@ fn make_free_glue(bcx: block, v: ValueRef, t: ty::t) {
}
ty::ty_class(did, ref substs) => {
// Call the dtor if there is one
do option::map_default(&ty::ty_dtor(bcx.tcx(), did), bcx) |dt_id| {
trans_class_drop(bcx, v, *dt_id, did, substs)
match ty::ty_dtor(bcx.tcx(), did) {
ty::NoDtor => bcx,
ty::LegacyDtor(ref dt_id) => {
trans_class_drop(bcx, v, *dt_id, did, substs, false)
}
ty::TraitDtor(ref dt_id) => {
trans_class_drop(bcx, v, *dt_id, did, substs, true)
}
}
}
_ => bcx
Expand All @@ -410,7 +416,8 @@ fn trans_class_drop(bcx: block,
v0: ValueRef,
dtor_did: ast::def_id,
class_did: ast::def_id,
substs: &ty::substs) -> block {
substs: &ty::substs,
take_ref: bool) -> block {
let drop_flag = GEPi(bcx, v0, struct_dtor());
do with_cond(bcx, IsNotNull(bcx, Load(bcx, drop_flag))) |cx| {
let mut bcx = cx;
Expand All @@ -427,7 +434,18 @@ fn trans_class_drop(bcx: block,
// just consist of the output pointer and the environment
// (self)
assert(params.len() == 2);
let self_arg = PointerCast(bcx, v0, params[1]);

// If we need to take a reference to the class (because it's using
// the Drop trait), do so now.
let llval;
if take_ref {
llval = alloca(bcx, val_ty(v0));
Store(bcx, v0, llval);
} else {
llval = v0;
}

let self_arg = PointerCast(bcx, llval, params[1]);
let args = ~[bcx.fcx.llretptr, self_arg];
Call(bcx, dtor_addr, args);

Expand Down Expand Up @@ -465,10 +483,13 @@ fn make_drop_glue(bcx: block, v0: ValueRef, t: ty::t) {
ty::ty_class(did, ref substs) => {
let tcx = bcx.tcx();
match ty::ty_dtor(tcx, did) {
Some(dtor) => {
trans_class_drop(bcx, v0, dtor, did, substs)
ty::TraitDtor(dtor) => {
trans_class_drop(bcx, v0, dtor, did, substs, true)
}
None => {
ty::LegacyDtor(dtor) => {
trans_class_drop(bcx, v0, dtor, did, substs, false)
}
ty::NoDtor => {
// No dtor? Just the default case
iter_structural_ty(bcx, v0, t, drop_ty)
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn simplify_type(tcx: ty::ctxt, typ: ty::t) -> ty::t {
// Reduce a class type to a record type in which all the fields are
// simplified
ty::ty_class(did, ref substs) => {
let simpl_fields = (if ty::ty_dtor(tcx, did).is_some() {
let simpl_fields = (if ty::ty_dtor(tcx, did).is_present() {
// remember the drop flag
~[{ident: syntax::parse::token::special_idents::dtor,
mt: {ty: ty::mk_u8(tcx),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ fn type_of(cx: @crate_ctxt, t: ty::t) -> TypeRef {

// include a byte flag if there is a dtor so that we know when we've
// been dropped
if ty::ty_dtor(cx.tcx, did) != None {
if ty::ty_dtor(cx.tcx, did).is_present() {
common::set_struct_body(llty, ~[T_struct(tys), T_i8()]);
} else {
common::set_struct_body(llty, ~[T_struct(tys)]);
Expand Down
36 changes: 29 additions & 7 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export enum_variants, substd_enum_variants, enum_is_univariant;
export trait_methods, store_trait_methods, impl_traits;
export enum_variant_with_id;
export ty_dtor;
export DtorKind, NoDtor, LegacyDtor, TraitDtor;
export ty_param_bounds_and_ty;
export ty_param_substs_and_ty;
export ty_bool, mk_bool, type_is_bool;
Expand Down Expand Up @@ -1868,7 +1869,7 @@ fn type_needs_drop(cx: ctxt, ty: t) -> bool {
}
ty_class(did, ref substs) => {
// Any class with a dtor needs a drop
ty_dtor(cx, did).is_some() || {
ty_dtor(cx, did).is_present() || {
for vec::each(ty::class_items_as_fields(cx, did, substs)) |f| {
if type_needs_drop(cx, f.mt.ty) { accum = true; }
}
Expand Down Expand Up @@ -3954,11 +3955,29 @@ fn item_path_str(cx: ctxt, id: ast::def_id) -> ~str {
ast_map::path_to_str(item_path(cx, id), cx.sess.parse_sess.interner)
}

enum DtorKind {
NoDtor,
LegacyDtor(def_id),
TraitDtor(def_id)
}

impl DtorKind {
pure fn is_not_present(&const self) -> bool {
match *self {
NoDtor => true,
_ => false
}
}
pure fn is_present(&const self) -> bool {
!self.is_not_present()
}
}

/* If class_id names a class with a dtor, return Some(the dtor's id).
Otherwise return none. */
fn ty_dtor(cx: ctxt, class_id: def_id) -> Option<def_id> {
fn ty_dtor(cx: ctxt, class_id: def_id) -> DtorKind {
match cx.destructor_for_type.find(class_id) {
Some(method_def_id) => return Some(method_def_id),
Some(method_def_id) => return TraitDtor(method_def_id),
None => {} // Continue.
}

Expand All @@ -3968,18 +3987,21 @@ fn ty_dtor(cx: ctxt, class_id: def_id) -> Option<def_id> {
node: ast::item_class(@{ dtor: Some(dtor), _ }, _),
_
}, _)) =>
Some(local_def(dtor.node.id)),
LegacyDtor(local_def(dtor.node.id)),
_ =>
None
NoDtor
}
}
else {
csearch::class_dtor(cx.sess.cstore, class_id)
match csearch::class_dtor(cx.sess.cstore, class_id) {
None => NoDtor,
Some(did) => LegacyDtor(did),
}
}
}

fn has_dtor(cx: ctxt, class_id: def_id) -> bool {
ty_dtor(cx, class_id).is_some()
ty_dtor(cx, class_id).is_present()
}

fn item_path(cx: ctxt, id: ast::def_id) -> ast_map::path {
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ struct PoisonOnFail {
}

impl PoisonOnFail : Drop {
fn finalize() {
fn finalize(&self) {
/* assert !*self.failed; -- might be false in case of cond.wait() */
if task::failing() { *self.failed = true; }
}
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub struct Arena {
}

impl Arena : Drop {
fn finalize() {
fn finalize(&self) {
unsafe {
destroy_chunk(&self.head);
for list::each(self.chunks) |chunk| {
Expand Down
10 changes: 5 additions & 5 deletions src/libstd/c_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ struct DtorRes {
}

impl DtorRes : Drop {
fn finalize() {
match self.dtor {
option::None => (),
option::Some(f) => f()
}
fn finalize(&self) {
match self.dtor {
option::None => (),
option::Some(f) => f()
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libstd/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct Future<A> {
// FIXME(#2829) -- futures should not be copyable, because they close
// over fn~'s that have pipes and so forth within!
impl<A> Future<A> : Drop {
fn finalize() {}
fn finalize(&self) {}
}

priv enum FutureState<A> {
Expand Down
8 changes: 4 additions & 4 deletions src/libstd/net_tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ struct TcpSocket {
}

impl TcpSocket : Drop {
fn finalize() {
unsafe {
tear_down_socket_data(self.socket_data)
}
fn finalize(&self) {
unsafe {
tear_down_socket_data(self.socket_data)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ mod big_tests {
}

impl LVal : Drop {
fn finalize() {
fn finalize(&self) {
let x = unsafe { task::local_data::local_data_get(self.key) };
match x {
Some(@y) => {
Expand Down
18 changes: 9 additions & 9 deletions src/libstd/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ struct SemRelease {
}

impl SemRelease : Drop {
fn finalize() {
fn finalize(&self) {
self.sem.release();
}
}
Expand All @@ -170,7 +170,7 @@ struct SemAndSignalRelease {
}

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

impl Condvar : Drop { fn finalize() {} }
impl Condvar : Drop { fn finalize(&self) {} }

impl &Condvar {
/**
Expand Down Expand Up @@ -257,7 +257,7 @@ impl &Condvar {
}

impl SemAndSignalReacquire : Drop {
fn finalize() {
fn finalize(&self) {
unsafe {
// Needs to succeed, instead of itself dying.
do task::unkillable {
Expand Down Expand Up @@ -607,7 +607,7 @@ struct RWlockReleaseRead {
}

impl RWlockReleaseRead : Drop {
fn finalize() {
fn finalize(&self) {
unsafe {
do task::unkillable {
let mut last_reader = false;
Expand Down Expand Up @@ -641,7 +641,7 @@ struct RWlockReleaseDowngrade {
}

impl RWlockReleaseDowngrade : Drop {
fn finalize() {
fn finalize(&self) {
unsafe {
do task::unkillable {
let mut writer_or_last_reader = false;
Expand Down Expand Up @@ -678,10 +678,10 @@ fn RWlockReleaseDowngrade(lock: &r/RWlock) -> RWlockReleaseDowngrade/&r {

/// The "write permission" token used for rwlock.write_downgrade().
pub struct RWlockWriteMode { /* priv */ lock: &RWlock }
impl RWlockWriteMode : Drop { fn finalize() {} }
impl RWlockWriteMode : Drop { fn finalize(&self) {} }
/// The "read permission" token used for rwlock.write_downgrade().
pub struct RWlockReadMode { priv lock: &RWlock }
impl RWlockReadMode : Drop { fn finalize() {} }
impl RWlockReadMode : Drop { fn finalize(&self) {} }

impl &RWlockWriteMode {
/// Access the pre-downgrade rwlock in write mode.
Expand Down Expand Up @@ -993,7 +993,7 @@ mod tests {
}

impl SendOnFailure : Drop {
fn finalize() {
fn finalize(&self) {
self.c.send(());
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/auxiliary/issue-2526.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct arc_destruct<T:Const> {
}

impl<T:Const> arc_destruct<T> : Drop {
fn finalize() {}
fn finalize(&self) {}
}

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

impl context_res : Drop {
fn finalize() {}
fn finalize(&self) {}
}

fn context_res() -> context_res {
Expand Down
2 changes: 1 addition & 1 deletion src/test/auxiliary/issue-3012-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct socket_handle {
}

impl socket_handle : Drop {
fn finalize() {
fn finalize(&self) {
/* c::close(self.sockfd); */
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/auxiliary/issue2170lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct rsrc {
}

impl rsrc : Drop {
fn finalize() {
fn finalize(&self) {
foo(self.x);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/auxiliary/test_comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct port_ptr<T:Send> {
}

impl<T:Send> port_ptr<T> : Drop {
fn finalize() {
fn finalize(&self) {
unsafe {
debug!("in the port_ptr destructor");
do task::unkillable {
Expand Down
2 changes: 1 addition & 1 deletion src/test/bench/task-perf-alloc-unwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct r {
}

impl r : Drop {
fn finalize() {}
fn finalize(&self) {}
}

fn r(l: @nillist) -> r {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
struct X { x: () }

impl X : Drop {
fn finalize() {
fn finalize(&self) {
error!("destructor runs");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
struct X { x: (), }

impl X : Drop {
fn finalize() {
fn finalize(&self) {
error!("destructor runs");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
struct X { x: (), }

impl X : Drop {
fn finalize() {
fn finalize(&self) {
error!("destructor runs");
}
}
Expand Down
Loading