Skip to content

Commit

Permalink
only disallow for foreign modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Simn committed Nov 27, 2024
1 parent 071b531 commit acfb656
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 14 deletions.
30 changes: 17 additions & 13 deletions src/typing/macroContext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -492,14 +492,6 @@ let make_macro_api ctx mctx p =
()
);
MacroApi.define_module = (fun m types imports usings ->
let mpath = Ast.parse_path m in
if ctx.com.module_lut#mem mpath then begin
let m = ctx.com.module_lut#find mpath in
let pos = { pfile = (Path.UniqueKey.lazy_path m.m_extra.m_file); pmin = 0; pmax = 0 } in
raise_typing_error_ext (make_error ~sub:[
make_error ~depth:1 (Custom "Previously defined here") pos
] (Custom (Printf.sprintf "Cannot redefine module %s" (s_type_path mpath))) p);
end;
let types = List.map (fun v ->
let _, tdef, pos = (try Interp.decode_type_def v with MacroApi.Invalid_expr -> Interp.exc_string "Invalid type definition") in
tdef, pos
Expand All @@ -511,11 +503,23 @@ let make_macro_api ctx mctx p =
EUsing (List.map (fun s -> s,null_pos) sl),pos
) usings in
let types = imports @ usings @ types in
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;
let mpath = Ast.parse_path m in
begin try
let m = ctx.com.module_lut#find mpath in
if m != ctx.m.curmod then begin
let pos = { pfile = (Path.UniqueKey.lazy_path m.m_extra.m_file); pmin = 0; pmax = 0 } in
raise_typing_error_ext (make_error ~sub:[
make_error ~depth:1 (Custom "Previously defined here") pos
] (Custom (Printf.sprintf "Cannot redefine module %s" (s_type_path mpath))) 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
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
);
MacroApi.module_dependency = (fun mpath file ->
let m = typing_timer ctx false (fun ctx ->
Expand Down
2 changes: 2 additions & 0 deletions tests/misc/projects/Issue4160/compile.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--main test.Main
--interp
1 change: 1 addition & 0 deletions tests/misc/projects/Issue4160/compile.hxml.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo
51 changes: 51 additions & 0 deletions tests/misc/projects/Issue4160/test/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package test;

import haxe.macro.Context;
import haxe.macro.Expr;

private class Bar {
public function new() { }
public function getValue() {
return "foo";
}
}

class Main
{
static function main()
{
defineFooExtendsBarInLocalModule();
#if !macro
var foo = new Foo();
Sys.stderr().writeString(foo.getValue());
#end
}

macro static function defineFooExtendsBarInLocalModule(?e)
{
var infos = Context.getPosInfos(Context.currentPos());
var position = Context.makePosition({min:0, max:0, file:infos.file});

var superTypePath:TypePath =
{
pack: [],
name: "Bar",
sub: null
}

var kind:TypeDefKind = TypeDefKind.TDClass(superTypePath);

var Foo:TypeDefinition =
{
name: "Foo",
pack: ["test"],
pos: position,
kind: kind,
fields: []
}

Context.defineModule(Context.getLocalModule(), [Foo]);

return e;
}
}
2 changes: 1 addition & 1 deletion tests/misc/projects/Issue6006/Macro.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import haxe.macro.Expr;
class Macro {
static function build() {
var pos = Context.currentPos();
Context.defineModule("A", [{
Context.defineModule(Context.getLocalModule(), [{
pos: pos,
pack: [],
name: "A",
Expand Down

0 comments on commit acfb656

Please sign in to comment.