From b7ae5dce8b444a6b2d2a1ef8a149e1edbf096c99 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Fri, 16 May 2025 16:12:37 +0200 Subject: [PATCH] Deprecate `MyEnum.elements`. --- lib/std/collections/enummap.c3 | 2 +- lib/std/collections/enumset.c3 | 5 +++-- lib/std/core/runtime.c3 | 6 ++---- releasenotes.md | 1 + src/compiler/sema_expr.c | 3 ++- test/test_suite/enumerations/compile_time.c3t | 2 +- test/test_suite/functions/test_regression.c3t | 4 ++-- test/test_suite/functions/test_regression_mingw.c3t | 4 ++-- 8 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/std/collections/enummap.c3 b/lib/std/collections/enummap.c3 index 8e5c26db3..f492a595c 100644 --- a/lib/std/collections/enummap.c3 +++ b/lib/std/collections/enummap.c3 @@ -6,7 +6,7 @@ import std::io; struct EnumMap (Printable) { - ValueType[Enum.len] values; + ValueType[Enum.values.len] values; } fn void EnumMap.init(&self, ValueType init_value) diff --git a/lib/std/collections/enumset.c3 b/lib/std/collections/enumset.c3 index 416927e19..1663ae6d1 100644 --- a/lib/std/collections/enumset.c3 +++ b/lib/std/collections/enumset.c3 @@ -8,9 +8,10 @@ module std::collections::enumset{Enum}; import std::io; -alias EnumSetType @private = $typefrom(type_for_enum_elements(Enum.elements)); +const ENUM_COUNT @private = Enum.values.len; +alias EnumSetType @private = $typefrom(type_for_enum_elements(ENUM_COUNT)); -const IS_CHAR_ARRAY = Enum.elements > 128; +const IS_CHAR_ARRAY = ENUM_COUNT > 128; typedef EnumSet (Printable) = EnumSetType; fn void EnumSet.add(&self, Enum v) diff --git a/lib/std/core/runtime.c3 b/lib/std/core/runtime.c3 index 5105961ac..819bce123 100644 --- a/lib/std/core/runtime.c3 +++ b/lib/std/core/runtime.c3 @@ -24,11 +24,9 @@ struct SliceRaw macro @enum_lookup($Type, #value, value) { - var $elements = $Type.elements; - $for var $i = 0; $i < $elements; $i++: - var $val = $Type.from_ordinal($i); + $foreach $val : $Type.values: if ($val.#value == value) return $val; - $endfor + $endforeach return NOT_FOUND?; } diff --git a/releasenotes.md b/releasenotes.md index 8fa3232a4..79b7263e0 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -11,6 +11,7 @@ - Added `@rnd()` compile time random function (using the `$$rnd()` builtin). #2078 - Add `math::@ceil()` compile time ceil function. #2134 - Improve error message when using keywords as functions/macros/variables #2133. +- Deprecate `MyEnum.elements`. ### Fixes - Assert triggered when casting from `int[2]` to `uint[2]` #2115 diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index f811c4e21..4b00c980e 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -2851,7 +2851,7 @@ INLINE bool sema_expr_analyse_lookup(SemaContext *context, Expr *expr, Expr *tag Decl *decl = tag->type_call_expr.type; if (inline_field) { - if (arg_count != 1) RETURN_SEMA_ERROR(expr, "Expected one (1) argument to 'lookup'."); + if (arg_count != 1) RETURN_SEMA_ERROR(expr, "Expected one (1) argument to 'lookup', did you want 'lookup_field' perhaps?"); } else { @@ -5126,6 +5126,7 @@ static bool sema_expr_rewrite_to_type_property(SemaContext *context, Expr *expr, case TYPE_PROPERTY_ELEMENTS: ASSERT_SPAN(expr, flat->type_kind == TYPE_ENUM); if (!sema_analyse_decl(context, type->decl)) return false; + SEMA_DEPRECATED(expr, ".elements is deprecated. Use .values.len instead."); expr_rewrite_const_int(expr, type_isz, vec_size(flat->decl->enums.values)); return true; case TYPE_PROPERTY_VALUES: diff --git a/test/test_suite/enumerations/compile_time.c3t b/test/test_suite/enumerations/compile_time.c3t index ccea48d73..9e45e1d5a 100644 --- a/test/test_suite/enumerations/compile_time.c3t +++ b/test/test_suite/enumerations/compile_time.c3t @@ -6,7 +6,7 @@ enum MyEnum : short BYE } -int myenum_elements = MyEnum.elements; +int myenum_elements = MyEnum.values.len; int myenum_alignof = MyEnum.alignof; int myenum_sizeof = MyEnum.sizeof; diff --git a/test/test_suite/functions/test_regression.c3t b/test/test_suite/functions/test_regression.c3t index fa8cb2fab..c44a9c41d 100644 --- a/test/test_suite/functions/test_regression.c3t +++ b/test/test_suite/functions/test_regression.c3t @@ -129,9 +129,9 @@ fn void main() } list.free(); - printf("Elements: %d\n", (int)(MyEnum.elements)); + printf("Elements: %d\n", (int)(MyEnum.values.len)); - int elements = MyEnum.elements; + int elements = MyEnum.values.len; printf("Hello\n"); IntArray array; array.push(100); diff --git a/test/test_suite/functions/test_regression_mingw.c3t b/test/test_suite/functions/test_regression_mingw.c3t index 88a22e912..1cd43607f 100644 --- a/test/test_suite/functions/test_regression_mingw.c3t +++ b/test/test_suite/functions/test_regression_mingw.c3t @@ -131,9 +131,9 @@ fn void main() } list.free(); - printf("Elements: %d\n", (int)(MyEnum.elements)); + printf("Elements: %d\n", (int)(MyEnum.values.len)); - int elements = MyEnum.elements; + int elements = MyEnum.values.len; printf("Hello\n"); IntArray array; array.push(100);