Skip to content

Commit

Permalink
[macro] respect imports on `@:build
Browse files Browse the repository at this point in the history
  • Loading branch information
Simn committed Nov 13, 2023
1 parent c6a8b20 commit 1d10416
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/context/resolution.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ open Globals
open Type

type resolution_kind =
| RTypeImport of string* module_type
| RTypeImport of string * module_type
| RClassFieldImport of string * tclass * tclass_field
| RAbstractFieldImport of string * tabstract * tclass * tclass_field
| REnumConstructorImport of string * tenum * tenum_field
Expand Down Expand Up @@ -107,8 +107,7 @@ class resolution_list (id : string list) = object(self)
l <- loop [] l;
end

method resolve (i : string) : resolution =
self#resolve_lazies;
method resolve' (i : string) : resolution =
let rec loop l = match l with
| [] ->
raise Not_found
Expand Down Expand Up @@ -148,6 +147,10 @@ class resolution_list (id : string list) = object(self)
in
loop l

method resolve (i : string) : resolution =
self#resolve_lazies;
self#resolve' i

method expand_enum_constructors (mt : module_type) = match mt with
| TAbstractDecl ({a_impl = Some c} as a) when a.a_enum ->
Some (class_statics_resolution c null_pos)
Expand Down
37 changes: 33 additions & 4 deletions src/typing/typeloadFields.ml
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,39 @@ let build_enum_abstract ctx c a fields p =
EVars [mk_evar ~t:(CTAnonymous fields,p) ("",null_pos)],p

let apply_macro ctx mode path el p =
let cpath, meth = (match List.rev (ExtString.String.nsplit path ".") with
| meth :: name :: pack -> (List.rev pack,name), meth
| _ -> raise_typing_error "Invalid macro path" p
) in
let cpath, meth = match List.rev (ExtString.String.nsplit path ".") with
| [fname;tname] ->
begin try
let res = ctx.m.import_resolution#resolve' tname in
begin match res.r_kind with
| RTypeImport(_,mt) ->
(t_path mt),fname
| _ ->
raise_typing_error "Invalid macro path" p
end;

with Not_found ->
([],tname),fname
end
| [fname;tname;mname] ->
begin try
let res = ctx.m.import_resolution#resolve' mname in
begin match res.r_kind with
| RTypeImport(_,mt) ->
let path = t_path mt in
(fst path @ [snd path],tname),fname
| _ ->
raise_typing_error "Invalid macro path" p
end;

with Not_found ->
([mname],tname),fname
end
| meth :: name :: pack ->
(List.rev pack,name), meth
| _ ->
raise_typing_error "Invalid macro path" p
in
ctx.g.do_macro ctx mode cpath meth el p

let build_module_def ctx mt meta fvars fbuild =
Expand Down
18 changes: 18 additions & 0 deletions tests/misc/projects/Issue11373/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pack.BuildMacro;
import pack.BuildMacro as BM;

@:build(BuildMacro.build())
class ClassImport {}

@:build(BuildMacro.BuildMacro.build())
class SubClassImport {}

@:build(BM.build())
class ClassImportAlias {}

@:build(BM.BuildMacro.build())
class SubClassImportAlias {}

function main() {
trace(BuildMacro.report());
}
2 changes: 2 additions & 0 deletions tests/misc/projects/Issue11373/compile.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--main Main
--interp
1 change: 1 addition & 0 deletions tests/misc/projects/Issue11373/compile.hxml.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Main.hx:17: ClassImport, ClassImportAlias, SubClassImport, SubClassImportAlias
21 changes: 21 additions & 0 deletions tests/misc/projects/Issue11373/pack/BuildMacro.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package pack;

import haxe.macro.Context;

class BuildMacro {
#if macro
static var builtTypes = [];
#end

static public function build():Array<haxe.macro.Expr.Field> {
#if macro
builtTypes.push("" + Context.getLocalClass());
#end
return null;
}

macro static public function report() {
builtTypes.sort(Reflect.compare);
return macro $v{builtTypes.join(", ")};
}
}

0 comments on commit 1d10416

Please sign in to comment.