Browse Source

[typer] Fix types in null coal null check. (#11726)

Zeta 1 year ago
parent
commit
3fc2152a87
2 changed files with 7 additions and 2 deletions
  1. 3 2
      src/typing/typer.ml
  2. 4 0
      tests/unit/src/unit/issues/Issue11425.hx

+ 3 - 2
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
 			| TAbstract({a_path = [],"Null"},[t]) -> tmin
 			| _ -> follow_null tmin
 			| _ -> follow_null tmin
 		in
 		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_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
 		let e_if = mk (TIf(e_cond,cast e1,Some e2)) iftype p in
 		vr#to_texpr e_if
 		vr#to_texpr e_if

+ 4 - 0
tests/unit/src/unit/issues/Issue11425.hx

@@ -56,8 +56,12 @@ class Issue11425 extends Test {
 		// generated: variant1 != null ? Variant.toFloat(variant1) : 1.0
 		// generated: variant1 != null ? Variant.toFloat(variant1) : 1.0
 		var testValue9:Float = variant1 ?? cast 1.0; // Works fine.
 		var testValue9:Float = variant1 ?? cast 1.0; // Works fine.
 		// generated: Variant.toFloat(variant1 != null ? variant1 : 1.0)
 		// 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.
 		var testValue10 = variant1 ?? cast 1.0; // Works fine.
 		// generated: variant1 != null ? variant1 : 1.0
 		// generated: variant1 != null ? variant1 : 1.0
+		#end
 
 
 		utest.Assert.pass();
 		utest.Assert.pass();
 	}
 	}