Skip to content

Commit

Permalink
auto merge of #7214 : Blei/rust/fix-owned-traits, r=pcwalton
Browse files Browse the repository at this point in the history
This finishes the incomplete conversion of unique traits as two-word
allocations started in 211d038.

Fixes #5882, #6717, #7153, #7208.
  • Loading branch information
bors committed Jun 22, 2013
2 parents df166ba + de471a2 commit 0739c6b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
6 changes: 2 additions & 4 deletions src/librustc/back/abi.rs
Expand Up @@ -57,12 +57,10 @@ pub static n_tydesc_fields: uint = 8u;
pub static fn_field_code: uint = 0u;
pub static fn_field_box: uint = 1u;

// The three fields of a trait object/trait instance: vtable, box, and type
// description.
// The two fields of a trait object/trait instance: vtable and box.
// The vtable contains the type descriptor as first element.
pub static trt_field_vtable: uint = 0u;
pub static trt_field_box: uint = 1u;
// This field is only present in unique trait objects, so it comes last.
pub static trt_field_tydesc: uint = 2u;

pub static vec_elt_fill: uint = 0u;

Expand Down
19 changes: 13 additions & 6 deletions src/librustc/middle/trans/glue.rs
Expand Up @@ -40,7 +40,6 @@ use middle::trans::type_::Type;
use core::io;
use core::libc::c_uint;
use core::str;
use core::vec;
use extra::time;
use syntax::ast;

Expand Down Expand Up @@ -578,11 +577,19 @@ pub fn make_take_glue(bcx: block, v: ValueRef, t: ty::t) {
bcx
}
ty::ty_trait(_, _, ty::UniqTraitStore, _) => {
let llval = GEPi(bcx, v, [0, abi::trt_field_box]);
let lltydesc = Load(bcx, GEPi(bcx, v, [0, abi::trt_field_tydesc]));
call_tydesc_glue_full(bcx, llval, lltydesc,
abi::tydesc_field_take_glue, None);
bcx
let lluniquevalue = GEPi(bcx, v, [0, abi::trt_field_box]);
let llvtable = Load(bcx, GEPi(bcx, v, [0, abi::trt_field_vtable]));

// Cast the vtable to a pointer to a pointer to a tydesc.
let llvtable = PointerCast(bcx, llvtable,
bcx.ccx().tydesc_type.ptr_to().ptr_to());
let lltydesc = Load(bcx, llvtable);
call_tydesc_glue_full(bcx,
lluniquevalue,
lltydesc,
abi::tydesc_field_take_glue,
None);
bcx
}
ty::ty_opaque_closure_ptr(ck) => {
closure::make_opaque_cbox_take_glue(bcx, ck, v)
Expand Down
37 changes: 37 additions & 0 deletions src/test/run-pass/owned-trait-objects.rs
@@ -0,0 +1,37 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::uint::{range};

trait FooTrait {
fn foo(&self) -> uint;
}

struct BarStruct {
x: uint
}

impl FooTrait for BarStruct {
fn foo(&self) -> uint {
self.x
}
}

pub fn main() {
let foos: ~[ ~FooTrait ] = ~[
~BarStruct{ x: 0 } as ~FooTrait,
~BarStruct{ x: 1 } as ~FooTrait,
~BarStruct{ x: 2 } as ~FooTrait
];

for range(0, foos.len()) |i| {
assert_eq!(i, foos[i].foo());
}
}

0 comments on commit 0739c6b

Please sign in to comment.