Sfoglia il codice sorgente

resolve `@:generic` classes upon construction (closes #2016)

Simon Krajewski 10 anni fa
parent
commit
7df778cffb
2 ha cambiato i file con 24 aggiunte e 2 eliminazioni
  1. 18 0
      tests/unit/src/unit/issues/Issue2016.hx
  2. 6 2
      typer.ml

+ 18 - 0
tests/unit/src/unit/issues/Issue2016.hx

@@ -0,0 +1,18 @@
+package unit.issues;
+
+@:generic
+private class Gen<T> {
+	public function new(a:T) { }
+}
+
+private typedef TGen<T> = Gen<T>;
+
+class Issue2016 extends Test {
+	function test() {
+		var t = new TGen("a");
+		eq("unit.issues._Issue2016.Gen_String", Type.getClassName(Type.getClass(t)));
+
+		var t = new TGen(1);
+		eq("unit.issues._Issue2016.Gen_Int", Type.getClassName(Type.getClass(t)));
+	}
+}

+ 6 - 2
typer.ml

@@ -3309,10 +3309,14 @@ and type_expr ctx (e,p) (with_type:with_type) =
 			ctx.constructor_argument_stack <- el :: ctx.constructor_argument_stack;
 			let t = follow (Typeload.load_instance ctx t p true) in
 			ctx.constructor_argument_stack <- List.tl ctx.constructor_argument_stack;
-			t
+			(* Try to properly build @:generic classes here (issue #2016) *)
+			begin match t with
+				| TInst({cl_kind = KGeneric } as c,tl) -> follow (Codegen.build_generic ctx c p tl)
+				| _ -> t
+			end
 		with Codegen.Generic_Exception _ ->
 			(* Try to infer generic parameters from the argument list (issue #2044) *)
-			match Typeload.load_type_def ctx p t with
+			match Typeload.resolve_typedef (Typeload.load_type_def ctx p t) with
 			| TClassDecl ({cl_constructor = Some cf} as c) ->
 				let monos = List.map (fun _ -> mk_mono()) c.cl_params in
 				let ct, f = get_constructor ctx c monos p in