Skip to content

Commit

Permalink
form conditional constraint from non-Conditional object
Browse files Browse the repository at this point in the history
We can form a conditional constraint from a branch condition itself
when its type isn't already `Conditional` but can be refined as
`Conditional(condx, Const(true), Const(false))`.
  • Loading branch information
aviatesk committed May 19, 2021
1 parent 4211c3f commit 5868354
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
10 changes: 7 additions & 3 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1667,13 +1667,17 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState)
elseif isa(stmt, GotoNode)
pc´ = (stmt::GotoNode).label
elseif isa(stmt, GotoIfNot)
condt = abstract_eval_value(interp, stmt.cond, changes, frame)
condx = stmt.cond
condt = abstract_eval_value(interp, condx, changes, frame)
if condt === Bottom
empty!(frame.pclimitations)
end
if condt === Bottom
break
end
if !(isa(condt, Const) || isa(condt, Conditional)) && isa(condx, SlotNumber)
# if this non-`Conditional` object is a slot, we form and propagate
# the conditional constraint on it
condt = Conditional(condx, Const(true), Const(false))
end
condval = maybe_extract_const_bool(condt)
l = stmt.dest::Int
if !isempty(frame.pclimitations)
Expand Down
18 changes: 18 additions & 0 deletions test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,24 @@ end
end == Any[Tuple{Int,Int}]
end

@testset "conditional constraint propagation from non-`Conditional` object" begin
@test Base.return_types((Bool,)) do b
if b
return !b ? nothing : 1 # ::Int
else
return 0
end
end == Any[Int]

@test Base.return_types((Any,)) do b
if b
return b # ::Bool
else
return nothing
end
end == Any[Union{Bool,Nothing}]
end

function f25579(g)
h = g[]
t = (h === nothing)
Expand Down

0 comments on commit 5868354

Please sign in to comment.