From c2684634132c0af6f2bd042280fcf667576fceb5 Mon Sep 17 00:00:00 2001 From: jakubtomsu <66876057+jakubtomsu@users.noreply.github.com> Date: Sun, 1 Oct 2023 12:21:44 +0200 Subject: [PATCH 1/3] Allow zero-length enumerated arrays --- src/check_type.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/check_type.cpp b/src/check_type.cpp index a43c296a603..3360df040fa 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -2810,7 +2810,7 @@ gb_internal bool check_type_internal(CheckerContext *ctx, Ast *e, Type **type, T } } - if (!is_sparse && t->EnumeratedArray.count > bt->Enum.fields.count) { + if (!is_sparse && t->EnumeratedArray.count > bt->Enum.fields.count && bt->Enum.fields.count > 0) { error(e, "Non-contiguous enumeration used as an index in an enumerated array"); long long ea_count = cast(long long)t->EnumeratedArray.count; long long enum_count = cast(long long)bt->Enum.fields.count; From 1a57ad233d06e52e486f717ddea1ce15934657eb Mon Sep 17 00:00:00 2001 From: jakubtomsu <66876057+jakubtomsu@users.noreply.github.com> Date: Sun, 1 Oct 2023 13:09:08 +0200 Subject: [PATCH 2/3] Fix field count in enumerated array type info --- src/check_type.cpp | 2 +- src/types.cpp | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/check_type.cpp b/src/check_type.cpp index 3360df040fa..ee12b90ca39 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -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) { diff --git a/src/types.cpp b/src/types.cpp index f3062365fed..574e628c5c7 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -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; @@ -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; } From 394c12f68dc380aea93ad447f5f348383811d09f Mon Sep 17 00:00:00 2001 From: jakubtomsu <66876057+jakubtomsu@users.noreply.github.com> Date: Sun, 1 Oct 2023 13:13:09 +0200 Subject: [PATCH 3/3] Remove unnecessary check zero fields check --- src/check_type.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/check_type.cpp b/src/check_type.cpp index ee12b90ca39..d66b196bc64 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -2810,7 +2810,7 @@ gb_internal bool check_type_internal(CheckerContext *ctx, Ast *e, Type **type, T } } - if (!is_sparse && t->EnumeratedArray.count > bt->Enum.fields.count && bt->Enum.fields.count > 0) { + if (!is_sparse && t->EnumeratedArray.count > bt->Enum.fields.count) { error(e, "Non-contiguous enumeration used as an index in an enumerated array"); long long ea_count = cast(long long)t->EnumeratedArray.count; long long enum_count = cast(long long)bt->Enum.fields.count;