Browse Source

[java/cs] Do not warn on unsafe casts that are actually compatible (because of type parameter constraints). Closes #3788

Cauê Waneck 10 years ago
parent
commit
72bb6b374a
2 changed files with 52 additions and 18 deletions
  1. 28 18
      gencommon.ml
  2. 24 0
      tests/unit/src/unit/issues/Issue3788.hx

+ 28 - 18
gencommon.ml

@@ -5961,6 +5961,12 @@ struct
 						true)
 						true)
 			| _ -> true
 			| _ -> true
 
 
+	let unifies tfrom tto = try
+		unify tfrom tto;
+		true
+	with | _ ->
+		false
+
 	let do_unsafe_cast gen from_t to_t e	=
 	let do_unsafe_cast gen from_t to_t e	=
 		let t_path t =
 		let t_path t =
 			match t with
 			match t with
@@ -5971,25 +5977,29 @@ struct
 				| TDynamic _ -> ([], "Dynamic")
 				| TDynamic _ -> ([], "Dynamic")
 				| _ -> raise Not_found
 				| _ -> raise Not_found
 		in
 		in
-		let do_default () =
-			gen.gon_unsafe_cast to_t e.etype e.epos;
+		match gen.gfollow#run_f from_t, gen.gfollow#run_f to_t with
+		| TInst({ cl_kind = KTypeParameter tl },_), t2 when List.exists (fun t -> unifies t t2) tl ->
 			mk_cast to_t (mk_cast t_dynamic e)
 			mk_cast to_t (mk_cast t_dynamic e)
-		in
-		(* TODO: there really should be a better way to write that *)
-		try
-			if (Hashtbl.find gen.gsupported_conversions (t_path from_t)) from_t to_t then
-				mk_cast to_t e
-			else
-				do_default()
-		with
-			| Not_found ->
-				try
-					if (Hashtbl.find gen.gsupported_conversions (t_path to_t)) from_t to_t then
-						mk_cast to_t e
-					else
-						do_default()
-				with
-					| Not_found -> do_default()
+		| _ ->
+			let do_default () =
+				gen.gon_unsafe_cast to_t e.etype e.epos;
+				mk_cast to_t (mk_cast t_dynamic e)
+			in
+			(* TODO: there really should be a better way to write that *)
+			try
+				if (Hashtbl.find gen.gsupported_conversions (t_path from_t)) from_t to_t then
+					mk_cast to_t e
+				else
+					do_default()
+			with
+				| Not_found ->
+					try
+						if (Hashtbl.find gen.gsupported_conversions (t_path to_t)) from_t to_t then
+							mk_cast to_t e
+						else
+							do_default()
+					with
+						| Not_found -> do_default()
 
 
 	(* ****************************** *)
 	(* ****************************** *)
 	(* cast handler *)
 	(* cast handler *)

+ 24 - 0
tests/unit/src/unit/issues/Issue3788.hx

@@ -0,0 +1,24 @@
+package unit.issues;
+
+class Issue3788 extends Test
+{
+	public function test()
+	{
+		var s =new Something();
+		eq(s.f(), null); // if no warning is on haxe output, it passed
+	}
+}
+
+private class Something<T:C>
+{
+	public function new()
+	{
+	}
+
+	public function f() {
+		var v:C = (null : T);
+		return v;
+	}
+}
+
+private class C {}