Skip to content

Commit b01ec28

Browse files
vtjnashKristofferC
authored andcommitted
inference: fix lattice for unusual InterConditional return and Const Bool representations (#57080)
Fixes #54886 Rule introduced by 55cee67 (cherry picked from commit 4e13e0e)
1 parent 8f8217a commit b01ec28

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3065,7 +3065,7 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState)
30653065
rt = abstract_eval_value(interp, stmt.val, currstate, frame)
30663066
rt = widenreturn(rt, BestguessInfo(interp, bestguess, nargs, slottypes, currstate))
30673067
# narrow representation of bestguess slightly to prepare for tmerge with rt
3068-
if rt isa InterConditional && bestguess isa Const
3068+
if rt isa InterConditional && bestguess isa Const && bestguess.val isa Bool
30693069
let slot_id = rt.slot
30703070
old_id_type = slottypes[slot_id]
30713071
if bestguess.val === true && rt.elsetype !== Bottom
@@ -3074,6 +3074,15 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState)
30743074
bestguess = InterConditional(slot_id, Bottom, old_id_type)
30753075
end
30763076
end
3077+
# or narrow representation of rt slightly to prepare for tmerge with bestguess
3078+
elseif bestguess isa InterConditional && rt isa Const && rt.val isa Bool
3079+
slot_id = bestguess.slot
3080+
old_id_type = widenconditional(slottypes[slot_id])
3081+
if rt.val === true && bestguess.elsetype !== Bottom
3082+
rt = InterConditional(slot_id, old_id_type, Bottom)
3083+
elseif rt.val === false && bestguess.thentype !== Bottom
3084+
rt = InterConditional(slot_id, Bottom, old_id_type)
3085+
end
30773086
end
30783087
# copy limitations to return value
30793088
if !isempty(frame.pclimitations)

base/compiler/typelattice.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,13 @@ end
427427
end
428428
a = Bool
429429
elseif isa(b, ConditionalT)
430+
if isa(a, Const) && isa(a.val, Bool)
431+
if (a.val === true && b.thentype === Any && b.elsetype === Bottom) ||
432+
(a.val === false && b.elsetype === Any && b.thentype === Bottom)
433+
# this Conditional contains distinctly no lattice information, and is simply an alternative representation of the Const Bool used for internal tracking purposes
434+
return true
435+
end
436+
end
430437
return false
431438
end
432439
return (widenlattice(lattice), a, b)

test/compiler/inference.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2675,6 +2675,26 @@ vacond(cnd, va...) = cnd ? va : 0
26752675
vacond(isa(x, Tuple{Int,Int}), x, x)
26762676
end |> only == Union{Int,Tuple{Any,Any}}
26772677

2678+
let A = Core.Const(true)
2679+
B = Core.InterConditional(2, Tuple, Union{})
2680+
C = Core.InterConditional(2, Any, Union{})
2681+
L = ipo_lattice(Core.Compiler.NativeInterpreter())
2682+
@test !(L, A, B)
2683+
@test (L, B, A)
2684+
@test tmerge(L, A, B) == C
2685+
@test (L, A, C)
2686+
end
2687+
function tail_is_ntuple((@nospecialize t::Tuple))
2688+
if unknown
2689+
t isa Tuple
2690+
else
2691+
tail_is_ntuple(t)
2692+
end
2693+
end
2694+
tail_is_ntuple_val((@nospecialize t::Tuple)) = Val(tail_is_ntuple(t))
2695+
@test Base.return_types(tail_is_ntuple, (Tuple,)) |> only === Bool
2696+
@test Base.return_types(tail_is_ntuple_val, (Tuple,)) |> only === Val{true}
2697+
26782698
# https://github.com/JuliaLang/julia/issues/47435
26792699
is_closed_ex(e::InvalidStateException) = true
26802700
is_closed_ex(e) = false

0 commit comments

Comments
 (0)