From: Brendan Hansen Date: Mon, 18 Dec 2023 14:48:29 +0000 (-0600) Subject: fixed: #90 enums not working with `hash.hash` by default X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=33e59ab333ed4472a54794fa5d080142eed80aae;p=onyx.git fixed: #90 enums not working with `hash.hash` by default --- diff --git a/compiler/src/symres.c b/compiler/src/symres.c index 0a9947fc..d112cf06 100644 --- a/compiler/src/symres.c +++ b/compiler/src/symres.c @@ -1324,6 +1324,8 @@ static SymresStatus symres_enum(AstEnumType* enum_node) { if (enum_node->scope == NULL) { enum_node->scope = scope_create(context.ast_alloc, current_scope, enum_node->token->pos); + symbol_raw_introduce(enum_node->scope, "__backing_type", enum_node->token->pos, (AstNode *) enum_node->backing); + type_build_from_ast(context.ast_alloc, (AstType *) enum_node); } diff --git a/core/hash/hash.onyx b/core/hash/hash.onyx index a93aca9f..1a5e596a 100644 --- a/core/hash/hash.onyx +++ b/core/hash/hash.onyx @@ -1,5 +1,7 @@ package core.hash +use core.intrinsics.types {type_is_enum} + // // DEPRECATED: to_u32 is the old name that kind of // makes sense, but is a little unintuitive. Use @@ -41,6 +43,10 @@ hash :: #match -> u32 { (key: type_expr) -> u32 { return hash(cast(u32) key); }, (key: bool) -> u32 { return 1 if key else 0; }, + (key: $T/type_is_enum) -> u32 { + return hash(cast(T.__backing_type) key); + }, + #order 10000 macro (key: $T/HasHashMethod) => key->hash() } diff --git a/tests/map_enum_key b/tests/map_enum_key new file mode 100644 index 00000000..01f6893a --- /dev/null +++ b/tests/map_enum_key @@ -0,0 +1,9 @@ +(/Users/brendan/.onyx/core/hash/hash.onyx:46,41) Failed to satisfy constraint where t is of type 'Fruit'. + 46 | macro (key: $T/type_is_enum) -> u32 { + ^ +(/Users/brendan/.onyx/core/container/map.onyx:19,66) Here is where the interface was used. + 19 | Map :: struct (Key_Type: type_expr, Value_Type: type_expr) where ValidKey(Key_Type) { + ^~~~~~~~ +(/Users/brendan/dev/onyx/tests/map_enum_key.onyx:18,18) Here is the code that caused this constraint to be checked. + 18 | m := make(Map(Fruit, str)); + ^ diff --git a/tests/map_enum_key.onyx b/tests/map_enum_key.onyx new file mode 100644 index 00000000..30757bb5 --- /dev/null +++ b/tests/map_enum_key.onyx @@ -0,0 +1,33 @@ +package main + +use core {*} + +Fruit :: enum { + Apple; + Banana; + Orange; +} + +Fruit_64 :: enum (i64) { + Apple; + Banana; + Orange; +} + +main :: () { + m := make(Map(Fruit, str)); + + m->put(.Apple, "This is apple."); + m->put(.Banana, "This is banana."); + m->put(.Orange, "This is orange."); + + println(m); + + m2 := make(Map(Fruit, str)); + + m2->put(.Apple, "This is apple 64."); + m2->put(.Banana, "This is banana 64."); + m2->put(.Orange, "This is orange 64."); + + println(m2); +}