Skip to content

Commit ed357af

Browse files
committed
Convert more resource tests to use classes with dtors
And monomorphize dtors correctly.
1 parent 8caf140 commit ed357af

File tree

8 files changed

+221
-102
lines changed

8 files changed

+221
-102
lines changed

src/rustc/middle/trans/base.rs

Lines changed: 184 additions & 75 deletions
Large diffs are not rendered by default.

src/rustc/middle/trans/common.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,12 @@ impl bcx_cxs for block {
423423
fn tcx() -> ty::ctxt { self.fcx.ccx.tcx }
424424
fn sess() -> session { self.fcx.ccx.sess }
425425

426+
fn val_str(val: ValueRef) -> str {
427+
val_str(self.ccx().tn, val)
428+
}
429+
fn ty_to_str(t: ty::t) -> str {
430+
ty_to_str(self.tcx(), t)
431+
}
426432
fn to_str() -> str {
427433
alt self.node_info {
428434
some(node_info) {

src/rustc/middle/trans/impl.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import build::*;
66
import driver::session::session;
77
import syntax::{ast, ast_map};
88
import ast_map::{path, path_mod, path_name, node_id_to_str};
9+
import driver::session::expect;
910
import syntax::ast_util::{local_def, split_class_items};
1011
import metadata::csearch;
1112
import back::{link, abi};
@@ -247,7 +248,10 @@ fn make_impl_vtable(ccx: @crate_ctxt, impl_id: ast::def_id, substs: [ty::t],
247248
vtables: typeck::vtable_res) -> ValueRef {
248249
let _icx = ccx.insn_ctxt("impl::make_impl_vtable");
249250
let tcx = ccx.tcx;
250-
let ifce_id = ty::ty_to_def_id(option::get(ty::impl_iface(tcx, impl_id)));
251+
let ifce_id = expect(ccx.sess,
252+
ty::ty_to_def_id(option::get(ty::impl_iface(tcx,
253+
impl_id))),
254+
{|| "make_impl_vtable: non-iface-type implemented"});
251255
let has_tps = (*ty::lookup_item_type(ccx.tcx, impl_id).bounds).len() > 0u;
252256
make_vtable(ccx, vec::map(*ty::iface_methods(tcx, ifce_id)) {|im|
253257
let fty = ty::subst_tps(tcx, substs, ty::mk_fn(tcx, im.fty));

src/rustc/middle/trans/type_use.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ fn type_uses_for(ccx: @crate_ctxt, fn_id: def_id, n_tps: uint)
9090
ast_map::node_ctor(_, _, ast_map::class_ctor(ctor, _), _){
9191
handle_body(cx, ctor.node.body);
9292
}
93+
ast_map::node_dtor(_, dtor, _, _){
94+
handle_body(cx, dtor.node.body);
95+
}
96+
9397
}
9498
let uses = vec::from_mut(cx.uses);
9599
ccx.type_use_cache.insert(fn_id, uses);

src/rustc/middle/ty.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,11 +2375,12 @@ fn impl_iface(cx: ctxt, id: ast::def_id) -> option<t> {
23752375
}
23762376
}
23772377

2378-
fn ty_to_def_id(ty: t) -> ast::def_id {
2379-
alt check get(ty).struct {
2378+
fn ty_to_def_id(ty: t) -> option<ast::def_id> {
2379+
alt get(ty).struct {
23802380
ty_iface(id, _) | ty_class(id, _) | ty_res(id, _, _) | ty_enum(id, _) {
2381-
id
2381+
some(id)
23822382
}
2383+
_ { none }
23832384
}
23842385
}
23852386

@@ -2413,7 +2414,7 @@ fn ty_dtor(cx: ctxt, class_id: def_id) -> option<def_id> {
24132414
some(ast_map::node_item(@{node: ast::item_class(_, _, _, _,
24142415
some(dtor), _), _}, _))
24152416
{ some(local_def(dtor.node.id)) }
2416-
_ { none }
2417+
_ { none }
24172418
}
24182419
}
24192420
else {

src/test/run-pass/resource-destruct-with-class.rs

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
resource shrinky_pointer(i: @mut int) { *i -= 1; }
2-
3-
fn look_at(pt: shrinky_pointer) -> int { ret **pt; }
1+
class shrinky_pointer {
2+
let i: @@mut int;
3+
fn look_at() -> int { ret **(self.i); }
4+
new(i: @@mut int) { self.i = i; }
5+
drop { log(error, "Hello!"); **(self.i) -= 1; }
6+
}
47

58
fn main() {
6-
let my_total = @mut 10;
7-
{ let pt <- shrinky_pointer(my_total); assert (look_at(pt) == 10); }
8-
assert (*my_total == 9);
9+
let my_total = @@mut 10;
10+
{ let pt <- shrinky_pointer(my_total); assert (pt.look_at() == 10); }
11+
log(error, #fmt("my_total = %d", **my_total));
12+
assert (**my_total == 9);
913
}

src/test/run-pass/resource-generic.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
resource finish<T>(arg: {val: T, fin: native fn(T)}) {
2-
arg.fin(arg.val);
1+
class finish<T: copy> {
2+
let arg: {val: T, fin: native fn(T)};
3+
new(arg: {val: T, fin: native fn(T)}) {
4+
self.arg = arg;
5+
}
6+
drop { self.arg.fin(self.arg.val); }
37
}
48

59
fn main() {
610
let box = @mut 10;
711
fn dec_box(&&i: @mut int) { *i -= 1; }
812

9-
{ let i <- finish({val: box, fin: dec_box}); }
13+
{ let _i <- finish({val: box, fin: dec_box}); }
1014
assert (*box == 9);
1115
}

0 commit comments

Comments
 (0)