浏览代码

[macro] don't add modules as dependencies when using Compiler.include (#11701)

* [macro] don't add modules as dependencies when using Compiler.include

* [tests] add test for 11701

* [macro] use MDepFromMacroInclude as origin for Compiler.include()

* [tests] hxml file not needed anymore
Rudy Ges 1 年之前
父节点
当前提交
02c30cb2eb

+ 10 - 6
src/core/tFunctions.ml

@@ -303,12 +303,16 @@ let null_abstract = {
 	a_enum = false;
 }
 
-let add_dependency ?(skip_postprocess=false) m mdep origin =
-	if m != null_module && mdep != null_module && (m.m_path != mdep.m_path || m.m_extra.m_sign != mdep.m_extra.m_sign) then begin
-		m.m_extra.m_deps <- PMap.add mdep.m_id ({md_sign = mdep.m_extra.m_sign; md_path = mdep.m_path; md_kind = mdep.m_extra.m_kind; md_origin = origin}) m.m_extra.m_deps;
-		(* In case the module is cached, we'll have to run post-processing on it again (issue #10635) *)
-		if not skip_postprocess then m.m_extra.m_processed <- 0
-	end
+let add_dependency ?(skip_postprocess=false) m mdep = function
+	(* These module dependency origins should not add as a dependency *)
+	| MDepFromMacroInclude -> ()
+
+	| origin ->
+		if m != null_module && mdep != null_module && (m.m_path != mdep.m_path || m.m_extra.m_sign != mdep.m_extra.m_sign) then begin
+			m.m_extra.m_deps <- PMap.add mdep.m_id ({md_sign = mdep.m_extra.m_sign; md_path = mdep.m_path; md_kind = mdep.m_extra.m_kind; md_origin = origin}) m.m_extra.m_deps;
+			(* In case the module is cached, we'll have to run post-processing on it again (issue #10635) *)
+			if not skip_postprocess then m.m_extra.m_processed <- 0
+		end
 
 let arg_name (a,_) = a.v_name
 

+ 1 - 0
src/core/tPrinting.ml

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

+ 1 - 0
src/core/tType.ml

@@ -406,6 +406,7 @@ and module_dep_origin =
 	| MDepFromTyping
 	| MDepFromImport
 	| MDepFromMacro
+	| MDepFromMacroInclude
 
 and module_dep = {
 	md_sign : Digest.t;

+ 5 - 0
src/macro/macroApi.ml

@@ -26,6 +26,7 @@ type 'value compiler_api = {
 	init_macros_done : unit -> bool;
 	get_type : string -> Type.t option;
 	get_module : string -> Type.t list;
+	include_module : string -> unit;
 	after_init_macros : (unit -> unit) -> unit;
 	after_typing : (module_type list -> unit) -> unit;
 	on_generate : (Type.t list -> unit) -> bool -> unit;
@@ -1878,6 +1879,10 @@ let macro_api ccom get_api =
 		"get_module", vfun1 (fun s ->
 			encode_array (List.map encode_type ((get_api()).get_module (decode_string s)))
 		);
+		"include_module", vfun1 (fun s ->
+			(get_api()).include_module (decode_string s);
+			vnull
+		);
 		"on_after_init_macros", vfun1 (fun f ->
 			let f = prepare_callback f 1 in
 			(get_api()).after_init_macros (fun tctx -> ignore(f []));

+ 9 - 0
src/typing/macroContext.ml

@@ -127,6 +127,9 @@ let make_macro_com_api com mcom p =
 		get_module = (fun s ->
 			Interp.exc_string "unsupported"
 		);
+		include_module = (fun s ->
+			Interp.exc_string "unsupported"
+		);
 		after_init_macros = (fun f ->
 			com.callbacks#add_after_init_macros (fun () ->
 				let t = macro_timer com ["afterInitMacros"] in
@@ -404,6 +407,12 @@ let make_macro_api ctx mctx p =
 				m
 			)
 		);
+		MacroApi.include_module = (fun s ->
+			typing_timer ctx false (fun ctx ->
+				let path = parse_path s in
+				ignore(TypeloadModule.load_module ~origin:MDepFromMacroInclude ctx path p)
+			)
+		);
 		MacroApi.type_expr = (fun e ->
 			typing_timer ctx true (fun ctx -> type_expr ctx e WithType.value)
 		);

+ 1 - 1
std/haxe/macro/Compiler.hx

@@ -224,7 +224,7 @@ class Compiler {
 						var cl = prefix + file.substr(0, file.length - 3);
 						if (skip(cl))
 							continue;
-						Context.getModule(cl);
+						load("include_module", 1)(cl);
 					} else if (rec && sys.FileSystem.isDirectory(path + "/" + file) && !skip(prefix + file))
 						include(prefix + file, true, ignore, classPaths);
 				}

+ 23 - 0
tests/server/src/cases/issues/Issue11701.hx

@@ -0,0 +1,23 @@
+package cases.issues;
+
+import haxe.display.Diagnostic;
+
+class Issue11701 extends TestCase {
+	function test(_) {
+		vfs.putContent("Main.hx", getTemplate("issues/Issue11701/Main.hx"));
+		vfs.putContent("bar/Bar.hx", getTemplate("issues/Issue11701/Bar.hx"));
+		vfs.putContent("baz/Baz.hx", getTemplate("issues/Issue11701/Baz.hx"));
+
+		var content = getTemplate("issues/Issue11701/Foo.hx");
+		var transform = Markers.parse(content);
+		vfs.putContent("foo/Foo.hx", transform.source);
+
+		var args = ["-main", "Main"];
+		runHaxe(args);
+		assertSuccess();
+
+		runHaxeJson(args, ServerMethods.Invalidate, {file: new FsPath("foo/Foo.hx")});
+		runHaxeJsonCb(args, DisplayMethods.Hover, {file: new FsPath("foo/Foo.hx"), offset: transform.offset(1)}, _ -> {});
+		assertSuccess();
+	}
+}

+ 5 - 0
tests/server/test/templates/issues/Issue11701/Bar.hx

@@ -0,0 +1,5 @@
+package bar;
+
+class Bar {
+	var baz:baz.Baz;
+}

+ 5 - 0
tests/server/test/templates/issues/Issue11701/Baz.hx

@@ -0,0 +1,5 @@
+package baz;
+
+class Baz {
+	var main:Main;
+}

+ 8 - 0
tests/server/test/templates/issues/Issue11701/Foo.hx

@@ -0,0 +1,8 @@
+package foo;
+
+class Foo {
+	function foo() {
+		baz.Baz;
+		tr{-1-}ace("");
+	}
+}

+ 10 - 0
tests/server/test/templates/issues/Issue11701/Main.hx

@@ -0,0 +1,10 @@
+class Main {
+	var foo:foo.Foo;
+
+	public static function main() include();
+
+	static macro function include() {
+		haxe.macro.Compiler.include("bar");
+		return macro null;
+	}
+}