From aed0a88336488abd5c31a106829acc3081ec1f61 Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Mon, 4 Mar 2024 12:14:02 -0500 Subject: [PATCH 1/2] quality improvement for recompile_invalidations The previous implementation was unnecessarily forcing compilation of the wrapper code for each use of the macro, and was assuming that hygiene rules do not apply to a gensym in a toplevel block and assuming that the internal tryfinally syntax is legal to generate directly. This change tries to avoid all of those assumptions, and should therefore also give better backtraces too. --- src/invalidations.jl | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/invalidations.jl b/src/invalidations.jl index 3a7d115..f3eff4b 100644 --- a/src/invalidations.jl +++ b/src/invalidations.jl @@ -8,17 +8,21 @@ Recompile any invalidations that occur within the given expression. This is gene by users in creating "Startup" packages to ensure that the code compiled by package authors is not invalidated. """ macro recompile_invalidations(expr) - list = gensym(:list) - Expr(:toplevel, - :($list = ccall(:jl_debug_method_invalidation, Any, (Cint,), 1)), - Expr(:tryfinally, - esc(expr), - :(ccall(:jl_debug_method_invalidation, Any, (Cint,), 0)) - ), - :(if ccall(:jl_generating_output, Cint, ()) == 1 - foreach($PrecompileTools.precompile_mi, $PrecompileTools.invalidation_leaves($list)) - end) - ) + # use QuoteNode instead of Expr(:quote) so that $ is not permitted as usual (instead of having this macro work like `@eval`) + return :(recompile_invalidations($__module__, $(QuoteNode(expr)))) +end + +function recompile_invalidations(__module__::Module, @nospecialize expr) + list = ccall(:jl_debug_method_invalidation, Any, (Cint,), 1) + try + eval(__module__, expr) + finally + ccall(:jl_debug_method_invalidation, Any, (Cint,), 0) + end + if ccall(:jl_generating_output, Cint, ()) == 1 + foreach(precompile_mi, invalidation_leaves(list)) + end + nothing end function invalidation_leaves(invlist) From 29941e70d0b0b6e063f54defcc4f32c2179d5b54 Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Mon, 4 Mar 2024 12:17:44 -0500 Subject: [PATCH 2/2] Update invalidations.jl --- src/invalidations.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/invalidations.jl b/src/invalidations.jl index f3eff4b..d73b421 100644 --- a/src/invalidations.jl +++ b/src/invalidations.jl @@ -8,14 +8,14 @@ Recompile any invalidations that occur within the given expression. This is gene by users in creating "Startup" packages to ensure that the code compiled by package authors is not invalidated. """ macro recompile_invalidations(expr) - # use QuoteNode instead of Expr(:quote) so that $ is not permitted as usual (instead of having this macro work like `@eval`) + # use QuoteNode instead of esc(Expr(:quote)) so that $ is not permitted as usual (instead of having this macro work like `@eval`) return :(recompile_invalidations($__module__, $(QuoteNode(expr)))) end function recompile_invalidations(__module__::Module, @nospecialize expr) list = ccall(:jl_debug_method_invalidation, Any, (Cint,), 1) try - eval(__module__, expr) + Core.eval(__module__, expr) finally ccall(:jl_debug_method_invalidation, Any, (Cint,), 0) end