aboutsummaryrefslogtreecommitdiff
path: root/src/check_type.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2019-12-27 16:55:32 +0000
committergingerBill <bill@gingerbill.org>2019-12-27 16:55:32 +0000
commit5ec8dd166a9baab230c235e9f24684197efb5a86 (patch)
tree526a7aed6ce337c5077a48e924b4eaafe7d96b33 /src/check_type.cpp
parent80a32a8182545b71f184e2889414e01ed27cc9da (diff)
Add #partial tag for enumerated arrays to prevent common errors using non-contiguous enumerations
Diffstat (limited to 'src/check_type.cpp')
-rw-r--r--src/check_type.cpp23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/check_type.cpp b/src/check_type.cpp
index bdb5f048b..098c4b76a 100644
--- a/src/check_type.cpp
+++ b/src/check_type.cpp
@@ -3241,11 +3241,32 @@ bool check_type_internal(CheckerContext *ctx, Ast *e, Type **type, Type *named_t
Type *t = alloc_type_enumerated_array(elem, index, bt->Enum.min_value, bt->Enum.max_value, Token_Invalid);
+ bool is_partial = false;
if (at->tag != nullptr) {
GB_ASSERT(at->tag->kind == Ast_BasicDirective);
String name = at->tag->BasicDirective.name;
- error(at->tag, "Invalid tag applied to an enumerated array, got #%.*s", LIT(name));
+ if (name == "partial") {
+ is_partial = true;
+ } else {
+ error(at->tag, "Invalid tag applied to an enumerated array, got #%.*s", LIT(name));
+ }
}
+
+ if (t->EnumeratedArray.count != bt->Enum.fields.count) {
+ if (!is_partial) {
+ 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)t->Enum.fields.count;
+ error_line("\tenumerated array length: %lld\n", ea_count);
+ error_line("\tenum field count: %lld\n", enum_count);
+ error_line("\tSuggestion: prepend #partial to the enumerated array to allow for non-named elements\n");
+ if (2*enum_count < ea_count) {
+ error_line("\tWarning: the number of named elements is much smaller than the length of the array, are you sure this is what you want?\n");
+ error_line("\t this warning will be removed if #partial is applied\n");
+ }
+ }
+ }
+
*type = t;
goto array_end;