Selaa lähdekoodia

Allow structs as generic type params (#6969)

Ben Morris 7 vuotta sitten
vanhempi
commit
48fe56592b
3 muutettua tiedostoa jossa 15 lisäystä ja 2 poistoa
  1. 1 0
      src/typing/typeload.ml
  2. 12 1
      tests/unit/src/unit/TestGeneric.hx
  3. 2 1
      tests/unit/src/unit/TestType.hx

+ 1 - 0
src/typing/typeload.ml

@@ -3789,6 +3789,7 @@ let make_generic ctx ps pt p =
 			let rec loop top t = match follow t with
 				| TInst(c,tl) -> (ident_safe (s_type_path_underscore c.cl_path)) ^ (loop_tl tl)
 				| TEnum(en,tl) -> (s_type_path_underscore en.e_path) ^ (loop_tl tl)
+				| TAnon(a) -> "anon_" ^ String.concat "_" (PMap.foldi (fun s f acc -> (s ^ "_" ^ (loop false (follow f.cf_type))) :: acc) a.a_fields [])
 				| 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))

+ 12 - 1
tests/unit/src/unit/TestGeneric.hx

@@ -1,5 +1,10 @@
 package unit;
 
+private typedef MyAnon = {
+	a:Int,
+	?b:MyRandomClass,
+}
+
 @:generic
 class MyGeneric<T> {
 	public var t:T;
@@ -51,4 +56,10 @@ class TestGeneric extends Test {
 		n.root.rbLeft = new MyData(2);
 		n.root.rbRight = new MyData(3);
 	}
-}
+
+	function testGenericAnon() {
+		var a = new MyGeneric<MyAnon>({a: 1});
+		eq(a.t.a, 1);
+		eq(a.t.b, null);
+	}
+}

+ 2 - 1
tests/unit/src/unit/TestType.hx

@@ -537,6 +537,7 @@ class TestType extends Test {
 		gf1(2);
 		gf1("foo");
 		gf1(true);
+		gf1({foo: 1});
 
 		gf1(new haxe.Template("foo"));
 
@@ -548,8 +549,8 @@ class TestType extends Test {
 		hsf(TestType, "gf1_haxe_Template");
 
 		hsf(TestType, "gf1_haxe_ds_GenericStack_Int");
+		hsf(TestType, "gf1_anon_foo_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]]));