Selaa lähdekoodia

[diagnostics] report null safety errors (#11729)

* [diagnostics] report null safety errors

* [tests] update test for 7931
Rudy Ges 1 vuosi sitten
vanhempi
commit
c4cc470360

+ 1 - 1
src/typing/nullSafety.ml

@@ -1687,7 +1687,7 @@ let run (com:Common.context) (types:module_type list) =
 	timer();
 	match com.callbacks#get_null_safety_report with
 		| [] ->
-			List.iter (fun err -> com.error err.sm_msg err.sm_pos) (List.rev report.sr_errors)
+			List.iter (fun err -> Common.display_error com err.sm_msg err.sm_pos) (List.rev report.sr_errors)
 		| callbacks ->
 			let errors =
 				List.map (fun err -> (err.sm_msg, err.sm_pos)) report.sr_errors

+ 18 - 0
tests/server/src/cases/issues/Issue7931.hx

@@ -10,8 +10,26 @@ class Issue7931 extends TestCase {
 		runHaxe(args);
 		assertErrorMessage("Local variable s used without being initialized");
 		runHaxeJsonCb(args, DisplayMethods.Diagnostics, {file: new FsPath("Main.hx")}, res -> {
+			Assert.equals(1, res.length);
+			Assert.equals(1, res[0].diagnostics.length);
 			Assert.equals("Local variable s used without being initialized", res[0].diagnostics[0].args);
 			Assert.same(transform.range(1,2), res[0].diagnostics[0].range);
 		});
 	}
+
+	function testNullSafety(_) {
+		var content = getTemplate("issues/Issue7931/Main1.hx");
+		var transform = Markers.parse(content);
+
+		vfs.putContent("Main.hx", transform.source);
+		var args = ["-main", "Main", "-js", "out.js", "--no-output"];
+		runHaxe(args);
+		assertErrorMessage("Null safety: Cannot assign nullable value here.");
+		runHaxeJsonCb(args, DisplayMethods.Diagnostics, {file: new FsPath("Main.hx")}, res -> {
+			Assert.equals(1, res.length);
+			Assert.equals(1, res[0].diagnostics.length);
+			Assert.equals("Null safety: Cannot assign nullable value here.", res[0].diagnostics[0].args);
+			Assert.same(transform.range(1,2), res[0].diagnostics[0].range);
+		});
+	}
 }

+ 7 - 0
tests/server/test/templates/issues/Issue7931/Main1.hx

@@ -0,0 +1,7 @@
+@:nullSafety(StrictThreaded)
+class Main {
+	static function main() {
+		var a = 1;
+		{-1-}a = null{-2-};
+	}
+}