From 1479c28b496e7c1db134b51f23dd2eb934b123bb Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Mon, 16 Mar 2020 18:42:01 +0100 Subject: ir: Correct ABI size calculation for arrays Zero-length array with a sentinel may not have zero size. Closes #4749 --- src/analyze.cpp | 17 +++++++---------- src/codegen.cpp | 4 +++- 2 files changed, 10 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/analyze.cpp b/src/analyze.cpp index 33d28269b9..d0f8979c79 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -803,13 +803,7 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size, Zi } buf_appendf(&entry->name, "]%s", buf_ptr(&child_type->name)); - size_t full_array_size; - if (array_size == 0) { - full_array_size = 0; - } else { - full_array_size = array_size + ((sentinel != nullptr) ? 1 : 0); - } - + size_t full_array_size = array_size + ((sentinel != nullptr) ? 1 : 0); entry->size_in_bits = child_type->size_in_bits * full_array_size; entry->abi_align = child_type->abi_align; entry->abi_size = child_type->abi_size * full_array_size; @@ -1197,7 +1191,8 @@ Error type_val_resolve_zero_bits(CodeGen *g, ZigValue *type_val, ZigType *parent LazyValueArrayType *lazy_array_type = reinterpret_cast(type_val->data.x_lazy); - if (lazy_array_type->length < 1) { + // The sentinel counts as an extra element + if (lazy_array_type->length == 0 && lazy_array_type->sentinel == nullptr) { *is_zero_bits = true; return ErrorNone; } @@ -1452,7 +1447,8 @@ static OnePossibleValue type_val_resolve_has_one_possible_value(CodeGen *g, ZigV case LazyValueIdArrayType: { LazyValueArrayType *lazy_array_type = reinterpret_cast(type_val->data.x_lazy); - if (lazy_array_type->length < 1) + // The sentinel counts as an extra element + if (lazy_array_type->length == 0 && lazy_array_type->sentinel == nullptr) return OnePossibleValueYes; return type_val_resolve_has_one_possible_value(g, lazy_array_type->elem_type->value); } @@ -5739,7 +5735,8 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) { case ZigTypeIdUnreachable: return OnePossibleValueYes; case ZigTypeIdArray: - if (type_entry->data.array.len == 0) + // The sentinel counts as an extra element + if (type_entry->data.array.len == 0 && type_entry->data.array.sentinel == nullptr) return OnePossibleValueYes; return type_has_one_possible_value(g, type_entry->data.array.child_type); case ZigTypeIdStruct: diff --git a/src/codegen.cpp b/src/codegen.cpp index dc6fe04cb4..75f3223250 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -3584,7 +3584,9 @@ static bool value_is_all_undef(CodeGen *g, ZigValue *const_val) { } return true; } else if (const_val->type->id == ZigTypeIdArray) { - return value_is_all_undef_array(g, const_val, const_val->type->data.array.len); + const size_t full_len = const_val->type->data.array.len + + (const_val->type->data.array.sentinel != nullptr); + return value_is_all_undef_array(g, const_val, full_len); } else if (const_val->type->id == ZigTypeIdVector) { return value_is_all_undef_array(g, const_val, const_val->type->data.vector.len); } else { -- cgit v1.2.3 From 63a4dbc30d3ef3c7f8a8c6a2ba2087eaab8b830a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 18 Mar 2020 11:11:41 -0400 Subject: array sentinel does not count towards type_has_one_possible_value --- src/analyze.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/analyze.cpp b/src/analyze.cpp index d0f8979c79..fbd7d85ac1 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -1447,8 +1447,7 @@ static OnePossibleValue type_val_resolve_has_one_possible_value(CodeGen *g, ZigV case LazyValueIdArrayType: { LazyValueArrayType *lazy_array_type = reinterpret_cast(type_val->data.x_lazy); - // The sentinel counts as an extra element - if (lazy_array_type->length == 0 && lazy_array_type->sentinel == nullptr) + if (lazy_array_type->length == 0) return OnePossibleValueYes; return type_val_resolve_has_one_possible_value(g, lazy_array_type->elem_type->value); } @@ -5735,8 +5734,7 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) { case ZigTypeIdUnreachable: return OnePossibleValueYes; case ZigTypeIdArray: - // The sentinel counts as an extra element - if (type_entry->data.array.len == 0 && type_entry->data.array.sentinel == nullptr) + if (type_entry->data.array.len == 0) return OnePossibleValueYes; return type_has_one_possible_value(g, type_entry->data.array.child_type); case ZigTypeIdStruct: -- cgit v1.2.3