From 6c3b0093240296c2a00ca4497358eef2f9ba66ed Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Mon, 16 Sep 2024 23:34:29 +0900 Subject: [PATCH] use `inferencebarrier` instead of `invokelatest` for 1-arg `@assert` This version would be better as per this comment: I confirmed this still allows us to avoid invalidations reported at JuliaLang/julia#55583. --- base/error.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/base/error.jl b/base/error.jl index ee533cee0b57d..c49ede624607d 100644 --- a/base/error.jl +++ b/base/error.jl @@ -232,12 +232,14 @@ macro assert(ex, msgs...) msg = msg # pass-through elseif !isempty(msgs) && (isa(msg, Expr) || isa(msg, Symbol)) # message is an expression needing evaluating - msg = :(Main.Base.invokelatest(Main.Base.string, $(esc(msg)))) + # N.B. To reduce the risk of invalidation caused by the complex callstack involved + # with `string`, use `inferencebarrier` here to hide this `string` from the compiler. + msg = :(Main.Base.inferencebarrier(Main.Base.string)($(esc(msg)))) elseif isdefined(Main, :Base) && isdefined(Main.Base, :string) && applicable(Main.Base.string, msg) msg = Main.Base.string(msg) else # string() might not be defined during bootstrap - msg = :(Main.Base.invokelatest(_assert_tostring, $(Expr(:quote,msg)))) + msg = :(Main.Base.inferencebarrier(_assert_tostring)($(Expr(:quote,msg)))) end return :($(esc(ex)) ? $(nothing) : throw(AssertionError($msg))) end