Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inference: fix #46207, make sure we never form nested Conditional #46208

Merged
merged 1 commit into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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