authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-05-04 04:27:04+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-03 22:27:04-04:00
logaa2586de182e5587c924740e80468c4c4d509500
tree9c029e910872c7bbbad002f9c1d03a9859c00bc3
parent7337029ce1a73f258ea42a965b380bb3b6fc85dd

Fixed extern enums having the wrong size (#970)

Fixed extern enums having the wrong size See #977

2 files changed, 16 insertions(+), 1 deletions(-)

src/analyze.cpp+7-1
......@@ -2325,8 +2325,14 @@ static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type) {
23252325 HashMap<BigInt, AstNode *, bigint_hash, bigint_eql> occupied_tag_values = {};
23262326 occupied_tag_values.init(field_count);
23272327
2328 TypeTableEntry *tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1);
2328 TypeTableEntry *tag_int_type;
2329 if (enum_type->data.enumeration.layout == ContainerLayoutExtern) {
2330 tag_int_type = get_c_int_type(g, CIntTypeInt);
2331 } else {
2332 tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1);
2333 }
23292334
2335 // TODO: Are extern enums allowed to have an init_arg_expr?
23302336 if (decl_node->data.container_decl.init_arg_expr != nullptr) {
23312337 TypeTableEntry *wanted_tag_int_type = analyze_type_expr(g, scope, decl_node->data.container_decl.init_arg_expr);
23322338 if (type_is_invalid(wanted_tag_int_type)) {
test/cases/enum.zig+9
......@@ -392,3 +392,12 @@ test "enum with 1 field but explicit tag type should still have the tag type" {
392392 const Enum = enum(u8) { B = 2 };
393393 comptime @import("std").debug.assert(@sizeOf(Enum) == @sizeOf(u8));
394394}
395
396test "empty extern enum with members" {
397 const E = extern enum {
398 A,
399 B,
400 C,
401 };
402 assert(@sizeOf(E) == @sizeOf(c_int));
403}