From 70905a81e3ff0d40b69338c80d86aefca4a8a362 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Sat, 2 Jul 2022 15:07:57 +0200 Subject: [PATCH] :art: No implicit bool2 float --- src/lib/prim.c | 13 +++++++++++++ tests/bool/bool2float.gw | 3 +++ 2 files changed, 16 insertions(+) create mode 100644 tests/bool/bool2float.gw diff --git a/src/lib/prim.c b/src/lib/prim.c index a6dc951a..d96470b7 100644 --- a/src/lib/prim.c +++ b/src/lib/prim.c @@ -257,6 +257,16 @@ static GACK(gack_bool) { INTERP_PRINTF("%s", *(m_uint *)VALUE ? "true" : "false"); } +static OP_CHECK(bool2float) { + struct Implicit *impl = (struct Implicit *)data; + if(!env->context->error) { + gwerr_basic("Can't implicitely cast {G+}bool{0} to {G+}float{0}", NULL, "Did you forget a cast?", + env->name, impl->e->pos, 0); + env_set_error(env, true); + } + return env->gwion->type[et_error]; +} + static GWION_IMPORT(int_values) { DECL_OB(const Type, t_bool, = gwi_mk_type(gwi, "bool", SZ_INT, "int")); GWI_BB(gwi_set_global_type(gwi, t_bool, et_bool)) @@ -269,6 +279,9 @@ static GWION_IMPORT(int_values) { GWI_BB(gwi_oper_add(gwi, opck_unary_meta)) GWI_BB(gwi_oper_add(gwi, opck_int_not)) GWI_BB(gwi_oper_end(gwi, "!", IntNot)) + GWI_BB(gwi_oper_ini(gwi, "bool", "float", NULL)) + GWI_BB(gwi_oper_add(gwi, bool2float)); + GWI_BB(gwi_oper_end(gwi, "@implicit", NULL)) struct SpecialId_ spid = { .type = t_bool, .exec = RegPushMaybe, .is_const = 1}; gwi_specialid(gwi, "maybe", &spid); diff --git a/tests/bool/bool2float.gw b/tests/bool/bool2float.gw new file mode 100644 index 00000000..ea857f9e --- /dev/null +++ b/tests/bool/bool2float.gw @@ -0,0 +1,3 @@ +fun void foo(float f) {} + +true => foo; -- 2.43.0