From 071b53136ad2c447b8c4f2350185c4fc2c70b274 Mon Sep 17 00:00:00 2001 From: Simon Krajewski Date: Tue, 26 Nov 2024 13:28:36 +0100 Subject: [PATCH] disallow defining types into existing modules --- src/typing/macroContext.ml | 24 +++++---- tests/misc/projects/Issue4160/compile.hxml | 2 - .../projects/Issue4160/compile.hxml.stderr | 1 - tests/misc/projects/Issue4160/test/Main.hx | 51 ------------------- tests/misc/projects/Issue6006/Macro.hx | 2 +- 5 files changed, 14 insertions(+), 66 deletions(-) delete mode 100644 tests/misc/projects/Issue4160/compile.hxml delete mode 100644 tests/misc/projects/Issue4160/compile.hxml.stderr delete mode 100644 tests/misc/projects/Issue4160/test/Main.hx diff --git a/src/typing/macroContext.ml b/src/typing/macroContext.ml index 7b0fde048c0..5583d26e4b3 100644 --- a/src/typing/macroContext.ml +++ b/src/typing/macroContext.ml @@ -492,6 +492,14 @@ 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 @@ -503,17 +511,11 @@ 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 mpath = Ast.parse_path m in - begin try - let m = ctx.com.module_lut#find mpath in - 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 + 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; ); 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 deleted file mode 100644 index aae7872d734..00000000000 --- a/tests/misc/projects/Issue4160/compile.hxml +++ /dev/null @@ -1,2 +0,0 @@ ---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 deleted file mode 100644 index 19102815663..00000000000 --- a/tests/misc/projects/Issue4160/compile.hxml.stderr +++ /dev/null @@ -1 +0,0 @@ -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 deleted file mode 100644 index af6c0542e0b..00000000000 --- a/tests/misc/projects/Issue4160/test/Main.hx +++ /dev/null @@ -1,51 +0,0 @@ -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 4429e6f839f..c9f6745a189 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(Context.getLocalModule(), [{ + Context.defineModule("A", [{ pos: pos, pack: [], name: "A",