Skip to content

Commit 10b010f

Browse files
authored
fix partially_inline! with isdefined (#40628)
ref #40562 (comment)
1 parent 1810952 commit 10b010f

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

base/meta.jl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,10 @@ function _partially_inline!(@nospecialize(x), slot_replacements::Vector{Any},
370370
if isa(x, Expr)
371371
head = x.head
372372
if head === :static_parameter
373-
return QuoteNode(static_param_values[x.args[1]])
373+
if isassigned(static_param_values, x.args[1])
374+
return QuoteNode(static_param_values[x.args[1]])
375+
end
376+
return x
374377
elseif head === :cfunction
375378
@assert !isa(type_signature, UnionAll) || !isempty(spvals)
376379
if !isa(x.args[2], QuoteNode) # very common no-op
@@ -413,6 +416,30 @@ function _partially_inline!(@nospecialize(x), slot_replacements::Vector{Any},
413416
x.args[2] += statement_offset
414417
elseif head === :enter
415418
x.args[1] += statement_offset
419+
elseif head === :isdefined
420+
arg = x.args[1]
421+
# inlining a QuoteNode or literal into `Expr(:isdefined, x)` is invalid, replace with true
422+
if isa(arg, Core.SlotNumber)
423+
id = arg.id
424+
if 1 <= id <= length(slot_replacements)
425+
replacement = slot_replacements[id]
426+
if isa(replacement, Union{Core.SlotNumber, GlobalRef, Symbol})
427+
return Expr(:isdefined, replacement)
428+
else
429+
@assert !isa(replacement, Expr)
430+
return true
431+
end
432+
end
433+
return Expr(:isdefined, Core.SlotNumber(id + slot_offset))
434+
elseif isexpr(arg, :static_parameter)
435+
if isassigned(static_param_values, arg.args[1])
436+
return true
437+
end
438+
return x
439+
else
440+
@assert isa(arg, Union{GlobalRef, Symbol})
441+
return x
442+
end
416443
elseif !is_meta_expr_head(head)
417444
partially_inline!(x.args, slot_replacements, type_signature, static_param_values,
418445
slot_offset, statement_offset, boundscheck)

test/meta.jl

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,29 @@ ci = code_lowered(f, Tuple{Int})[1]
241241

242242
g(::Val{x}) where {x} = x ? 1 : 0
243243
ci = code_lowered(g, Tuple{Val{true}})[1]
244-
@test Meta.partially_inline!(ci.code, [], Tuple{typeof(g),Val{true}}, Any[Val{true}], 0, 0, :propagate)[1] ==
245-
Core.GotoIfNot(QuoteNode(Val{true}), 3)
246-
@test Meta.partially_inline!(ci.code, [], Tuple{typeof(g),Val{true}}, Any[Val{true}], 0, 2, :propagate)[1] ==
247-
Core.GotoIfNot(QuoteNode(Val{true}), 5)
244+
@test Meta.partially_inline!(ci.code, [], Tuple{typeof(g),Val{true}}, Any[true], 0, 0, :propagate)[1] ==
245+
Core.GotoIfNot(QuoteNode(true), 3)
246+
@test Meta.partially_inline!(ci.code, [], Tuple{typeof(g),Val{true}}, Any[true], 0, 2, :propagate)[1] ==
247+
Core.GotoIfNot(QuoteNode(true), 5)
248+
249+
@testset "inlining with isdefined" begin
250+
isdefined_slot(x) = @isdefined(x)
251+
ci = code_lowered(isdefined_slot, Tuple{Int})[1]
252+
@test Meta.partially_inline!(copy(ci.code), [], Tuple{typeof(isdefined_slot), Int},
253+
[], 0, 0, :propagate)[1] == Expr(:isdefined, Core.SlotNumber(2))
254+
@test Meta.partially_inline!(copy(ci.code), [isdefined_slot, 1], Tuple{typeof(isdefined_slot), Int},
255+
[], 0, 0, :propagate)[1] == true
256+
257+
isdefined_sparam(::T) where {T} = @isdefined(T)
258+
ci = code_lowered(isdefined_sparam, Tuple{Int})[1]
259+
@test Meta.partially_inline!(copy(ci.code), [], Tuple{typeof(isdefined_sparam), Int},
260+
Any[Int], 0, 0, :propagate)[1] == true
261+
@test Meta.partially_inline!(copy(ci.code), [], Tuple{typeof(isdefined_sparam), Int},
262+
[], 0, 0, :propagate)[1] == Expr(:isdefined, Expr(:static_parameter, 1))
263+
264+
@eval isdefined_globalref(x) = $(Expr(:isdefined, GlobalRef(Base, :foo)))
265+
ci = code_lowered(isdefined_globalref, Tuple{Int})[1]
266+
@test Meta.partially_inline!(copy(ci.code), Any[isdefined_globalref, 1], Tuple{typeof(isdefined_globalref), Int},
267+
[], 0, 0, :propagate)[1] == Expr(:isdefined, GlobalRef(Base, :foo))
268+
269+
end

0 commit comments

Comments
 (0)