From: Brendan Hansen Date: Fri, 1 Jan 2021 20:17:11 +0000 (-0600) Subject: fix auto-cast bug with overloaded procs X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=34b2dbcacc4b07ee5d32e05ab0b7bb1f89435a47;p=onyx.git fix auto-cast bug with overloaded procs --- diff --git a/bin/onyx b/bin/onyx index 297704e5..27ee833f 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/docs/bugs b/docs/bugs index 583d9344..56bdc2eb 100644 --- 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 { diff --git a/src/onyxutils.c b/src/onyxutils.c index 9308525e..132a43cc 100644 --- a/src/onyxutils.c +++ b/src/onyxutils.c @@ -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) { diff --git a/src/onyxwasm.c b/src/onyxwasm.c index 99d8dac9..a563d589 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -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 index 00000000..1eb3461e --- /dev/null +++ b/tests/overload_with_autocast @@ -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 index 00000000..7788c70f --- /dev/null +++ b/tests/overload_with_autocast.onyx @@ -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"); +}