Skip to content

Make univariant enums act like structs in type_of, so that they're aligned correctly. #4890

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

Closed
wants to merge 2 commits 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
15 changes: 12 additions & 3 deletions src/librustc/middle/trans/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/test/run-pass/const-enum-newtype-align.rs
Original file line number Diff line number Diff line change
@@ -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) };

pub fn main() {
assert C.b == 0xDEADBEEF;
}