Skip to content

Commit 945517b

Browse files
authored
Add invokelatest barrier to string(...) in @assert (#55739)
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)
1 parent 22eded8 commit 945517b

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

base/error.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,12 @@ macro assert(ex, msgs...)
232232
msg = msg # pass-through
233233
elseif !isempty(msgs) && (isa(msg, Expr) || isa(msg, Symbol))
234234
# message is an expression needing evaluating
235-
msg = :(Main.Base.string($(esc(msg))))
235+
msg = :(Main.Base.invokelatest(Main.Base.string, $(esc(msg))))
236236
elseif isdefined(Main, :Base) && isdefined(Main.Base, :string) && applicable(Main.Base.string, msg)
237237
msg = Main.Base.string(msg)
238238
else
239239
# string() might not be defined during bootstrap
240-
msg = :(_assert_tostring($(Expr(:quote,msg))))
240+
msg = :(Main.Base.invokelatest(_assert_tostring, $(Expr(:quote,msg))))
241241
end
242242
return :($(esc(ex)) ? $(nothing) : throw(AssertionError($msg)))
243243
end

0 commit comments

Comments
 (0)