fixed: #90 enums not working with `hash.hash` by default
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 18 Dec 2023 14:48:29 +0000 (08:48 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 18 Dec 2023 14:48:29 +0000 (08:48 -0600)
compiler/src/symres.c
core/hash/hash.onyx
tests/map_enum_key [new file with mode: 0644]
tests/map_enum_key.onyx [new file with mode: 0644]

index 0a9947fcf3ba078f6f99624c18d465a28344ce65..d112cf0606cfcba58a0b0ad6019af75e0d58723a 100644 (file)
@@ -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);
     }
 
index a93aca9f5487f00266a8cb6f9adc8e667e6d4fe8..1a5e596a5a1975a2eb278ee53fbe27f669132108 100644 (file)
@@ -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 (file)
index 0000000..01f6893
--- /dev/null
@@ -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 (file)
index 0000000..30757bb
--- /dev/null
@@ -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);
+}