Pārlūkot izejas kodu

[java] don't run string switch on null (closes #4481)

Aleksandr Kuzmenko 6 gadi atpakaļ
vecāks
revīzija
f9d22171c1
2 mainītis faili ar 15 papildinājumiem un 2 dzēšanām
  1. 11 2
      src/generators/genjava.ml
  2. 4 0
      tests/unit/src/unit/issues/Issue4481.hx

+ 11 - 2
src/generators/genjava.ml

@@ -458,7 +458,7 @@ struct
 
 		let hash_cache = ref None in
 
-		let call_hashcode = { local with
+		(* let call_hashcode = { local with
 			eexpr = TCall({ local with
 				eexpr = TField(local, FDynamic "hashCode");
 				etype = TFun([], basic.tint);
@@ -472,6 +472,13 @@ struct
 				mk (TConst (TInt (Int32.of_int 0))) basic.tint local.epos,
 				Some call_hashcode
 			)
+		} in *)
+		let local_hashcode = ref { local with
+			eexpr = TCall({ local with
+				eexpr = TField(local, FDynamic "hashCode");
+				etype = TFun([], basic.tint);
+			}, []);
+			etype = basic.tint
 		} in
 
 		let get_hash_cache () =
@@ -587,7 +594,9 @@ struct
 			(el, e)
 		in
 
-		let switch = { eswitch with
+		let is_not_null_check = mk (TBinop (OpNotEq, local, { local with eexpr = TConst TNull })) basic.tbool local.epos in
+		let if_not_null e = { e with eexpr = TIf (is_not_null_check, e, None) } in
+		let switch = if_not_null { eswitch with
 			eexpr = TSwitch(!local_hashcode, List.map change_case (reorder_cases ecases []), None);
 		} in
 		(if !has_case then begin

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

@@ -13,5 +13,9 @@ class Issue4481 extends unit.Test {
 			case 'a': 0;
 			case _: 1;
 		});
+
+		switch s {
+			case '': assert();
+		}
 	}
 }