Ver Fonte

support `macro : $tp<Param>` (closes #5162)

Simon Krajewski há 9 anos atrás
pai
commit
5411d122d0
2 ficheiros alterados com 42 adições e 3 exclusões
  1. 17 3
      src/syntax/parser.ml
  2. 25 0
      tests/unit/src/unit/issues/Issue5162.hx

+ 17 - 3
src/syntax/parser.ml

@@ -233,9 +233,23 @@ let reify in_macro =
 		mk_enum "TypeParam" n [v] p
 	and to_tpath (t,_) p =
 		let len = String.length t.tname in
-		if t.tpackage = [] && len > 1 && t.tname.[0] = '$' then
-			(EConst (Ident (String.sub t.tname 1 (len - 1))),p)
-		else begin
+		if t.tpackage = [] && len > 1 && t.tname.[0] = '$' then begin
+			let name = String.sub t.tname 1 (len - 1) in
+			let ei = (EConst (Ident name),p) in
+			match t.tparams with
+			| [] -> ei
+			| _ ->
+				(* `macro : $TP<Int>` conveys the intent to use TP and overwrite the
+				   type parameters. *)
+				let ea = to_array to_tparam t.tparams p in
+				let fields = [
+					("pack", (EField(ei,"pack"),p));
+					("name", (EField(ei,"name"),p));
+					("sub", (EField(ei,"sub"),p));
+					("params", ea);
+				] in
+				to_obj fields p
+		end else begin
 			let fields = [
 				("pack", to_array to_string t.tpackage p);
 				("name", to_string t.tname p);

+ 25 - 0
tests/unit/src/unit/issues/Issue5162.hx

@@ -0,0 +1,25 @@
+package unit.issues;
+
+import haxe.macro.Expr;
+
+class Issue5162 extends unit.Test {
+	function test() {
+		var tp = {
+			name: "Array",
+			pack: [],
+			sub: null
+		}
+		var ct = macro : $tp<Int>;
+		t(ct.match(TPath({params: [TPType(TPath({name: "Int"}))]})));
+
+		var tp = {
+			name: "Map",
+			pack: [],
+			sub: null
+		}
+		var param1 = macro : Int;
+		var param2 = macro : String;
+		var ct = macro : $tp<$param1, $param2>;
+		t(ct.match(TPath({params: [TPType(TPath({name: "Int"})), TPType(TPath({name: "String"}))]})));
+	}
+}