Răsfoiți Sursa

[nullSafety] do not skip unsafe cast

Rudy Ges 4 luni în urmă
părinte
comite
5357cc9e67

+ 2 - 7
src/typing/nullSafety.ml

@@ -1240,13 +1240,8 @@ class expr_checker mode immediate_execution report =
 		*)
 		method private check_cast expr to_type p =
 			self#check_expr expr;
-			match to_type with
-				(* untyped cast *)
-				| TMono _ -> ()
-				(* typed cast and type check *)
-				| _ ->
-					if not (self#can_pass_expr expr to_type p) then
-						self#error "Cannot cast nullable value to not nullable type." [p; expr.epos]
+			if not (self#can_pass_expr expr to_type p) then
+				self#error "Cannot cast nullable value to not nullable type." [p; expr.epos]
 		(**
 			Check safety in a function
 		*)

+ 0 - 5
tests/nullsafety/src/cases/TestStrict.hx

@@ -635,11 +635,6 @@ class TestStrict {
 		shouldFail(cast(s, String));
 	}
 
-	static function untypedCast_shouldPass() {
-		var n:Null<String> = null;
-		var s:String = cast n;
-	}
-
 	static function enum_switchOnNullableEnum_shouldFail(e:Null<DummyEnum>) {
 		switch shouldFail(e) {
 			case DummyOne:

+ 13 - 4
tests/server/src/cases/issues/Issue11904.hx

@@ -4,11 +4,12 @@ import haxe.display.Diagnostic;
 
 class Issue11904 extends TestCase {
 	function test(_) {
-		vfs.putContent("Issue11904.hx", getTemplate("issues/Issue11904.hx"));
-		var args = ["-main", "Issue11904", "--js", "no.js", "--no-output"];
+		vfs.putContent("Main.hx", getTemplate("issues/Issue11904/Main.hx"));
+		var args = ["-main", "Main", "--js", "no.js", "--no-output"];
 		runHaxe(args);
-		runHaxeJson([], ServerMethods.Invalidate, {file: new FsPath("Issue11904.hx")});
-		runHaxeJsonCb(args, DisplayMethods.Diagnostics, {file: new FsPath("Issue11904.hx")}, res -> {
+		assertSuccess();
+		runHaxeJson([], ServerMethods.Invalidate, {file: new FsPath("Main.hx")});
+		runHaxeJsonCb(args, DisplayMethods.Diagnostics, {file: new FsPath("Main.hx")}, res -> {
 			Assert.equals(1, res.length);
 			Assert.equals(2, res[0].diagnostics.length);
 
@@ -27,4 +28,12 @@ class Issue11904 extends TestCase {
 			for (d in diag) check(d);
 		});
 	}
+
+	function testUntypedCast(_) {
+		vfs.putContent("Main.hx", getTemplate("issues/Issue11904/Main1.hx"));
+		vfs.putContent("MyStringTools.hx", getTemplate("issues/Issue11904/MyStringTools.hx"));
+		var args = ["-main", "Main", "--js", "no.js", "--no-output"];
+		runHaxe(args);
+		assertErrorMessage("Null safety: Cannot unify String with { charCodeAt : Int -> Int }");
+	}
 }

+ 1 - 1
tests/server/test/templates/issues/Issue11904.hx → tests/server/test/templates/issues/Issue11904/Main.hx

@@ -1,7 +1,7 @@
 using StringTools;
 
 @:nullSafety
-class Issue11904 {
+class Main {
 	static function main() {}
 	static function extractReturnType(hint:String):Void {
 		for (i => code in hint) {}

+ 6 - 0
tests/server/test/templates/issues/Issue11904/Main1.hx

@@ -0,0 +1,6 @@
+@:nullSafety
+class Main {
+	static function main() {
+		MyStringTools.unsafeCodeAt("foo", 1);
+	}
+}

+ 5 - 0
tests/server/test/templates/issues/Issue11904/MyStringTools.hx

@@ -0,0 +1,5 @@
+class MyStringTools {
+	public static inline function unsafeCodeAt(s:String, index:Int):Int {
+		return (cast s).charCodeAt(index);
+	}
+}

+ 2 - 2
tests/unit/src/unit/TestNullCoalescing.hx

@@ -191,8 +191,8 @@ class TestNullCoalescing extends Test {
 		for (i => v in [1, 2, 3])
 			eq(arr[i], v);
 
-		var b:B = cast null;
-		var c:C = cast null;
+		@:nullSafety(Off) var b:B = null;
+		@:nullSafety(Off) var c:C = null;
 		var a = if (b != null) b else c;
 		var a = b ?? c;
 		eq("unit._TestNullCoalescing.A", HelperMacros.typeString(a));