Skip to content

Commit

Permalink
use inferencebarrier instead of invokelatest for 1-arg @assert
Browse files Browse the repository at this point in the history
This version would be better as per this comment: <#55739 (review)>
I confirmed this still allows us to avoid invalidations reported at
#55583.
  • Loading branch information
aviatesk committed Sep 16, 2024
1 parent a993cd8 commit 2b4ef4b
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions base/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2b4ef4b

Please sign in to comment.