Skip to content

Commit 0739c6b

Browse files
committed
auto merge of #7214 : Blei/rust/fix-owned-traits, r=pcwalton
This finishes the incomplete conversion of unique traits as two-word allocations started in 211d038. Fixes #5882, #6717, #7153, #7208.
2 parents df166ba + de471a2 commit 0739c6b

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

src/librustc/back/abi.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,10 @@ 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.
60+
// The two fields of a trait object/trait instance: vtable and box.
61+
// The vtable contains the type descriptor as first element.
6262
pub static trt_field_vtable: uint = 0u;
6363
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;
6664

6765
pub static vec_elt_fill: uint = 0u;
6866

src/librustc/middle/trans/glue.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ use middle::trans::type_::Type;
4040
use core::io;
4141
use core::libc::c_uint;
4242
use core::str;
43-
use core::vec;
4443
use extra::time;
4544
use syntax::ast;
4645

@@ -578,11 +577,19 @@ pub fn make_take_glue(bcx: block, v: ValueRef, t: ty::t) {
578577
bcx
579578
}
580579
ty::ty_trait(_, _, ty::UniqTraitStore, _) => {
581-
let llval = GEPi(bcx, v, [0, abi::trt_field_box]);
582-
let lltydesc = Load(bcx, GEPi(bcx, v, [0, abi::trt_field_tydesc]));
583-
call_tydesc_glue_full(bcx, llval, lltydesc,
584-
abi::tydesc_field_take_glue, None);
585-
bcx
580+
let lluniquevalue = GEPi(bcx, v, [0, abi::trt_field_box]);
581+
let llvtable = Load(bcx, GEPi(bcx, v, [0, abi::trt_field_vtable]));
582+
583+
// Cast the vtable to a pointer to a pointer to a tydesc.
584+
let llvtable = PointerCast(bcx, llvtable,
585+
bcx.ccx().tydesc_type.ptr_to().ptr_to());
586+
let lltydesc = Load(bcx, llvtable);
587+
call_tydesc_glue_full(bcx,
588+
lluniquevalue,
589+
lltydesc,
590+
abi::tydesc_field_take_glue,
591+
None);
592+
bcx
586593
}
587594
ty::ty_opaque_closure_ptr(ck) => {
588595
closure::make_opaque_cbox_take_glue(bcx, ck, v)
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::uint::{range};
12+
13+
trait FooTrait {
14+
fn foo(&self) -> uint;
15+
}
16+
17+
struct BarStruct {
18+
x: uint
19+
}
20+
21+
impl FooTrait for BarStruct {
22+
fn foo(&self) -> uint {
23+
self.x
24+
}
25+
}
26+
27+
pub fn main() {
28+
let foos: ~[ ~FooTrait ] = ~[
29+
~BarStruct{ x: 0 } as ~FooTrait,
30+
~BarStruct{ x: 1 } as ~FooTrait,
31+
~BarStruct{ x: 2 } as ~FooTrait
32+
];
33+
34+
for range(0, foos.len()) |i| {
35+
assert_eq!(i, foos[i].foo());
36+
}
37+
}

0 commit comments

Comments
 (0)