about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPhilipp Brüschweiler <blei42@gmail.com>2013-06-22 09:37:40 +0200
committerPhilipp Brüschweiler <blei42@gmail.com>2013-06-22 09:37:40 +0200
commitde471a2eca15e6b9f3e4b81fc50d31b2ca9ee164 (patch)
treea0948d10875794fa49b78279e5c1ae215824c39d
parentdc4560dc26498573516d833cb2333b54071070e3 (diff)
downloadrust-de471a2eca15e6b9f3e4b81fc50d31b2ca9ee164.tar.gz
rust-de471a2eca15e6b9f3e4b81fc50d31b2ca9ee164.zip
Fix take glue of owned trait objects
This finishes the incomplete conversion of unique traits as two-word
allocations started in 211d038abc05c77785f72a31840016517cf218c2.

Fixes #5882, #6717, #7153, #7208.
-rw-r--r--src/librustc/back/abi.rs6
-rw-r--r--src/librustc/middle/trans/glue.rs19
-rw-r--r--src/test/run-pass/owned-trait-objects.rs37
3 files changed, 52 insertions, 10 deletions
diff --git a/src/librustc/back/abi.rs b/src/librustc/back/abi.rs
index e722e1a33c6..004170dea7f 100644
--- a/src/librustc/back/abi.rs
+++ b/src/librustc/back/abi.rs
@@ -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;
 
diff --git a/src/librustc/middle/trans/glue.rs b/src/librustc/middle/trans/glue.rs
index 3e3fdf686e3..e9febb51005 100644
--- a/src/librustc/middle/trans/glue.rs
+++ b/src/librustc/middle/trans/glue.rs
@@ -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;
 
@@ -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)
diff --git a/src/test/run-pass/owned-trait-objects.rs b/src/test/run-pass/owned-trait-objects.rs
new file mode 100644
index 00000000000..50b6666a20b
--- /dev/null
+++ b/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());
+    }
+}