Răsfoiți Sursa

[typer] don't infer Null<?> if it already is Null<?>

see #11286
Simon Krajewski 1 an în urmă
părinte
comite
a0ac5ad4eb

+ 3 - 2
src/typing/typer.ml

@@ -329,8 +329,9 @@ let rec type_ident_raise ctx i p mode with_type =
 				let t = match with_type with
 					| WithType.WithType(t,_) ->
 						begin match follow t with
-						| TMono r ->
-							(* If our expected type is a monomorph, bind it to Null<?>. *)
+						| TMono r when not (is_nullable t) ->
+							(* If our expected type is a monomorph, bind it to Null<?>. The is_nullable check is here because
+							   the expected type could already be Null<?>, in which case we don't want to double-wrap (issue #11286). *)
 							Monomorph.do_bind r (tnull())
 						| _ ->
 							(* Otherwise there's no need to create a monomorph, we can just type the null literal

+ 14 - 0
tests/misc/projects/Issue11286/Main.hx

@@ -0,0 +1,14 @@
+function main() {
+	var a = null;
+	if (a == null) a = 10;
+	$type(a); // Null<Null<Int>>
+
+	var b = null;
+	$type(b); // Null<Unknown<0>>
+	b = null;
+	$type(b); // Null<Null<Unknown<0>>>
+	b = null;
+	$type(b); // Null<Null<Null<Unknown<0>>>>
+	b = 10;
+	$type(b); // Null<Null<Null<Int>>>
+}

+ 1 - 0
tests/misc/projects/Issue11286/compile.hxml

@@ -0,0 +1 @@
+--main Main

+ 5 - 0
tests/misc/projects/Issue11286/compile.hxml.stderr

@@ -0,0 +1,5 @@
+Main.hx:4: characters 8-9 : Warning : Null<Int>
+Main.hx:7: characters 8-9 : Warning : Null<Unknown<0>>
+Main.hx:9: characters 8-9 : Warning : Null<Unknown<0>>
+Main.hx:11: characters 8-9 : Warning : Null<Unknown<0>>
+Main.hx:13: characters 8-9 : Warning : Null<Int>