Skip to content

Commit 41b82ee

Browse files
committed
use inferencebarrier instead of invokelatest for 1-arg @assert
This version would be better as per this comment: <#55739 (review)> I confirmed this still allows us to avoid invalidations reported at #55583.
1 parent a993cd8 commit 41b82ee

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

base/error.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,14 @@ 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.invokelatest(Main.Base.string, $(esc(msg))))
235+
# N.B. To reduce the risk of invalidation caused by the complex callstack involved
236+
# with `string`, use `inferencebarrier` here to hide this `string` from the compiler.
237+
msg = :(Main.Base.inferencebarrier(Main.Base.string)($(esc(msg))))
236238
elseif isdefined(Main, :Base) && isdefined(Main.Base, :string) && applicable(Main.Base.string, msg)
237239
msg = Main.Base.string(msg)
238240
else
239241
# string() might not be defined during bootstrap
240-
msg = :(Main.Base.invokelatest(_assert_tostring, $(Expr(:quote,msg))))
242+
msg = :(Main.Base.inferencebarrier(_assert_tostring)($(Expr(:quote,msg))))
241243
end
242244
return :($(esc(ex)) ? $(nothing) : throw(AssertionError($msg)))
243245
end

base/strings/string.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ end
208208
i = i′
209209
@inbounds l = codeunit(s, i)
210210
(l < 0x80) | (0xf8 l) && return i+1
211-
@assert l >= 0xc0 "invalid codeunit"
211+
@assert l >= 0xc0
212212
end
213213
# first continuation byte
214214
(i += 1) > n && return i

0 commit comments

Comments
 (0)