Browse Source

add Context.makeMonomorph (#10728)

* add Context.makeMonomorph

* add test
Dan Korostelev 3 years ago
parent
commit
b0d9152111
3 changed files with 35 additions and 0 deletions
  1. 4 0
      src/macro/macroApi.ml
  2. 10 0
      std/haxe/macro/Context.hx
  3. 21 0
      tests/unit/src/unit/issues/Issue10728.hx

+ 4 - 0
src/macro/macroApi.ml

@@ -1873,6 +1873,10 @@ let macro_api ccom get_api =
 			(get_api()).define_type v (opt decode_string m);
 			vnull
 		);
+		"make_monomorph", vfun0 (fun() ->
+			let t = TMono (Monomorph.create ()) in
+			encode_type t
+		);
 		"define_module", vfun4 (fun path vl ui ul ->
 			(get_api()).define_module (decode_string path) (decode_array vl) (List.map decode_import (decode_array ui)) (List.map fst (List.map decode_path (decode_array ul)));
 			vnull

+ 10 - 0
std/haxe/macro/Context.hx

@@ -530,6 +530,16 @@ class Context {
 		load("define_type", 2)(t, moduleDependency);
 	}
 
+	/**
+		Creates and returns a new instance of monomorph (`TMono`) type.
+
+		Returned monomorph can be used with e.g. `Context.unify` to make the compiler
+		bind the monomorph to an actual type and let macro further process the resulting type.
+	**/
+	public static function makeMonomorph():Type {
+		return load("make_monomorph", 0)();
+	}
+
 	/**
 		Defines a new module as `modulePath` with several `TypeDefinition`
 		`types`. This is analogous to defining a .hx file.

+ 21 - 0
tests/unit/src/unit/issues/Issue10728.hx

@@ -0,0 +1,21 @@
+package unit.issues;
+
+class Issue10728 extends Test {
+	function test(){
+		eq("String", m());
+	}
+
+	static macro function m() {
+		var mono = haxe.macro.Context.makeMonomorph();
+		var td = switch (haxe.macro.Context.getType("B")) {
+			case TType(td, _): td;
+			case _: throw "assert";
+		};
+		haxe.macro.Context.unify(haxe.macro.Context.getType("A"), TType(td, [mono]));
+		return macro $v{haxe.macro.TypeTools.toString(mono)};
+	}
+}
+
+private abstract A(Int) to B<String> {}
+
+private typedef B<T> = Int;