Forráskód Böngészése

[macro] Add modules created with defineType/defineModule as dependency of current module (#11720)

* [macro] Add modules created with defineType/defineModule as dependency

... of current module.
This makes sure those modules will be pulled from cache when restoring current
module.

* [tests] add test

* Use cache bound objects instead of dependencies

* Whitespace

* Revert "Whitespace"

This reverts commit 9665f2f4306bd2fd7757b126a4a9a5d995fbbde1.

* Revert "Use cache bound objects instead of dependencies"

This reverts commit c517036ca7838985f58789a8e3e01d3f3ccc8f35.
Rudy Ges 1 éve
szülő
commit
87b7dbe366

+ 1 - 0
src/core/tPrinting.ml

@@ -618,6 +618,7 @@ module Printer = struct
 		| MDepFromTyping -> "MDepFromTyping"
 		| MDepFromMacro -> "MDepFromMacro"
 		| MDepFromMacroInclude -> "MDepFromMacroInclude"
+		| MDepFromMacroDefine -> "MDepFromMacroDefine"
 
 	let s_module_tainting_reason = function
 		| CheckDisplayFile -> "check_display_file"

+ 3 - 0
src/core/tType.ml

@@ -406,7 +406,10 @@ and module_dep_origin =
 	| MDepFromTyping
 	| MDepFromImport
 	| MDepFromMacro
+	(* Compiler.include loads module with this special origin, which tells add_dependency not to add as a proper dependency. *)
 	| MDepFromMacroInclude
+	(* Modules created via Compiler.defineType or Compiler.defineModule will be added as dependency to their "parent" module with this origin. *)
+	| MDepFromMacroDefine
 
 and module_dep = {
 	md_sign : Digest.t;

+ 2 - 0
src/typing/macroContext.ml

@@ -480,6 +480,7 @@ let make_macro_api ctx mctx p =
 				let mnew = TypeloadModule.type_module ctx.com ctx.g ~dont_check_path:(has_native_meta) m (ctx.com.file_keys#generate_virtual ctx.com.compilation_step) [tdef,pos] pos in
 				mnew.m_extra.m_kind <- if is_macro then MMacro else MFake;
 				add_dependency mnew mdep MDepFromMacro;
+				add_dependency mdep mnew MDepFromMacroDefine;
 				ctx.com.module_nonexistent_lut#clear;
 			in
 			add false ctx;
@@ -510,6 +511,7 @@ let make_macro_api ctx mctx p =
 				let mnew = TypeloadModule.type_module ctx.com ctx.g mpath (ctx.com.file_keys#generate_virtual ctx.com.compilation_step) types pos in
 				mnew.m_extra.m_kind <- MFake;
 				add_dependency mnew ctx.m.curmod MDepFromMacro;
+				add_dependency ctx.m.curmod mnew MDepFromMacroDefine;
 				ctx.com.module_nonexistent_lut#clear;
 			end
 		);

+ 22 - 0
tests/server/src/cases/issues/Issue11720.hx

@@ -0,0 +1,22 @@
+package cases.issues;
+
+import haxe.display.Diagnostic;
+
+class Issue11720 extends TestCase {
+	function test(_) {
+		vfs.putContent("Main.hx", getTemplate("issues/Issue11720/Main.hx"));
+		vfs.putContent("Macro.hx", getTemplate("issues/Issue11720/Macro.hx"));
+		vfs.putContent("data/Weapon.hx", getTemplate("issues/Issue11720/data/Weapon.hx"));
+		vfs.putContent("col/Collection.hx", getTemplate("issues/Issue11720/col/Collection.hx"));
+		vfs.putContent("col/Item.hx", getTemplate("issues/Issue11720/col/Item.hx"));
+
+		var args = ["-main", "Main", "--macro", "include('data',true)", "--interp"];
+		runHaxe(args);
+		debugErrorMessages();
+		assertSuccess();
+
+		runHaxe(args);
+		assertSuccess();
+		assertHasPrint("Main.hx:4: data.WeaponCollection has been generated");
+	}
+}

+ 23 - 0
tests/server/test/templates/issues/Issue11720/Macro.hx

@@ -0,0 +1,23 @@
+import haxe.macro.Context;
+import haxe.macro.Expr;
+import haxe.macro.TypeTools;
+
+class Macro {
+	public static function build() {
+		var fields = Context.getBuildFields();
+		var cl = Context.getLocalClass().get();
+		var itemType = Context.getLocalType();
+		var itemComplexType = TypeTools.toComplexType(itemType);
+
+		var type:TypeDefinition = {
+			pos: Context.currentPos(),
+			name: cl.name + "Collection",
+			pack: cl.pack,
+			kind: TDClass({pack: ["col"], name: "Collection", params: [TPType(itemComplexType)]}),
+			fields: [],
+		}
+
+		Context.defineType(type);
+		return fields;
+	}
+}

+ 6 - 0
tests/server/test/templates/issues/Issue11720/Main.hx

@@ -0,0 +1,6 @@
+class Main {
+	static function main() {
+		var collectionType = Type.resolveClass("data.WeaponCollection");
+		if (collectionType != null) trace("data.WeaponCollection has been generated");
+	}
+}

+ 7 - 0
tests/server/test/templates/issues/Issue11720/col/Collection.hx

@@ -0,0 +1,7 @@
+package col;
+
+import col.Item;
+
+class Collection<T:Item> {
+	public var items:Array<T>;
+}

+ 5 - 0
tests/server/test/templates/issues/Issue11720/col/Item.hx

@@ -0,0 +1,5 @@
+package col;
+@:autoBuild(Macro.build())
+abstract class Item {
+    
+}

+ 7 - 0
tests/server/test/templates/issues/Issue11720/data/Weapon.hx

@@ -0,0 +1,7 @@
+package data;
+
+typedef Image = {}
+
+class Weapon extends col.Item {
+	public var picture:Image;
+}