| ... | @@ -1742,26 +1742,36 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) { | ... | @@ -1742,26 +1742,36 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) { |
| 1742 | | 1742 | |
| 1743 | ZigType *most_aligned_union_member = nullptr; | 1743 | ZigType *most_aligned_union_member = nullptr; |
| 1744 | uint32_t field_count = union_type->data.unionation.src_field_count; | 1744 | uint32_t field_count = union_type->data.unionation.src_field_count; |
| | 1745 | bool packed = union_type->data.unionation.layout == ContainerLayoutPacked; |
| 1745 | | 1746 | |
| 1746 | for (uint32_t i = 0; i < field_count; i += 1) { | 1747 | for (uint32_t i = 0; i < field_count; i += 1) { |
| 1747 | TypeUnionField *union_field = &union_type->data.unionation.fields[i]; | 1748 | TypeUnionField *field = &union_type->data.unionation.fields[i]; |
| 1748 | ZigType *field_type = union_field->type_entry; | 1749 | if (field->gen_index == UINT32_MAX) |
| | 1750 | continue; |
| 1749 | | 1751 | |
| 1750 | if ((err = type_resolve(g, field_type, ResolveStatusAlignmentKnown))) { | 1752 | size_t this_field_align; |
| 1751 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; | 1753 | if (packed) { |
| 1752 | return ErrorSemanticAnalyzeFail; | 1754 | // TODO: https://github.com/ziglang/zig/issues/1512 |
| 1753 | } | 1755 | this_field_align = 1; |
| | 1756 | // This is the same hack as resolve_struct_alignment. See the comment there. |
| | 1757 | } else if (field->type_entry == nullptr) { |
| | 1758 | this_field_align = g->builtin_types.entry_usize->abi_align; |
| | 1759 | } else { |
| | 1760 | if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) { |
| | 1761 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| | 1762 | return ErrorSemanticAnalyzeFail; |
| | 1763 | } |
| 1754 | | 1764 | |
| 1755 | if (type_is_invalid(union_type)) | 1765 | if (union_type->data.unionation.resolve_status == ResolveStatusInvalid) |
| 1756 | return ErrorSemanticAnalyzeFail; | 1766 | return ErrorSemanticAnalyzeFail; |
| 1757 | | 1767 | |
| 1758 | if (!type_has_bits(field_type)) | 1768 | this_field_align = field->type_entry->abi_align; |
| 1759 | continue; | 1769 | } |
| 1760 | | 1770 | |
| 1761 | if (most_aligned_union_member == nullptr || | 1771 | if (most_aligned_union_member == nullptr || |
| 1762 | field_type->abi_align > most_aligned_union_member->abi_align) | 1772 | this_field_align > most_aligned_union_member->abi_align) |
| 1763 | { | 1773 | { |
| 1764 | most_aligned_union_member = field_type; | 1774 | most_aligned_union_member = field->type_entry; |
| 1765 | } | 1775 | } |
| 1766 | } | 1776 | } |
| 1767 | | 1777 | |
| ... | @@ -2395,6 +2405,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { | ... | @@ -2395,6 +2405,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { |
| 2395 | TypeUnionField *union_field = &union_type->data.unionation.fields[i]; | 2405 | TypeUnionField *union_field = &union_type->data.unionation.fields[i]; |
| 2396 | union_field->name = field_node->data.struct_field.name; | 2406 | union_field->name = field_node->data.struct_field.name; |
| 2397 | union_field->decl_node = field_node; | 2407 | union_field->decl_node = field_node; |
| | 2408 | union_field->gen_index = UINT32_MAX; |
| 2398 | | 2409 | |
| 2399 | auto field_entry = union_type->data.unionation.fields_by_name.put_unique(union_field->name, union_field); | 2410 | auto field_entry = union_type->data.unionation.fields_by_name.put_unique(union_field->name, union_field); |
| 2400 | if (field_entry != nullptr) { | 2411 | if (field_entry != nullptr) { |