Skip to content

Commit

Permalink
disallow defining types into existing modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Simn committed Nov 26, 2024
1 parent d989ab3 commit 071b531
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 66 deletions.
24 changes: 13 additions & 11 deletions src/typing/macroContext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ->
Expand Down
2 changes: 0 additions & 2 deletions tests/misc/projects/Issue4160/compile.hxml

This file was deleted.

1 change: 0 additions & 1 deletion tests/misc/projects/Issue4160/compile.hxml.stderr

This file was deleted.

51 changes: 0 additions & 51 deletions tests/misc/projects/Issue4160/test/Main.hx

This file was deleted.

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(Context.getLocalModule(), [{
Context.defineModule("A", [{
pos: pos,
pack: [],
name: "A",
Expand Down

0 comments on commit 071b531

Please sign in to comment.