Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix take glue of owned trait objects #7214

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/librustc/back/abi.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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());
}
}