Bit negating const zero flags would give an incorrect result. #1213

This commit is contained in:
Christoffer Lerno
2024-06-28 16:43:57 +02:00
parent 7dcd1618d8
commit 492f83f5e2
3 changed files with 21 additions and 1 deletions

View File

@@ -23,6 +23,7 @@
- Bit negate does implicit integer promotion. - Bit negate does implicit integer promotion.
- Bitstructs, unions and flexible arrays now correctly emitted in headers. - Bitstructs, unions and flexible arrays now correctly emitted in headers.
- Fix distinct inline conversions. - Fix distinct inline conversions.
- Bit negating const zero flags would give an incorrect result.
### Stdlib changes ### Stdlib changes
- Added `remove_first_item` `remove_last_item` and `remove_item` as aliases for the `match` functions. - Added `remove_first_item` `remove_last_item` and `remove_item` as aliases for the `match` functions.

View File

@@ -568,6 +568,7 @@ void sema_invert_bitstruct_const_initializer(ConstInitializer *initializer)
if (init->kind == CONST_INIT_ZERO) if (init->kind == CONST_INIT_ZERO)
{ {
init->init_value = expr_new_const_bool(INVALID_SPAN, init->type, true); init->init_value = expr_new_const_bool(INVALID_SPAN, init->type, true);
init->kind = CONST_INIT_VALUE;
continue; continue;
} }
init->init_value->const_expr.b = !init->init_value->const_expr.b; init->init_value->const_expr.b = !init->init_value->const_expr.b;
@@ -922,11 +923,11 @@ static inline void sema_update_const_initializer_with_designator_array(ConstInit
ConstInitializer **array_elements = const_init->init_array.elements; ConstInitializer **array_elements = const_init->init_array.elements;
unsigned array_count = vec_size(array_elements); unsigned array_count = vec_size(array_elements);
MemberIndex insert_index = 0; MemberIndex insert_index = 0;
for (MemberIndex index = low_index; index <= high_index; index++) for (MemberIndex index = low_index; index <= high_index; index++)
{ {
assert(insert_index >= array_count || array_elements);
// Walk to the insert point or until we reached the end of the array. // Walk to the insert point or until we reached the end of the array.
while (insert_index < array_count && array_elements[insert_index]->init_array_value.index < index) while (insert_index < array_count && array_elements[insert_index]->init_array_value.index < index)
{ {

View File

@@ -69,4 +69,22 @@ fn void test_inc_array()
assert(x.a++ == 10); assert(x.a++ == 10);
assert(x.a == 11, "Value was %d", x.a); assert(x.a == 11, "Value was %d", x.a);
assert(--x.a == 10); assert(--x.a == 10);
}
bitstruct Flags : int
{
bool flag1;
bool flag2;
}
fn void negate()
{
Flags flags;
flags = ~flags;
assert(~0 == (int)flags);
flags = ~Flags {};
assert(3 == (int)flags);
const Flags FLAGS = {.flag1 };
flags = ~FLAGS;
assert(2 == (int)flags);
} }