From cc1bc58ed039b6e7715863f8fd9f356befa779d4 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sun, 24 Jul 2022 15:39:11 +0200 Subject: [PATCH] Allow using enums for indexing. --- src/compiler/sema_expr.c | 9 +++++++-- test/test_suite/arrays/array_indexing.c3 | 11 +++++++++++ test/test_suite2/arrays/array_indexing.c3 | 11 +++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 test/test_suite/arrays/array_indexing.c3 create mode 100644 test/test_suite2/arrays/array_indexing.c3 diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index b228ba113..183a12956 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -748,7 +748,9 @@ static inline bool sema_type_error_on_binop(Expr *expr) static bool expr_cast_to_index(Expr *index) { - switch (index->type->canonical->type_kind) + Type *type = index->type->canonical; + RETRY: + switch (type->type_kind) { case TYPE_I8: case TYPE_I16: @@ -764,8 +766,11 @@ static bool expr_cast_to_index(Expr *index) SEMA_ERROR(index, "You need to explicitly cast this to a uint or ulong."); return false; case TYPE_I128: - SEMA_ERROR(index, "You need to explicitly cast this to a int or long."); + SEMA_ERROR(index, "index->type->canonical this to an int or long."); return false; + case TYPE_ENUM: + type = type->decl->enums.type_info->type->canonical; + goto RETRY; default: SEMA_ERROR(index, "Cannot implicitly convert '%s' to an index.", type_to_error_string(index->type)); return false; diff --git a/test/test_suite/arrays/array_indexing.c3 b/test/test_suite/arrays/array_indexing.c3 new file mode 100644 index 000000000..72cb162f6 --- /dev/null +++ b/test/test_suite/arrays/array_indexing.c3 @@ -0,0 +1,11 @@ +enum Foo : int +{ + TEST +} + +int[100] a = { [Foo.TEST] = 123 }; + +fn void test() +{ + a[Foo.TEST] = 33; +} \ No newline at end of file diff --git a/test/test_suite2/arrays/array_indexing.c3 b/test/test_suite2/arrays/array_indexing.c3 new file mode 100644 index 000000000..72cb162f6 --- /dev/null +++ b/test/test_suite2/arrays/array_indexing.c3 @@ -0,0 +1,11 @@ +enum Foo : int +{ + TEST +} + +int[100] a = { [Foo.TEST] = 123 }; + +fn void test() +{ + a[Foo.TEST] = 33; +} \ No newline at end of file