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] Add modules created with defineType/defineModule as dependency of current module #11720

Merged
merged 6 commits into from
Jul 19, 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
1 change: 1 addition & 0 deletions src/core/tPrinting.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions src/core/tType.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/typing/macroContext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
);
Expand Down
22 changes: 22 additions & 0 deletions tests/server/src/cases/issues/Issue11720.hx
Original file line number Diff line number Diff line change
@@ -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 changes: 23 additions & 0 deletions tests/server/test/templates/issues/Issue11720/Macro.hx
Original file line number Diff line number Diff line change
@@ -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 changes: 6 additions & 0 deletions tests/server/test/templates/issues/Issue11720/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Main {
static function main() {
var collectionType = Type.resolveClass("data.WeaponCollection");
if (collectionType != null) trace("data.WeaponCollection has been generated");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package col;

import col.Item;

class Collection<T:Item> {
public var items:Array<T>;
}
5 changes: 5 additions & 0 deletions tests/server/test/templates/issues/Issue11720/col/Item.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package col;
@:autoBuild(Macro.build())
abstract class Item {

}
7 changes: 7 additions & 0 deletions tests/server/test/templates/issues/Issue11720/data/Weapon.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package data;

typedef Image = {}

class Weapon extends col.Item {
public var picture:Image;
}
Loading