From feb9b6159ef0c9b41c0dcefe319fc68fe645a8e7 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 29 Jun 2026 11:06:34 +0100 Subject: [PATCH] Fix `pkg.Constant.field` union access --- src/exact_value.cpp | 16 ++++++++++++++++ src/llvm_backend_const.cpp | 18 ++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/exact_value.cpp b/src/exact_value.cpp index f232f95e0..184a8f2e4 100644 --- a/src/exact_value.cpp +++ b/src/exact_value.cpp @@ -32,6 +32,22 @@ enum ExactValueKind { ExactValue_Count, }; +gb_global char const *exact_value_kind_string[ExactValue_Count] = { + "Invalid", + + "Bool", + "String", + "Integer", + "Float", + "Complex", + "Quaternion", + "Pointer", + "Compound", + "Procedure", + "Typeid", + "String16", +}; + struct ExactValue { ExactValueKind kind; union { diff --git a/src/llvm_backend_const.cpp b/src/llvm_backend_const.cpp index be036f553..0973d49d5 100644 --- a/src/llvm_backend_const.cpp +++ b/src/llvm_backend_const.cpp @@ -972,6 +972,7 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, Ty if (cl->elems.count == 0) { return lb_const_nil(m, original_type); } + value_type = type_of_expr(value.value_compound); } else if (value.kind == ExactValue_Invalid) { return lb_const_nil(m, original_type); } @@ -983,18 +984,31 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, Ty i64 block_size = bt->Union.variant_block_size; - if (are_types_identical(value_type, original_type)) { + while (are_types_identical(value_type, original_type)) { if (value.kind == ExactValue_Compound) { ast_node(cl, CompoundLit, value.value_compound); if (cl->elems.count == 0) { return lb_const_nil(m, original_type); } + + value_type = type_of_expr(value.value_compound); + if (!are_types_identical(value_type, original_type)) { + break; + } + + GB_PANIC("%s --> %s vs %s", + expr_to_string(value.value_compound), + temp_canonical_string(value_type), temp_canonical_string(original_type)); + } else if (value.kind == ExactValue_Invalid) { return lb_const_nil(m, original_type); } - GB_PANIC("%s vs %s", type_to_string(value_type), type_to_string(original_type)); + GB_PANIC("(value.kind=%s) %s vs %s", + exact_value_kind_string[value.kind], + temp_canonical_string(value_type), temp_canonical_string(original_type)); } + // union_multiple_allow_compound:; lbValue cv = lb_const_value(m, value_type, value, value_type, cc); Type *variant_type = cv.type;