Skip to content

Commit

Permalink
Include module path in virtual file names (#11852)
Browse files Browse the repository at this point in the history
* Include module path in virtual file names

* m -> mpath

* Use directory structure instead of dotpath

* [tests] Add test

* Handle empty package
  • Loading branch information
kLabz authored Jan 21, 2025
1 parent 43cddee commit d5dd8f5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/context/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,10 @@ class file_keys = object(self)

val virtual_counter = ref 0

method generate_virtual step =
method generate_virtual mpath step =
incr virtual_counter;
Printf.sprintf "file_%i_%i" step !virtual_counter
let base = match fst mpath with | [] -> "." | pack -> ExtLib.String.join "/" pack in
Printf.sprintf "%s/%s_%i_%i" base (snd mpath) step !virtual_counter

end

Expand Down
6 changes: 3 additions & 3 deletions src/typing/macroContext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ let make_macro_api ctx mctx p =
let mctx = (match ctx.g.macros with None -> die "" __LOC__ | Some (_,mctx) -> mctx) in
let ttype = Typeload.load_instance mctx (make_ptp cttype p) ParamNormal LoadNormal in
let f () = Interp.decode_type_def v in
let m, tdef, pos = safe_decode ctx.com v "TypeDefinition" ttype p f in
let mpath, tdef, pos = safe_decode ctx.com v "TypeDefinition" ttype p f in
let has_native_meta = match tdef with
| EClass d -> Meta.has Meta.Native d.d_meta
| EEnum d -> Meta.has Meta.Native d.d_meta
Expand All @@ -465,7 +465,7 @@ let make_macro_api ctx mctx p =
in
let add is_macro ctx =
let mdep = Option.map_default (fun s -> TypeloadModule.load_module ~origin:MDepFromMacro ctx (parse_path s) pos) ctx.m.curmod mdep in
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
let mnew = TypeloadModule.type_module ctx.com ctx.g ~dont_check_path:(has_native_meta) mpath (ctx.com.file_keys#generate_virtual mpath 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;
Expand Down Expand Up @@ -502,7 +502,7 @@ let make_macro_api ctx mctx p =
end else
ignore(TypeloadModule.type_types_into_module ctx.com ctx.g m types pos)
with Not_found ->
let mnew = TypeloadModule.type_module ctx.com ctx.g mpath (ctx.com.file_keys#generate_virtual ctx.com.compilation_step) types pos in
let mnew = TypeloadModule.type_module ctx.com ctx.g mpath (ctx.com.file_keys#generate_virtual mpath 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;
Expand Down
51 changes: 51 additions & 0 deletions tests/misc/projects/Issue11852/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import sys.io.File;

class Main {
#if macro
public static function init() {
var pos = haxe.macro.Context.currentPos();

haxe.macro.Context.onAfterInitMacros(() -> {
haxe.macro.Context.defineType({
pack: [],
name: "TopLevelType",
pos: pos,
kind: TDStructure,
fields: []
});

haxe.macro.Context.defineType({
pack: ["foo", "bar"],
name: "DefinedType",
pos: pos,
kind: TDStructure,
fields: []
});
});
}
#else
public static function main() {
// Add generated modules as dependencies for Main
var _:TopLevelType = {};
var _:foo.bar.DefinedType = {};

var lines = File.getContent("dump/eval/dependencies.dump").split("\n");
lines = lines.map(l -> StringTools.replace(l, "\\", "/"));
inline function check(module:String) {
var line = Lambda.filter(lines, l -> StringTools.endsWith(l, module)).shift();

if (line == null)
throw 'Cannot find $module in dependency dump';

if (!StringTools.endsWith(line, 'tests/misc/projects/Issue11852/$module')) {
trace(module, line);
throw 'Incorrect path generated for $module';
}
}

// Check generated path for macro generated modules
check("TopLevelType_1_1");
check("foo/bar/DefinedType_1_2");
}
#end
}
4 changes: 4 additions & 0 deletions tests/misc/projects/Issue11852/compile.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--macro Main.init()
-main Main
-D dump-dependencies
--interp

0 comments on commit d5dd8f5

Please sign in to comment.