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)

(cherry picked from commit 945517b)
  • Loading branch information
topolarity authored and KristofferC committed Sep 24, 2024
1 parent 47e8cab commit c86965b
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 @@ -223,14 +223,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.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 = quote
msg = $(Expr(:quote,msg))
isdefined(Main, :Base) ? Main.Base.string(msg) :
isdefined(Main, :Base) ? Main.Base.invokelatest(Main.Base.string, msg) :
(Core.println(msg); "Error during bootstrap. See stdout.")
end
end
Expand Down

0 comments on commit c86965b

Please sign in to comment.