Skip to content

Commit d009c63

Browse files
committed
Make univariant enums act like structs, so that they're aligned correctly.
Consts of such enums are aligned correctly, so we could either misalign them to match the type_of, or fix the type_of. The latter seems like a better idea.
1 parent 9ea05a4 commit d009c63

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+
fn main() {
16+
assert C.b == 0xDEADBEEF;
17+
}

0 commit comments

Comments
 (0)