Browse Source

consider type parameters for generic paths (closes #2576)

Simon Krajewski 11 năm trước cách đây
mục cha
commit
b6852d7277
2 tập tin đã thay đổi với 15 bổ sung12 xóa
  1. 11 8
      codegen.ml
  2. 4 4
      tests/unit/TestType.hx

+ 11 - 8
codegen.ml

@@ -226,16 +226,19 @@ let make_generic ctx ps pt p =
 	in
 	let name =
 		String.concat "_" (List.map2 (fun (s,_) t ->
-			let path = (match follow t with
-				| TInst (ct,_) -> ct.cl_path
-				| TEnum (e,_) -> e.e_path
-				| TAbstract (a,_) when Meta.has Meta.RuntimeValue a.a_meta -> a.a_path
+			let s_type_path_underscore (p,s) = match p with [] -> s | _ -> String.concat "_" p ^ "_" ^ s in
+			let rec loop top t = match follow t with
+				| TInst(c,tl) -> (s_type_path_underscore c.cl_path) ^ (loop_tl tl)
+				| TEnum(en,tl) -> (s_type_path_underscore en.e_path) ^ (loop_tl tl)
+				| TAbstract(a,tl) -> (s_type_path_underscore a.a_path) ^ (loop_tl tl)
+				| _ when not top -> "_" (* allow unknown/incompatible types as type parameters to retain old behavior *)
 				| TMono _ -> raise (Generic_Exception (("Could not determine type for parameter " ^ s), p))
 				| t -> raise (Generic_Exception (("Type parameter must be a class or enum instance (found " ^ (s_type (print_context()) t) ^ ")"), p))
-			) in
-			match path with
-			| [] , name -> name
-			| l , name -> String.concat "_" l ^ "_" ^ name
+			and loop_tl tl = match tl with
+				| [] -> ""
+				| tl -> "_" ^ String.concat "_" (List.map (loop false) tl)
+			in
+			loop true t
 		) ps pt)
 	in
 	{

+ 4 - 4
tests/unit/TestType.hx

@@ -572,26 +572,26 @@ class TestType extends Test {
 		hsf(TestType, "gf1_haxe_Template");
 		#end
 
-		hsf(TestType, #if (flash9 || cpp) "gf1_haxe_ds_GenericStack_Int" #else "gf1_haxe_ds_GenericStack" #end);
+		hsf(TestType, "gf1_haxe_ds_GenericStack_Int");
 		t(typeError(gf1(null))); // monos don't work
 		t(typeError(gf1( { foo:1 } ))); // structures don't work
 
 		eq("foo[1,2]", gf2("foo", [1, 2]));
 		eq("foo[[1,2]]", gf2("foo", [[1, 2]]));
 		hsf(TestType, "gf2_String_Int");
-		hsf(TestType, "gf2_String_Array");
+		hsf(TestType, "gf2_String_Array_Int");
 
 		var a = gf3("foo", ["bar", "baz"]);
 		eq(a[0], "bar");
 		eq(a[1], "baz");
 		eq(a[2], "foo");
-		hsf(TestType, "gf3_String_Array");
+		hsf(TestType, "gf3_String_Array_String");
 
 		#if !flash8
 		var t = new haxe.Template("foo");
 		var ta = gf3(t, [])[0];
 		f(t == ta);
-		hsf(TestType, "gf3_haxe_Template_Array");
+		hsf(TestType, "gf3_haxe_Template_Array__");
 		#end
 
 		eq(overloadFake(1), 1);