Переглянути джерело

[typer] infer `null` as `Null<?>`

closes #7736
Simon Krajewski 3 роки тому
батько
коміт
69e428016d

+ 1 - 1
src/typing/typer.ml

@@ -312,7 +312,7 @@ let rec type_ident_raise ctx i p mode with_type =
 		AKExpr (mk (TConst TSuper) t p)
 	| "null" ->
 		if mode = MGet then
-			AKExpr (null (spawn_monomorph ctx p) p)
+			AKExpr (null (ctx.t.tnull (spawn_monomorph ctx p)) p)
 		else
 			AKNo i
 	| _ ->

+ 7 - 6
tests/unit/src/unit/issues/Issue2776.hx

@@ -1,34 +1,35 @@
 package unit.issues;
+
 import unit.Test;
 
 class Issue2776 extends Test {
 	function test() {
 		var type = null;
-		switch(type) {
+		switch (type) {
 			case String:
 			case Int:
 		}
 		unit.HelperMacros.typedAs(type, getClassT());
 
 		var type = null;
-		switch(type) {
+		switch (type) {
 			case haxe.macro.Expr.ExprDef:
 			case haxe.macro.Expr.Constant:
 		}
 		unit.HelperMacros.typedAs(type, getEnumT());
 
 		var type = null;
-		t(unit.HelperMacros.typeError(switch(type) {
+		t(unit.HelperMacros.typeError(switch (type) {
 			case String:
 			case haxe.macro.Expr.ExprDef:
 		}));
 	}
 
-	static function getClassT<T>():Class<T> {
+	static function getClassT<T>():Null<Class<T>> {
 		return (null : Class<T>);
 	}
 
-	static function getEnumT<T>():Enum<T> {
+	static function getEnumT<T>():Null<Enum<T>> {
 		return (null : Enum<T>);
 	}
-}
+}

+ 3 - 3
tests/unit/src/unit/issues/Issue3396.hx

@@ -8,7 +8,7 @@ class Issue3396 extends Test {
 		Reflect.makeVarArgs(f);
 		Reflect.makeVarArgs(f2);
 		var s:String = f([]);
-		unit.HelperMacros.typedAs(r, (r:String));
-		unit.HelperMacros.typedAs(f2([]), (null:Void));
+		unit.HelperMacros.typedAs(r, (r : Null<String>));
+		unit.HelperMacros.typedAs(f2([]), (null : Void));
 	}
-}
+}

+ 19 - 0
tests/unit/src/unit/issues/Issue7736.hx

@@ -0,0 +1,19 @@
+package unit.issues;
+
+class Issue7736 extends Test {
+	@:nullSafety
+	function test() {
+		var found = null;
+		for (i in 0...10) {
+			if (i == 5) {
+				found = i;
+				break;
+			}
+		}
+		unit.HelperMacros.typedAs(found, (null : Null<Int>));
+		if (found != null) {
+			var x:Int = found;
+			eq(5, x);
+		}
+	}
+}