From 5db762ef446720a093ef6edae2d1a86053f40487 Mon Sep 17 00:00:00 2001 From: Apprentice-Alchemist <53486764+Apprentice-Alchemist@users.noreply.github.com> Date: Thu, 18 Jul 2024 11:52:32 +0200 Subject: [PATCH] [typer] Fix types in null coal null check. --- src/typing/typer.ml | 5 +++-- tests/unit/src/unit/issues/Issue11425.hx | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/typing/typer.ml b/src/typing/typer.ml index 7a586d178a4..a2b74023129 100644 --- a/src/typing/typer.ml +++ b/src/typing/typer.ml @@ -1871,8 +1871,9 @@ and type_expr ?(mode=MGet) ctx (e,p) (with_type:WithType.t) = | TAbstract({a_path = [],"Null"},[t]) -> tmin | _ -> follow_null tmin in - let e1 = vr#as_var "tmp" {e1 with etype = ctx.t.tnull tmin} in - let e_null = Builder.make_null e1.etype e1.epos in + let e1_null_t = if is_nullable e1.etype then e1.etype else ctx.t.tnull e1.etype in + let e1 = vr#as_var "tmp" {e1 with etype = e1_null_t} in + let e_null = Builder.make_null e1_null_t e1.epos in let e_cond = mk (TBinop(OpNotEq,e1,e_null)) ctx.t.tbool e1.epos in let e_if = mk (TIf(e_cond,cast e1,Some e2)) iftype p in vr#to_texpr e_if diff --git a/tests/unit/src/unit/issues/Issue11425.hx b/tests/unit/src/unit/issues/Issue11425.hx index 07bfd56df4b..e30eefe53ef 100644 --- a/tests/unit/src/unit/issues/Issue11425.hx +++ b/tests/unit/src/unit/issues/Issue11425.hx @@ -56,8 +56,12 @@ class Issue11425 extends Test { // generated: variant1 != null ? Variant.toFloat(variant1) : 1.0 var testValue9:Float = variant1 ?? cast 1.0; // Works fine. // generated: Variant.toFloat(variant1 != null ? variant1 : 1.0) + // testValue10 is inferred as Variant + // and hxcpp does not like casting Float to that + #if !cpp var testValue10 = variant1 ?? cast 1.0; // Works fine. // generated: variant1 != null ? variant1 : 1.0 + #end utest.Assert.pass(); }