Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/core/tFunctions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/core/tPrinting.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/core/tType.ml
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ and module_dep_origin =
| MDepFromTyping
| MDepFromImport
| MDepFromMacro
| MDepFromMacroInclude

and module_dep = {
md_sign : Digest.t;
Expand Down
5 changes: 5 additions & 0 deletions src/macro/macroApi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 []));
Expand Down
9 changes: 9 additions & 0 deletions src/typing/macroContext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
);
Expand Down
2 changes: 1 addition & 1 deletion std/haxe/macro/Compiler.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/server/src/cases/issues/Issue11701.hx
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 0 deletions tests/server/test/templates/issues/Issue11701/Bar.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package bar;

class Bar {
var baz:baz.Baz;
}
5 changes: 5 additions & 0 deletions tests/server/test/templates/issues/Issue11701/Baz.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package baz;

class Baz {
var main:Main;
}
8 changes: 8 additions & 0 deletions tests/server/test/templates/issues/Issue11701/Foo.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package foo;

class Foo {
function foo() {
baz.Baz;
tr{-1-}ace("");
}
}
10 changes: 10 additions & 0 deletions tests/server/test/templates/issues/Issue11701/Main.hx
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading