diff --git a/src/typing/macroContext.ml b/src/typing/macroContext.ml index 5583d26e4b3..ded95a06318 100644 --- a/src/typing/macroContext.ml +++ b/src/typing/macroContext.ml @@ -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 @@ -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 -> diff --git a/tests/misc/projects/Issue4160/compile.hxml b/tests/misc/projects/Issue4160/compile.hxml new file mode 100644 index 00000000000..aae7872d734 --- /dev/null +++ b/tests/misc/projects/Issue4160/compile.hxml @@ -0,0 +1,2 @@ +--main test.Main +--interp \ No newline at end of file diff --git a/tests/misc/projects/Issue4160/compile.hxml.stderr b/tests/misc/projects/Issue4160/compile.hxml.stderr new file mode 100644 index 00000000000..19102815663 --- /dev/null +++ b/tests/misc/projects/Issue4160/compile.hxml.stderr @@ -0,0 +1 @@ +foo \ No newline at end of file diff --git a/tests/misc/projects/Issue4160/test/Main.hx b/tests/misc/projects/Issue4160/test/Main.hx new file mode 100644 index 00000000000..af6c0542e0b --- /dev/null +++ b/tests/misc/projects/Issue4160/test/Main.hx @@ -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; + } +} \ No newline at end of file diff --git a/tests/misc/projects/Issue6006/Macro.hx b/tests/misc/projects/Issue6006/Macro.hx index c9f6745a189..4429e6f839f 100644 --- a/tests/misc/projects/Issue6006/Macro.hx +++ b/tests/misc/projects/Issue6006/Macro.hx @@ -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",