Skip to content

Commit

Permalink
Add invokelatest barrier to string(...) in @assert (#55739)
Browse files Browse the repository at this point in the history
This change protects `@assert` from invalidations to `Base.string(...)`
by adding an `invokelatest` barrier.

A common source of invalidations right now is `print(io,
join(args...))`. The problem is:
1. Inference concludes that `join(::Any...)` returns
`Union{String,AnnotatedString}`
2. The `print` call is union-split to `String` and `AnnotatedString`
3. This code is now invalidated when StyledStrings defines `print(io,
::AnnotatedString)`

The invalidation chain for `@assert` is similar: ` @Assert 1 == 1` calls
into `string(::Expr)` which calls into `print(io, join(args::Any...))`.
Unfortunately that leads to the invalidation of almost all `@assert`s
without an explicit error message

Similar to
#55583 (comment)
  • Loading branch information
topolarity authored and kshyatt committed Sep 12, 2024
1 parent 988eb28 commit 00fce67
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions base/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,12 @@ 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.string($(esc(msg))))
msg = :(Main.Base.invokelatest(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 = :(_assert_tostring($(Expr(:quote,msg))))
msg = :(Main.Base.invokelatest(_assert_tostring, $(Expr(:quote,msg))))
end
return :($(esc(ex)) ? $(nothing) : throw(AssertionError($msg)))
end
Expand Down

0 comments on commit 00fce67

Please sign in to comment.