Skip to content

Commit abbcc03

Browse files
committed
Poison ty_param values in trans; fix buggy parametric obj and box malloc paths. Add generic-box test.
1 parent 0624f9d commit abbcc03

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

src/comp/middle/trans.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ fn type_of_inner(@crate_ctxt cx, @ty.t t, bool boxed) -> TypeRef {
661661
fail;
662662
}
663663
case (ty.ty_param(_)) {
664-
llty = T_typaram_ptr(cx.tn);
664+
llty = T_i8();
665665
}
666666
case (ty.ty_type) { llty = T_ptr(T_tydesc(cx.tn)); }
667667
}
@@ -1216,17 +1216,14 @@ fn trans_raw_malloc(@block_ctxt cx, TypeRef llptr_ty, ValueRef llsize)
12161216
ret rslt;
12171217
}
12181218

1219-
fn trans_malloc_without_cleanup(@block_ctxt cx, @ty.t t) -> result {
1220-
auto llty = type_of(cx.fcx.ccx, t);
1221-
auto llsize = llsize_of(llvm.LLVMGetElementType(llty));
1222-
ret trans_raw_malloc(cx, llty, llsize);
1223-
}
1224-
1225-
fn trans_malloc(@block_ctxt cx, @ty.t t) -> result {
1226-
auto scope_cx = find_scope_cx(cx);
1227-
auto rslt = trans_malloc_without_cleanup(cx, t);
1228-
scope_cx.cleanups += clean(bind drop_ty(_, rslt.val, t));
1229-
ret rslt;
1219+
fn trans_malloc_boxed(@block_ctxt cx, @ty.t t) -> result {
1220+
// Synthesize a fake box type structurally so we have something
1221+
// to measure the size of.
1222+
auto boxed_body = plain_ty(ty.ty_tup(vec(plain_ty(ty.ty_int), t)));
1223+
auto box_ptr = plain_ty(ty.ty_box(t));
1224+
auto sz = size_of(cx, boxed_body);
1225+
auto llty = type_of(cx.fcx.ccx, box_ptr);
1226+
ret trans_raw_malloc(sz.bcx, llty, sz.val);
12301227
}
12311228

12321229

@@ -2256,7 +2253,11 @@ fn trans_unary(@block_ctxt cx, ast.unop op,
22562253
case (ast.box) {
22572254
auto e_ty = ty.expr_ty(e);
22582255
auto e_val = sub.val;
2259-
sub = trans_malloc(sub.bcx, node_ann_type(sub.bcx.fcx.ccx, a));
2256+
auto box_ty = node_ann_type(sub.bcx.fcx.ccx, a);
2257+
sub = trans_malloc_boxed(sub.bcx, e_ty);
2258+
find_scope_cx(cx).cleanups +=
2259+
clean(bind drop_ty(_, sub.val, box_ty));
2260+
22602261
auto box = sub.val;
22612262
auto rc = sub.bcx.build.GEP(box,
22622263
vec(C_int(0),
@@ -4707,11 +4708,8 @@ fn trans_obj(@crate_ctxt cx, &ast._obj ob, ast.def_id oid,
47074708
fields_ty)));
47084709
let @ty.t boxed_body_ty = plain_ty(ty.ty_box(body_ty));
47094710

4710-
let TypeRef llboxed_body_ty = type_of(cx, boxed_body_ty);
4711-
47124711
// Malloc a box for the body.
4713-
auto box = trans_raw_malloc(bcx, llboxed_body_ty,
4714-
llsize_of(llvm.LLVMGetElementType(llboxed_body_ty)));
4712+
auto box = trans_malloc_boxed(bcx, body_ty);
47154713
bcx = box.bcx;
47164714
auto rc = GEP_tup_like(bcx, boxed_body_ty, box.val,
47174715
vec(0, abi.box_rc_field_refcnt));

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn box[T](&tup(T,T,T) x) -> @tup(T,T,T) {
2+
ret @x;
3+
}
4+
5+
fn main() {
6+
let @tup(int,int,int) x = box[int](tup(1,2,3));
7+
check (x._1 == 2);
8+
}

0 commit comments

Comments
 (0)