Skip to content

Commit 31599fe

Browse files
committed
auto merge of #6172 : Sodel-the-Vociferous/rust/rm_trt_obj_magic_nums, r=catamorphism,graydon
I don't know how one would write a separate test for this sort of thing. Building the compiler, and `make check` worked, which should mean I didn't screw anything.
2 parents ba84251 + c64471a commit 31599fe

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

src/librustc/back/abi.rs

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ pub static n_tydesc_fields: uint = 8u;
5757
pub static fn_field_code: uint = 0u;
5858
pub static fn_field_box: uint = 1u;
5959

60+
// The three fields of a trait object/trait instance: vtable, box, and type
61+
// description.
62+
pub static trt_field_vtable: uint = 0u;
63+
pub static trt_field_box: uint = 1u;
64+
// This field is only present in unique trait objects, so it comes last.
65+
pub static trt_field_tydesc: uint = 2u;
66+
6067
pub static vec_elt_fill: uint = 0u;
6168

6269
pub static vec_elt_alloc: uint = 1u;

src/librustc/middle/trans/glue.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -537,12 +537,12 @@ pub fn make_drop_glue(bcx: block, v0: ValueRef, t: ty::t) {
537537
closure::make_closure_glue(bcx, v0, t, drop_ty)
538538
}
539539
ty::ty_trait(_, _, ty::BoxTraitStore, _) => {
540-
let llbox = Load(bcx, GEPi(bcx, v0, [0u, 1u]));
540+
let llbox = Load(bcx, GEPi(bcx, v0, [0u, abi::trt_field_box]));
541541
decr_refcnt_maybe_free(bcx, llbox, ty::mk_opaque_box(ccx.tcx))
542542
}
543543
ty::ty_trait(_, _, ty::UniqTraitStore, _) => {
544-
let lluniquevalue = GEPi(bcx, v0, [0, 1]);
545-
let lltydesc = Load(bcx, GEPi(bcx, v0, [0, 2]));
544+
let lluniquevalue = GEPi(bcx, v0, [0, abi::trt_field_box]);
545+
let lltydesc = Load(bcx, GEPi(bcx, v0, [0, abi::trt_field_tydesc]));
546546
call_tydesc_glue_full(bcx, lluniquevalue, lltydesc,
547547
abi::tydesc_field_free_glue, None);
548548
bcx
@@ -601,13 +601,13 @@ pub fn make_take_glue(bcx: block, v: ValueRef, t: ty::t) {
601601
closure::make_closure_glue(bcx, v, t, take_ty)
602602
}
603603
ty::ty_trait(_, _, ty::BoxTraitStore, _) => {
604-
let llbox = Load(bcx, GEPi(bcx, v, [0u, 1u]));
604+
let llbox = Load(bcx, GEPi(bcx, v, [0u, abi::trt_field_box]));
605605
incr_refcnt_of_boxed(bcx, llbox);
606606
bcx
607607
}
608608
ty::ty_trait(_, _, ty::UniqTraitStore, _) => {
609-
let llval = GEPi(bcx, v, [0, 1]);
610-
let lltydesc = Load(bcx, GEPi(bcx, v, [0, 2]));
609+
let llval = GEPi(bcx, v, [0, abi::trt_field_box]);
610+
let lltydesc = Load(bcx, GEPi(bcx, v, [0, abi::trt_field_tydesc]));
611611
call_tydesc_glue_full(bcx, llval, lltydesc,
612612
abi::tydesc_field_take_glue, None);
613613
bcx

src/librustc/middle/trans/meth.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -637,14 +637,15 @@ pub fn trans_trait_callee_from_llval(bcx: block,
637637
val_str(bcx.ccx().tn, llpair));
638638
let llvtable = Load(bcx,
639639
PointerCast(bcx,
640-
GEPi(bcx, llpair, [0u, 0u]),
640+
GEPi(bcx, llpair,
641+
[0u, abi::trt_field_vtable]),
641642
T_ptr(T_ptr(T_vtable()))));
642643
643644
// Load the box from the @Trait pair and GEP over the box header if
644645
// necessary:
645646
let mut llself;
646647
debug!("(translating trait callee) loading second index from pair");
647-
let llbox = Load(bcx, GEPi(bcx, llpair, [0u, 1u]));
648+
let llbox = Load(bcx, GEPi(bcx, llpair, [0u, abi::trt_field_box]));
648649
649650
// Munge `llself` appropriately for the type of `self` in the method.
650651
let self_mode;
@@ -845,27 +846,30 @@ pub fn trans_trait_cast(bcx: block,
845846

846847
match store {
847848
ty::RegionTraitStore(_) | ty::BoxTraitStore => {
848-
let mut llboxdest = GEPi(bcx, lldest, [0u, 1u]);
849-
// Just store the pointer into the pair.
849+
let mut llboxdest = GEPi(bcx, lldest, [0u, abi::trt_field_box]);
850+
// Just store the pointer into the pair. (Region/borrowed
851+
// and boxed trait objects are represented as pairs, and
852+
// have no type descriptor field.)
850853
llboxdest = PointerCast(bcx,
851854
llboxdest,
852855
T_ptr(type_of(bcx.ccx(), v_ty)));
853856
bcx = expr::trans_into(bcx, val, SaveIn(llboxdest));
854857
}
855858
ty::UniqTraitStore => {
856-
// Translate the uniquely-owned value into the second element of
857-
// the triple. (The first element is the vtable.)
858-
let mut llvaldest = GEPi(bcx, lldest, [0, 1]);
859+
// Translate the uniquely-owned value in the
860+
// triple. (Unique trait objects are represented as
861+
// triples.)
862+
let mut llvaldest = GEPi(bcx, lldest, [0, abi::trt_field_box]);
859863
llvaldest = PointerCast(bcx,
860864
llvaldest,
861865
T_ptr(type_of(bcx.ccx(), v_ty)));
862866
bcx = expr::trans_into(bcx, val, SaveIn(llvaldest));
863867

864-
// Get the type descriptor of the wrapped value and store it into
865-
// the third element of the triple as well.
868+
// Get the type descriptor of the wrapped value and store
869+
// it in the triple as well.
866870
let tydesc = get_tydesc(bcx.ccx(), v_ty);
867871
glue::lazily_emit_all_tydesc_glue(bcx.ccx(), tydesc);
868-
let lltydescdest = GEPi(bcx, lldest, [0, 2]);
872+
let lltydescdest = GEPi(bcx, lldest, [0, abi::trt_field_tydesc]);
869873
Store(bcx, tydesc.tydesc, lltydescdest);
870874
}
871875
}
@@ -875,7 +879,7 @@ pub fn trans_trait_cast(bcx: block,
875879
let orig = resolve_vtable_in_fn_ctxt(bcx.fcx, orig);
876880
let vtable = get_vtable(bcx.ccx(), orig);
877881
Store(bcx, vtable, PointerCast(bcx,
878-
GEPi(bcx, lldest, [0u, 0u]),
882+
GEPi(bcx, lldest, [0u, abi::trt_field_vtable]),
879883
T_ptr(val_ty(vtable))));
880884

881885
bcx

0 commit comments

Comments
 (0)