Răsfoiți Sursa

[java] fix switching on a null string (fixes #4481)

Aleksandr Kuzmenko 6 ani în urmă
părinte
comite
447a0dfb8e

+ 10 - 2
src/generators/genjava.ml

@@ -416,7 +416,7 @@ struct
 		and modify it to:
 		{
 			var execute_def = true;
-			switch(str.hashCode())
+			switch(str == null ? 0 : str.hashCode())
 			{
 				case (hashcode of a):
 					if (str == "a")
@@ -458,12 +458,20 @@ struct
 
 		let hash_cache = ref None in
 
-		let local_hashcode = ref { local with
+		let call_hashcode = { local with
 			eexpr = TCall({ local with
 				eexpr = TField(local, FDynamic "hashCode");
 				etype = TFun([], basic.tint);
 			}, []);
 			etype = basic.tint
+		}
+		and is_null_check = mk (TBinop (OpEq, local, { local with eexpr = TConst TNull })) basic.tbool local.epos in
+		let local_hashcode = ref { call_hashcode with
+			eexpr = TIf (
+				is_null_check,
+				mk (TConst (TInt (Int32.of_int 0))) basic.tint local.epos,
+				Some call_hashcode
+			)
 		} in
 
 		let get_hash_cache () =

+ 3 - 3
tests/runci/targets/Java.hx

@@ -24,6 +24,9 @@ class Java {
 		runCommand("haxe", ["compile-java.hxml","-dce","no"].concat(args));
 		runCommand("java", ["-jar", "bin/java/TestMain-Debug.jar"]);
 
+		changeDirectory(miscJavaDir);
+		runCommand("haxe", ["run.hxml"]);
+
 		changeDirectory(sysDir);
 		runCommand("haxe", ["compile-java.hxml"]);
 		runCommand("java", ["-jar", "bin/java/Main-Debug.jar"]);
@@ -50,8 +53,5 @@ class Java {
 				}
 			}
 		}
-
-		changeDirectory(miscJavaDir);
-		runCommand("haxe", ["run.hxml"]);
 	}
 }

+ 12 - 0
tests/unit/src/unit/issues/Issue4481.hx

@@ -0,0 +1,12 @@
+package unit.issues;
+
+class Issue4481 extends unit.Test {
+	static var s:String = null;
+
+	function test() {
+		switch s {
+			case 'a': assert();
+			case _: eq(null, s);
+		}
+	}
+}