From 753615e856d28aa22517ce33b4896a17f48b009b Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 26 Feb 2026 08:38:34 +0300 Subject: [PATCH] cgen: fix enum explicit size for tcc and gcc (fixes #25041) --- vlib/v/gen/c/cgen.v | 6 ++++-- .../enums/enum_explicit_size_big_and_small_test.v | 12 ++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 64b5ce20f..b10d06d75 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5148,10 +5148,12 @@ fn (mut g Gen) debugger_stmt(node ast.DebuggerStmt) { fn (mut g Gen) enum_decl(node ast.EnumDecl) { enum_name := util.no_dots(node.name) is_flag := node.is_flag - if g.is_cc_msvc { + // Explicit-size enums are emitted as typedef + defines, so all C compilers + // (including tinyc) respect the selected storage size. + if g.is_cc_msvc || node.typ != ast.int_type { mut last_value := '0' enum_typ_name := g.table.get_type_name(node.typ) - if g.pref.skip_unused && node.typ.idx() !in g.table.used_features.used_syms { + if g.pref.skip_unused && node.enum_typ !in g.table.used_features.used_syms { return } g.enum_typedefs.writeln('') diff --git a/vlib/v/tests/enums/enum_explicit_size_big_and_small_test.v b/vlib/v/tests/enums/enum_explicit_size_big_and_small_test.v index e4ba5ded6..d8ac11f18 100644 --- a/vlib/v/tests/enums/enum_explicit_size_big_and_small_test.v +++ b/vlib/v/tests/enums/enum_explicit_size_big_and_small_test.v @@ -6,6 +6,10 @@ enum SmallEnum as i8 { e } +struct SmallEnumWrap { + value SmallEnum +} + enum BigEnum as u64 { a = 0xABCD_EF09_1234_5678 b = 0xFFFF_FFFF_FFFF_FFF0 @@ -24,12 +28,8 @@ enum BigIEnum as i64 { fn test_small_enum() { dump(sizeof(SmallEnum)) - $if tinyc { - // TODO: TCC currently ignores `__attribute__((packed))` for enums, and uses an int instead, even with -fshort-enums :-| - assert sizeof(SmallEnum) == 4 - } $else { - assert sizeof(SmallEnum) == 1 - } + assert sizeof(SmallEnum) == 1 + assert sizeof(SmallEnumWrap) == 1 dump(i8(SmallEnum.a)) dump(i8(SmallEnum.b)) dump(i8(SmallEnum.c)) -- 2.39.5