Skip to content

Commit

Permalink
inference: fix JuliaLang#46207, make sure we never form nested `Condi…
Browse files Browse the repository at this point in the history
…tional` (JuliaLang#46208)
  • Loading branch information
aviatesk committed Jul 29, 2022
1 parent 255acea commit 823766e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1498,20 +1498,26 @@ function abstract_call_builtin(interp::AbstractInterpreter, f::Builtin, (; fargs
if isa(aty, Const) && isa(b, SlotNumber)
if rt === Const(false)
aty = Union{}
bty = widenconditional(bty)
elseif rt === Const(true)
bty = Union{}
elseif bty isa Type && isdefined(typeof(aty.val), :instance) # can only widen a if it is a singleton
bty = typesubtract(bty, typeof(aty.val), InferenceParams(interp).MAX_UNION_SPLITTING)
else
bty = widenconditional(bty)
end
return Conditional(b, aty, bty)
end
if isa(bty, Const) && isa(a, SlotNumber)
if rt === Const(false)
bty = Union{}
aty = widenconditional(aty)
elseif rt === Const(true)
aty = Union{}
elseif aty isa Type && isdefined(typeof(bty.val), :instance) # same for b
aty = typesubtract(aty, typeof(bty.val), InferenceParams(interp).MAX_UNION_SPLITTING)
else
aty = widenconditional(aty)
end
return Conditional(a, bty, aty)
end
Expand Down
10 changes: 10 additions & 0 deletions test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2102,6 +2102,16 @@ let M = Module()
@test rt == Tuple{Union{Nothing,Int},Any}
end

# make sure we never form nested `Conditional` (https://github.com/JuliaLang/julia/issues/46207)
@test Base.return_types((Any,)) do a
c = isa(a, Integer)
42 === c ? :a : "b"
end |> only === String
@test Base.return_types((Any,)) do a
c = isa(a, Integer)
c === 42 ? :a : "b"
end |> only === String

@testset "conditional constraint propagation from non-`Conditional` object" begin
@test Base.return_types((Bool,)) do b
if b
Expand Down

0 comments on commit 823766e

Please sign in to comment.