From 36fb174e9de913c661ca531e55dd79b72bf4688b Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Tue, 9 Apr 2024 12:05:58 +0200 Subject: [PATCH 1/5] [eval] Catch runtime exceptions in safe_decode --- src/macro/macroApi.ml | 1 - src/typing/macroContext.ml | 13 +++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/macro/macroApi.ml b/src/macro/macroApi.ml index 50bc9333d45..22686caa6bf 100644 --- a/src/macro/macroApi.ml +++ b/src/macro/macroApi.ml @@ -846,7 +846,6 @@ and decode_field v = } and decode_ctype t = - if t = vnull then raise Invalid_expr; let (i,args),p = decode_enum_with_pos t in (match i,args with | 0, [p] -> diff --git a/src/typing/macroContext.ml b/src/typing/macroContext.ml index 7f428adb155..08ed47474fd 100644 --- a/src/typing/macroContext.ml +++ b/src/typing/macroContext.ml @@ -41,15 +41,20 @@ module HxbWriterConfigWriterEval = HxbWriterConfig.WriterConfigWriter(EvalDataAp let macro_interp_cache = ref None let safe_decode com v expected t p f = - try - f () - with MacroApi.Invalid_expr -> + let raise_decode_error s = let path = [dump_path com;"decoding_error"] in let ch = Path.create_file false ".txt" [] path in let errors = Interp.handle_decoding_error (output_string ch) v t in List.iter (fun (s,i) -> Printf.fprintf ch "\nline %i: %s" i s) (List.rev errors); close_out ch; - raise_typing_error (Printf.sprintf "Expected %s but got %s (see %s.txt for details)" expected (Interp.value_string v) (String.concat "/" path)) p + raise_typing_error (Printf.sprintf "%s (see %s.txt for details)" s (String.concat "/" path)) p + in + + try f () with + | EvalContext.RunTimeException (VString emsg,_,_) -> + raise_decode_error (Printf.sprintf "Eval runtime exception: %s" emsg.sstring) + | MacroApi.Invalid_expr -> + raise_decode_error (Printf.sprintf "Expected %s but got %s" expected (Interp.value_string v)) let macro_timer com l = From c289c092905eae3b44a891e1778e7d8364b32d5b Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Tue, 9 Apr 2024 14:44:58 +0200 Subject: [PATCH 2/5] [macro] narrow build macro eval failures to individual field (hacky...) --- src/typing/macroContext.ml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/typing/macroContext.ml b/src/typing/macroContext.ml index 08ed47474fd..611afcc2992 100644 --- a/src/typing/macroContext.ml +++ b/src/typing/macroContext.ml @@ -965,7 +965,9 @@ let type_macro ctx mode cpath f (el:Ast.expr list) p = | None -> die "" __LOC__ | Some (_,_,fields) -> fields) else - List.map Interp.decode_field (Interp.decode_array v) + (* Hacky way to retrieve haxe.macro.Field type... *) + let t = match follow mret with | TInst(_, [ftype]) -> ftype | _ -> assert false in + List.map (fun f -> safe_decode ctx.com f "Field" t p (fun () -> Interp.decode_field f)) (Interp.decode_array v) in MSuccess (EVars [mk_evar ~t:(CTAnonymous fields,p) ("fields",null_pos)],p) ) From b0f1e5a0f47d9976d3fb47c6761b9f279ee41f42 Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Tue, 9 Apr 2024 14:45:36 +0200 Subject: [PATCH 3/5] [tests] add test for 11632 --- tests/misc/projects/Issue11632/Main.hx | 16 ++++++++++++++++ tests/misc/projects/Issue11632/Test.hx | 6 ++++++ tests/misc/projects/Issue11632/compile-fail.hxml | 1 + .../projects/Issue11632/compile-fail.hxml.stderr | 1 + tests/misc/projects/Issue11632/compile-test.hxml | 1 + .../projects/Issue11632/compile-test.hxml.stdout | 14 ++++++++++++++ 6 files changed, 39 insertions(+) create mode 100644 tests/misc/projects/Issue11632/Main.hx create mode 100644 tests/misc/projects/Issue11632/Test.hx create mode 100644 tests/misc/projects/Issue11632/compile-fail.hxml create mode 100644 tests/misc/projects/Issue11632/compile-fail.hxml.stderr create mode 100644 tests/misc/projects/Issue11632/compile-test.hxml create mode 100644 tests/misc/projects/Issue11632/compile-test.hxml.stdout diff --git a/tests/misc/projects/Issue11632/Main.hx b/tests/misc/projects/Issue11632/Main.hx new file mode 100644 index 00000000000..3931cdde9ae --- /dev/null +++ b/tests/misc/projects/Issue11632/Main.hx @@ -0,0 +1,16 @@ +#if macro +import haxe.macro.Expr; +import haxe.macro.Type; +#end + +#if !macro @:build(Macro.build()) #end +class Main {} + +class Macro { + public static function build() { + var ct = TPath({pack: [], name: "Null", params: [TPType(null)]}); + return (macro class { + var foo:$ct; + }).fields; + } +} diff --git a/tests/misc/projects/Issue11632/Test.hx b/tests/misc/projects/Issue11632/Test.hx new file mode 100644 index 00000000000..fcc7db404d9 --- /dev/null +++ b/tests/misc/projects/Issue11632/Test.hx @@ -0,0 +1,6 @@ +import sys.io.File; + +function main() { + var dump = File.getContent("dump/decoding_error.txt"); + Sys.println(dump); +} diff --git a/tests/misc/projects/Issue11632/compile-fail.hxml b/tests/misc/projects/Issue11632/compile-fail.hxml new file mode 100644 index 00000000000..20afe58fbc6 --- /dev/null +++ b/tests/misc/projects/Issue11632/compile-fail.hxml @@ -0,0 +1 @@ +--main Main diff --git a/tests/misc/projects/Issue11632/compile-fail.hxml.stderr b/tests/misc/projects/Issue11632/compile-fail.hxml.stderr new file mode 100644 index 00000000000..cf1e048201b --- /dev/null +++ b/tests/misc/projects/Issue11632/compile-fail.hxml.stderr @@ -0,0 +1 @@ +Main.hx:6: characters 12-19 : Eval runtime exception: Null Access (see dump/decoding_error.txt for details) diff --git a/tests/misc/projects/Issue11632/compile-test.hxml b/tests/misc/projects/Issue11632/compile-test.hxml new file mode 100644 index 00000000000..34c9a3273df --- /dev/null +++ b/tests/misc/projects/Issue11632/compile-test.hxml @@ -0,0 +1 @@ +--run Test diff --git a/tests/misc/projects/Issue11632/compile-test.hxml.stdout b/tests/misc/projects/Issue11632/compile-test.hxml.stdout new file mode 100644 index 00000000000..29b199f8d88 --- /dev/null +++ b/tests/misc/projects/Issue11632/compile-test.hxml.stdout @@ -0,0 +1,14 @@ +{ + access: null + doc: null + kind: FVar(TPath({ + name: Null + pack: [] + params: [TPType(null <- expected enum value)] + sub: null + }), null) + meta: null + name: foo + pos: #pos +} +line 7: expected enum value From aede1af498203b54c3241775a2796a3de053b2a0 Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Tue, 9 Apr 2024 15:00:02 +0200 Subject: [PATCH 4/5] Less hacky way to retrieve haxe.macro.Field type --- src/typing/macroContext.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/typing/macroContext.ml b/src/typing/macroContext.ml index 611afcc2992..84c66b004b2 100644 --- a/src/typing/macroContext.ml +++ b/src/typing/macroContext.ml @@ -965,8 +965,8 @@ let type_macro ctx mode cpath f (el:Ast.expr list) p = | None -> die "" __LOC__ | Some (_,_,fields) -> fields) else - (* Hacky way to retrieve haxe.macro.Field type... *) - let t = match follow mret with | TInst(_, [ftype]) -> ftype | _ -> assert false in + let ct = make_ptp_th (mk_type_path ~sub:"Field" (["haxe";"macro"], "Expr")) null_pos in + let t = Typeload.load_complex_type mctx false LoadNormal ct in List.map (fun f -> safe_decode ctx.com f "Field" t p (fun () -> Interp.decode_field f)) (Interp.decode_array v) in MSuccess (EVars [mk_evar ~t:(CTAnonymous fields,p) ("fields",null_pos)],p) From af4509fc6c57fb2922a4211ccd649ce07d783297 Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Tue, 9 Apr 2024 15:13:57 +0200 Subject: [PATCH 5/5] [tests] remove unneeded import --- tests/misc/projects/Issue11632/Main.hx | 1 - tests/misc/projects/Issue11632/compile-fail.hxml.stderr | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/misc/projects/Issue11632/Main.hx b/tests/misc/projects/Issue11632/Main.hx index 3931cdde9ae..c49eedc91f7 100644 --- a/tests/misc/projects/Issue11632/Main.hx +++ b/tests/misc/projects/Issue11632/Main.hx @@ -1,6 +1,5 @@ #if macro import haxe.macro.Expr; -import haxe.macro.Type; #end #if !macro @:build(Macro.build()) #end diff --git a/tests/misc/projects/Issue11632/compile-fail.hxml.stderr b/tests/misc/projects/Issue11632/compile-fail.hxml.stderr index cf1e048201b..b329610445d 100644 --- a/tests/misc/projects/Issue11632/compile-fail.hxml.stderr +++ b/tests/misc/projects/Issue11632/compile-fail.hxml.stderr @@ -1 +1 @@ -Main.hx:6: characters 12-19 : Eval runtime exception: Null Access (see dump/decoding_error.txt for details) +Main.hx:5: characters 12-19 : Eval runtime exception: Null Access (see dump/decoding_error.txt for details)