Skip to content

Commit

Permalink
Merge pull request #2836 from jakubtomsu/fix-zero-length-enum-array
Browse files Browse the repository at this point in the history
Allow zero-length enum array (to stay consistent with `[0]T`)
  • Loading branch information
gingerBill authored Oct 6, 2023
2 parents 2cca005 + 394c12f commit 96778c6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/check_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2797,7 +2797,7 @@ gb_internal bool check_type_internal(CheckerContext *ctx, Ast *e, Type **type, T
Type *bt = base_type(index);
GB_ASSERT(bt->kind == Type_Enum);

Type *t = alloc_type_enumerated_array(elem, index, bt->Enum.min_value, bt->Enum.max_value, Token_Invalid);
Type *t = alloc_type_enumerated_array(elem, index, bt->Enum.min_value, bt->Enum.max_value, bt->Enum.fields.count, Token_Invalid);

bool is_sparse = false;
if (at->tag != nullptr) {
Expand Down
8 changes: 6 additions & 2 deletions src/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ gb_internal Type *alloc_type_matrix(Type *elem, i64 row_count, i64 column_count,
}


gb_internal Type *alloc_type_enumerated_array(Type *elem, Type *index, ExactValue const *min_value, ExactValue const *max_value, TokenKind op) {
gb_internal Type *alloc_type_enumerated_array(Type *elem, Type *index, ExactValue const *min_value, ExactValue const *max_value, isize count, TokenKind op) {
Type *t = alloc_type(Type_EnumeratedArray);
t->EnumeratedArray.elem = elem;
t->EnumeratedArray.index = index;
Expand All @@ -993,7 +993,11 @@ gb_internal Type *alloc_type_enumerated_array(Type *elem, Type *index, ExactValu
gb_memmove(t->EnumeratedArray.max_value, max_value, gb_size_of(ExactValue));
t->EnumeratedArray.op = op;

t->EnumeratedArray.count = 1 + exact_value_to_i64(exact_value_sub(*max_value, *min_value));
if (count == 0) {
t->EnumeratedArray.count = 0;
} else {
t->EnumeratedArray.count = 1 + exact_value_to_i64(exact_value_sub(*max_value, *min_value));
}
return t;
}

Expand Down

0 comments on commit 96778c6

Please sign in to comment.