From a26aca2fe6dfe90693d0c41022d6308bb3ab4904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Tesa=C5=99?= Date: Tue, 15 Sep 2026 22:28:53 +0200 Subject: [PATCH] fix constant bit_field field access returning zero value --- src/check_expr.cpp | 2 +- tests/internal/test_bit_field.odin | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 891e4093a..aa62a6114 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -5600,7 +5600,7 @@ gb_internal ExactValue get_constant_field_single(CheckerContext *c, ExactValue v if (success_) *success_ = false; if (finish_) *finish_ = true; return empty_exact_value; - } else if (is_type_struct(node->tav.type)) { + } else if (is_type_struct(node->tav.type) || is_type_bit_field(node->tav.type)) { bool found = false; for (Ast *elem : cl->elems) { if (elem->kind != Ast_FieldValue) { diff --git a/tests/internal/test_bit_field.odin b/tests/internal/test_bit_field.odin index 40000b16e..71e77c3e0 100644 --- a/tests/internal/test_bit_field.odin +++ b/tests/internal/test_bit_field.odin @@ -171,3 +171,28 @@ bit_field_full_width_fields :: proc(t: ^testing.T) { h.hi = 0 testing.expect_value(t, transmute(u64)h, 0xCAFE_F00D) } + +// field access on a constant bit_field used to always return the zero value +@(test) +bit_field_constant_field_access :: proc(t: ^testing.T) { + BF :: bit_field u32 { a: u8 | 3, b: u16 | 9, c: bool | 1, d: u8 | 7 } + + FULL :: BF{ a = 3, b = 100, c = true, d = 5 } + testing.expect_value(t, FULL.a, u8(3)) + testing.expect_value(t, FULL.b, u16(100)) + testing.expect_value(t, FULL.c, true) + testing.expect_value(t, FULL.d, u8(5)) + + // unset fields still read as zero + EMPTY :: BF{} + testing.expect_value(t, EMPTY.a, u8(0)) + testing.expect_value(t, EMPTY.b, u16(0)) + testing.expect_value(t, EMPTY.c, false) + testing.expect_value(t, EMPTY.d, u8(0)) + + v := FULL + testing.expect_value(t, v.a, FULL.a) + testing.expect_value(t, v.b, FULL.b) + testing.expect_value(t, v.c, FULL.c) + testing.expect_value(t, v.d, FULL.d) +}