Skip to content

Commit aa2586d

Browse files
Hejsilandrewrk
authored andcommitted
Fixed extern enums having the wrong size (#970)
Fixed extern enums having the wrong size See #977
1 parent 7337029 commit aa2586d

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/analyze.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
396+
test "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+
}

0 commit comments

Comments
 (0)