From ef275c5c0e5b23391cc05d363265cd68485b828a Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 21 Apr 2026 17:45:36 +0100 Subject: [PATCH] Support `[]int{multiple_returns(), 123}` --- src/check_expr.cpp | 37 +++++++++++++++++++++-------- src/llvm_backend_const.cpp | 48 +++++++++++++++++++++++++++----------- src/llvm_backend_expr.cpp | 37 +++++++++++++++++++++-------- 3 files changed, 89 insertions(+), 33 deletions(-) diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 8c96f019f..88e774b4e 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -10630,8 +10630,9 @@ gb_internal ExprKind check_compound_literal(CheckerContext *c, Operand *o, Ast * cl->max_count = max; } else { - isize index = 0; - for (; index < cl->elems.count; index++) { + for (isize index = 0; index < cl->elems.count; index++) { + defer (max += 1); + Ast *e = cl->elems[index]; if (e == nullptr) { error(node, "Invalid literal element"); @@ -10648,17 +10649,25 @@ gb_internal ExprKind check_compound_literal(CheckerContext *c, Operand *o, Ast * } Operand operand = {}; - check_expr_with_type_hint(c, &operand, e, elem_type); - check_assignment(c, &operand, elem_type, context_name); + check_multi_expr_with_type_hint(c, &operand, e, elem_type); + if (is_type_tuple(operand.type)) { + is_constant = false; + TypeTuple *tt = &operand.type->Tuple; + for_array(jj, tt->variables) { + Operand oo = operand; + oo.type = tt->variables[jj]->type; + check_assignment(c, &oo, elem_type, context_name); + } - if (is_constant) { - is_constant = check_is_operand_compound_lit_constant(c, &operand, elem_type); + max += tt->variables.count-1; + } else { + check_assignment(c, &operand, elem_type, context_name); + + if (is_constant) { + is_constant = check_is_operand_compound_lit_constant(c, &operand, elem_type); + } } } - - if (max < index) { - max = index; - } } @@ -10679,6 +10688,12 @@ gb_internal ExprKind check_compound_literal(CheckerContext *c, Operand *o, Ast * error(node, "Expected %lld values for this #soa array literal, got %lld", cast(long long)t->Struct.soa_count, cast(long long)max); } } + } else if (t->kind == Type_FixedCapacityDynamicArray) { + if (max > t->FixedCapacityDynamicArray.capacity) { + error(node, "Expected a maximum of %lld values for this fixed capacity dynamic array, got %lld", + cast(long long)t->FixedCapacityDynamicArray.capacity, + cast(long long)max); + } } @@ -10704,6 +10719,8 @@ gb_internal ExprKind check_compound_literal(CheckerContext *c, Operand *o, Ast * } } + cl->max_count = max; + break; } diff --git a/src/llvm_backend_const.cpp b/src/llvm_backend_const.cpp index 762243ee3..0156e2359 100644 --- a/src/llvm_backend_const.cpp +++ b/src/llvm_backend_const.cpp @@ -1476,17 +1476,24 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, lb return res; } else { // Assume that compound value is an array literal - GB_ASSERT_MSG(elem_count == type->Array.count, "%td != %td", elem_count, type->Array.count); + GB_ASSERT_MSG(elem_count <= type->Array.count, "%td <= %td", elem_count, type->Array.count); LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->Array.count); + isize elem_index = 0; for (isize i = 0; i < elem_count; i++) { TypeAndValue tav = cl->elems[i]->tav; GB_ASSERT(tav.mode != Addressing_Invalid); - values[i] = lb_const_value(m, elem_type, tav.value, cc, tav.type).value; + if (is_type_tuple(tav.type)) { + elem_index += tav.type->Tuple.variables.count; + } else { + values[elem_index++] = lb_const_value(m, elem_type, tav.value, cc, tav.type).value; + } } - for (isize i = elem_count; i < type->Array.count; i++) { - values[i] = LLVMConstNull(lb_type(m, elem_type)); + for (isize i = 0; i < type->Array.count; i++) { + if (values[i] == nullptr) { + values[i] = LLVMConstNull(lb_type(m, elem_type)); + } } res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->Array.count, values, cc); @@ -1560,20 +1567,28 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, lb res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->EnumeratedArray.count, values, cc); return res; } else { - GB_ASSERT_MSG(elem_count == type->EnumeratedArray.count, "%td != %td", elem_count, type->EnumeratedArray.count); + // Assume that compound value is an array literal + GB_ASSERT_MSG(elem_count <= type->EnumeratedArray.count, "%td <= %td", elem_count, type->EnumeratedArray.count); LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->EnumeratedArray.count); + isize elem_index = 0; for (isize i = 0; i < elem_count; i++) { TypeAndValue tav = cl->elems[i]->tav; GB_ASSERT(tav.mode != Addressing_Invalid); - values[i] = lb_const_value(m, elem_type, tav.value, cc, tav.type).value; + if (is_type_tuple(tav.type)) { + elem_index += tav.type->Tuple.variables.count; + } else { + values[elem_index++] = lb_const_value(m, elem_type, tav.value, cc, tav.type).value; + } } - for (isize i = elem_count; i < type->EnumeratedArray.count; i++) { - values[i] = LLVMConstNull(lb_type(m, elem_type)); + for (isize i = 0; i < type->EnumeratedArray.count; i++) { + if (values[i] == nullptr) { + values[i] = LLVMConstNull(lb_type(m, elem_type)); + } } - res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->EnumeratedArray.count, values, cc); + res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->Array.count, values, cc); return res; } } else if (is_type_fixed_capacity_dynamic_array(type)) { @@ -1666,16 +1681,23 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, lb LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)capacity); + isize elem_index = 0; for (isize i = 0; i < elem_count; i++) { TypeAndValue tav = cl->elems[i]->tav; GB_ASSERT(tav.mode != Addressing_Invalid); - values[i] = lb_const_value(m, elem_type, tav.value, cc, tav.type).value; + if (is_type_tuple(tav.type)) { + elem_index += tav.type->Tuple.variables.count; + } else { + values[elem_index++] = lb_const_value(m, elem_type, tav.value, cc, tav.type).value; + } } - for (isize i = elem_count; i < capacity; i++) { - values[i] = LLVMConstNull(lb_type(m, elem_type)); + for (isize i = 0; i < capacity; i++) { + if (values[i] == nullptr) { + values[i] = LLVMConstNull(lb_type(m, elem_type)); + } } - res.value = lb_fill_fixed_capacity_dynamic_array(m, elem_count, original_type, values, cc); + res.value = lb_fill_fixed_capacity_dynamic_array(m, elem_index, original_type, values, cc); return res; } } else if (is_type_simd_vector(type)) { diff --git a/src/llvm_backend_expr.cpp b/src/llvm_backend_expr.cpp index 4dd66b4f7..d64a708c5 100644 --- a/src/llvm_backend_expr.cpp +++ b/src/llvm_backend_expr.cpp @@ -4516,9 +4516,9 @@ gb_internal void lb_build_addr_compound_lit_populate(lbProcedure *p, Slicekind == Ast_FieldValue) { ast_node(fv, FieldValue, elem); if (bt->kind != Type_DynamicArray && lb_is_elem_const(fv->value, et)) { @@ -4588,22 +4588,39 @@ gb_internal void lb_build_addr_compound_lit_populate(lbProcedure *p, Slicekind != Type_DynamicArray && lb_is_elem_const(elem, et)) { + elem_index++; continue; } lbValue field_expr = lb_build_expr(p, elem); - GB_ASSERT(!is_type_tuple(field_expr.type)); - lbValue ev = lb_emit_conv(p, field_expr, et); + if (is_type_tuple(field_expr.type)) { + TypeTuple *tt = &field_expr.type->Tuple; + for_array(jj, tt->variables) { + lbValue sub_field_expr = lb_emit_struct_ev(p, field_expr, cast(i32)jj); + lbValue ev = lb_emit_conv(p, sub_field_expr, et); - lbCompoundLitElemTempData data = {}; - data.value = ev; - if (bt->kind == Type_Matrix) { - data.elem_index = matrix_row_major_index_to_offset(bt, i); + lbCompoundLitElemTempData data = {}; + data.value = ev; + if (bt->kind == Type_Matrix) { + data.elem_index = matrix_row_major_index_to_offset(bt, elem_index++); + } else { + data.elem_index = elem_index++; + } + array_add(temp_data, data); + } } else { - data.elem_index = i; + lbValue ev = lb_emit_conv(p, field_expr, et); + + lbCompoundLitElemTempData data = {}; + data.value = ev; + if (bt->kind == Type_Matrix) { + data.elem_index = matrix_row_major_index_to_offset(bt, elem_index++); + } else { + data.elem_index = elem_index++; + } + array_add(temp_data, data); } - array_add(temp_data, data); } } }