fix auto-cast bug with overloaded procs
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 1 Jan 2021 20:17:11 +0000 (14:17 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 1 Jan 2021 20:17:11 +0000 (14:17 -0600)
bin/onyx
docs/bugs
src/onyxutils.c
src/onyxwasm.c
tests/overload_with_autocast [new file with mode: 0644]
tests/overload_with_autocast.onyx [new file with mode: 0644]

index 297704e57c3bf140828b5b1e883f1fafe5bd0bc3..27ee833f4b9a6119cefa6523676a68ceec73d127 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 583d9344f70a71a090e214fd41f3db2694524728..56bdc2eb02eb3d005aac1815a9ca638dd2c96ccc 100644 (file)
--- a/docs/bugs
+++ b/docs/bugs
@@ -1,6 +1,6 @@
 List of known bugs:
 
-[ ] Using an auto-cast on an argument when calling an overloaded proc leads
+[X] Using an auto-cast on an argument when calling an overloaded proc leads
     to an unexpected error. Take the following example:
     ```
     overloaded :: proc {
index 9308525eb71f4f3d271fe2ff3aca490e6cfa526f..132a43ccd815263852ec4fd7bc8edb493844da42 100644 (file)
@@ -875,7 +875,7 @@ b32 convert_numlit_to_type(AstNumLit* num, Type* type) {
             if (type->Basic.size == 8) {
                 // TODO(Brendan): Check these boundary conditions
                 if (bh_abs(num->value.l) >= (1ull << 52)) {
-                    onyx_report_error(num->token->pos, "Integer '%l' does not fit in 32-bit float exactly.", num->value.l);
+                    onyx_report_error(num->token->pos, "Integer '%l' does not fit in 64-bit float exactly.", num->value.l);
                     return 0;
                 }
 
@@ -932,7 +932,7 @@ b32 type_check_or_auto_cast(AstTyped** pnode, Type* type) {
         // If the node is an auto cast, we convert it to a cast node which will reports errors if
         // the cast is illegal in the code generation.
         ((AstUnaryOp *) node)->type = type;
-        ((AstUnaryOp *) node)->operation = Unary_Op_Cast;
+        // ((AstUnaryOp *) node)->operation = Unary_Op_Cast;
         return 1;
     }
     else if (node->kind == Ast_Kind_NumLit) {
index 99d8dac926711906982ca7ca51e55f1153b10ebb..a563d589de751785ab510c08b38ee33bd1e11808 100644 (file)
@@ -1346,12 +1346,17 @@ EMIT_FUNC(unaryop, AstUnaryOp* unop) {
 
             TypeBasic* type = &unop->type->Basic;
 
-            if (type->kind == Basic_Kind_I32 || type->kind == Basic_Kind_U32
-                    || type->kind == Basic_Kind_I16 || type->kind == Basic_Kind_U16
-                    || type->kind == Basic_Kind_I8 || type->kind == Basic_Kind_U8) {
+            if (type->kind == Basic_Kind_I8 || type->kind == Basic_Kind_U8) {
+                WID(WI_I32_CONST, 0xff);
+                WI(WI_I32_XOR);
+            }
+            else if (type->kind == Basic_Kind_I16 || type->kind == Basic_Kind_U16) {
+                WID(WI_I32_CONST, 0xffff);
+                WI(WI_I32_XOR);
+            }
+            else if (type->kind == Basic_Kind_I32 || type->kind == Basic_Kind_U32) {
                 WID(WI_I32_CONST, 0xffffffff);
                 WI(WI_I32_XOR);
-
             }
             else if (type->kind == Basic_Kind_I64 || type->kind == Basic_Kind_U64) {
                 WIL(WI_I64_CONST, 0xffffffffffffffff);
@@ -1364,7 +1369,7 @@ EMIT_FUNC(unaryop, AstUnaryOp* unop) {
         case Unary_Op_Cast: emit_cast(mod, &code, unop); break;
 
         // NOTE: Any remaining auto casts can be ignored since it means that a cast was not necessary. - brendanfh 2020/09/19
-        case Unary_Op_Auto_Cast: emit_expression(mod, &code, unop->expr); break;
+        case Unary_Op_Auto_Cast: emit_cast(mod, &code, unop); break;
     }
 
     *pcode = code;
diff --git a/tests/overload_with_autocast b/tests/overload_with_autocast
new file mode 100644 (file)
index 0000000..1eb3461
--- /dev/null
@@ -0,0 +1,2 @@
+Called i32, i32
+Called f32, str
diff --git a/tests/overload_with_autocast.onyx b/tests/overload_with_autocast.onyx
new file mode 100644 (file)
index 0000000..7788c70
--- /dev/null
@@ -0,0 +1,16 @@
+#include_file "core/std/js"
+
+use package core
+
+overloaded :: proc {
+    proc (x: f32, y: str) do println("Called f32, str"); ,
+    proc (x: i32, y: i32) do println("Called i32, i32"); ,
+}
+
+main :: proc (args: [] cstr) {
+    x: i32 = 1234;
+    overloaded(~~x, 4);
+
+    y: i32 = 1234;
+    overloaded(~~x, "Test");
+}