authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-02 17:40:01-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-02 18:31:19-04:00
log9780fd59f063d23e69ceadf4305453531b704c11
tree175b9977bca02b13a61521e1a1ec483f4eb77380
parentcb0241fe44e347bd14fd5568a240f1237dafacc3
signature Commit is signed but in an unrecognized format.

put the alignment hack in for unions too. fixes std tests


1 files changed, 23 insertions(+), 12 deletions(-)

src/analyze.cpp+23-12
...@@ -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) {
17421742
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;
17451746
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;
17491751
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 }
17541764
1755 if (type_is_invalid(union_type))1765 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid)
1756 return ErrorSemanticAnalyzeFail;1766 return ErrorSemanticAnalyzeFail;
17571767
1758 if (!type_has_bits(field_type))1768 this_field_align = field->type_entry->abi_align;
1759 continue;1769 }
17601770
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 }
17671777
...@@ -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;
23982409
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) {