Skip to content

Commit a1d11bc

Browse files
committed
Make single-variant "c-like" enums have no size.
This makes an enum like `enum Foo { Foo }` be zero-sized. If a discriminant is set to non-zero, it still treats it as a c-like enum. Fixes rust-lang#15747
1 parent 34d6800 commit a1d11bc

File tree

1 file changed

+11
-10
lines changed
  • src/librustc_trans/trans

1 file changed

+11
-10
lines changed

src/librustc_trans/trans/adt.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,17 @@ fn represent_type_uncached<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
190190
dtor);
191191
}
192192

193+
if cases.len() == 1 && cases[0].discr == 0 {
194+
// Equivalent to a struct/tuple/newtype.
195+
// (Typechecking will reject discriminant-sizing attrs.)
196+
assert_eq!(hint, attr::ReprAny);
197+
let mut ftys = cases[0].tys.clone();
198+
if dtor { ftys.push(ty::mk_bool()); }
199+
return Univariant(mk_struct(cx, ftys.as_slice(), false, t),
200+
dtor);
201+
}
202+
203+
193204
if !dtor && cases.iter().all(|c| c.tys.len() == 0) {
194205
// All bodies empty -> intlike
195206
let discrs: Vec<u64> = cases.iter().map(|c| c.discr).collect();
@@ -212,16 +223,6 @@ fn represent_type_uncached<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
212223
def_id)).as_slice());
213224
}
214225

215-
if cases.len() == 1 {
216-
// Equivalent to a struct/tuple/newtype.
217-
// (Typechecking will reject discriminant-sizing attrs.)
218-
assert_eq!(hint, attr::ReprAny);
219-
let mut ftys = cases[0].tys.clone();
220-
if dtor { ftys.push(ty::mk_bool()); }
221-
return Univariant(mk_struct(cx, ftys.as_slice(), false, t),
222-
dtor);
223-
}
224-
225226
if !dtor && cases.len() == 2 && hint == attr::ReprAny {
226227
// Nullable pointer optimization
227228
let mut discr = 0;

0 commit comments

Comments
 (0)