diff --git a/src/librustc_trans/trans/adt.rs b/src/librustc_trans/trans/adt.rs index f7edb281b9eda..7a17a0e635ae5 100644 --- a/src/librustc_trans/trans/adt.rs +++ b/src/librustc_trans/trans/adt.rs @@ -190,6 +190,17 @@ fn represent_type_uncached<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, dtor); } + if cases.len() == 1 && cases[0].discr == 0 { + // Equivalent to a struct/tuple/newtype. + // (Typechecking will reject discriminant-sizing attrs.) + assert_eq!(hint, attr::ReprAny); + let mut ftys = cases[0].tys.clone(); + if dtor { ftys.push(ty::mk_bool()); } + return Univariant(mk_struct(cx, ftys.as_slice(), false, t), + dtor); + } + + if !dtor && cases.iter().all(|c| c.tys.len() == 0) { // All bodies empty -> intlike let discrs: Vec = cases.iter().map(|c| c.discr).collect(); @@ -212,16 +223,6 @@ fn represent_type_uncached<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, def_id)).as_slice()); } - if cases.len() == 1 { - // Equivalent to a struct/tuple/newtype. - // (Typechecking will reject discriminant-sizing attrs.) - assert_eq!(hint, attr::ReprAny); - let mut ftys = cases[0].tys.clone(); - if dtor { ftys.push(ty::mk_bool()); } - return Univariant(mk_struct(cx, ftys.as_slice(), false, t), - dtor); - } - if !dtor && cases.len() == 2 && hint == attr::ReprAny { // Nullable pointer optimization let mut discr = 0; diff --git a/src/test/run-pass/type-sizes.rs b/src/test/run-pass/type-sizes.rs index 2596bc9c83763..7bb2e78980daa 100644 --- a/src/test/run-pass/type-sizes.rs +++ b/src/test/run-pass/type-sizes.rs @@ -18,6 +18,14 @@ struct w {a: int, b: ()} struct x {a: int, b: (), c: ()} struct y {x: int} +enum c1 { + c1 +} + +enum c2 { + c2 = 1 +} + pub fn main() { assert_eq!(size_of::(), 1 as uint); assert_eq!(size_of::(), 4 as uint); @@ -34,4 +42,8 @@ pub fn main() { assert_eq!(size_of::(), size_of::()); assert_eq!(size_of::(), size_of::()); assert_eq!(size_of::(), size_of::()); + + // Make sure enum sizes are correct + assert_eq!(size_of::(), 0 as uint); + assert_eq!(size_of::(), 1 as uint); }