Skip to content

Commit a2068f1

Browse files
committed
auto merge of #4890 : jld/rust/enum-newtype-alignment, r=catamorphism
2 parents a6945f2 + 7c34908 commit a2068f1

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/librustc/middle/trans/type_of.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,20 @@ pub fn type_of(cx: @crate_ctxt, t: ty::t) -> TypeRef {
328328
pub fn enum_body_types(cx: @crate_ctxt, did: ast::def_id, t: ty::t)
329329
-> ~[TypeRef] {
330330
let univar = ty::enum_is_univariant(cx.tcx, did);
331-
let size = machine::static_size_of_enum(cx, t);
332331
if !univar {
332+
let size = machine::static_size_of_enum(cx, t);
333333
~[T_enum_discrim(cx), T_array(T_i8(), size)]
334-
} else {
335-
~[T_array(T_i8(), size)]
334+
}
335+
else {
336+
// Use the actual fields, so we get the alignment right.
337+
match ty::get(t).sty {
338+
ty::ty_enum(_, ref substs) => {
339+
do ty::enum_variants(cx.tcx, did)[0].args.map |&field_ty| {
340+
sizing_type_of(cx, ty::subst(cx.tcx, substs, field_ty))
341+
}
342+
}
343+
_ => cx.sess.bug(~"enum is not an enum")
344+
}
336345
}
337346
}
338347

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
enum E = u32;
12+
struct S { a: u8, b: E }
13+
const C: S = S { a: 0xA5, b: E(0xDEADBEEF) };
14+
15+
pub fn main() {
16+
assert C.b == 0xDEADBEEF;
17+
}

0 commit comments

Comments
 (0)