about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/librustc/middle/trans/type_of.rs15
-rw-r--r--src/test/run-pass/const-enum-newtype-align.rs17
2 files changed, 29 insertions, 3 deletions
diff --git a/src/librustc/middle/trans/type_of.rs b/src/librustc/middle/trans/type_of.rs
index d87674f6b6e..6be82f833a6 100644
--- a/src/librustc/middle/trans/type_of.rs
+++ b/src/librustc/middle/trans/type_of.rs
@@ -328,11 +328,20 @@ pub fn type_of(cx: @crate_ctxt, t: ty::t) -> TypeRef {
 pub fn enum_body_types(cx: @crate_ctxt, did: ast::def_id, t: ty::t)
                     -> ~[TypeRef] {
     let univar = ty::enum_is_univariant(cx.tcx, did);
-    let size = machine::static_size_of_enum(cx, t);
     if !univar {
+        let size = machine::static_size_of_enum(cx, t);
         ~[T_enum_discrim(cx), T_array(T_i8(), size)]
-    } else {
-        ~[T_array(T_i8(), size)]
+    }
+    else {
+        // Use the actual fields, so we get the alignment right.
+        match ty::get(t).sty {
+            ty::ty_enum(_, ref substs) => {
+                do ty::enum_variants(cx.tcx, did)[0].args.map |&field_ty| {
+                    sizing_type_of(cx, ty::subst(cx.tcx, substs, field_ty))
+                }
+            }
+            _ => cx.sess.bug(~"enum is not an enum")
+        }
     }
 }
 
diff --git a/src/test/run-pass/const-enum-newtype-align.rs b/src/test/run-pass/const-enum-newtype-align.rs
new file mode 100644
index 00000000000..126af6ae730
--- /dev/null
+++ b/src/test/run-pass/const-enum-newtype-align.rs
@@ -0,0 +1,17 @@
+// 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.
+
+enum E = u32;
+struct S { a: u8, b: E }
+const C: S = S { a: 0xA5, b: E(0xDEADBEEF) };
+
+fn main() {
+    assert C.b == 0xDEADBEEF;
+}