Skip to content

Commit

Permalink
Merge pull request #40832 from aviatesk/nonconditional
Browse files Browse the repository at this point in the history
inference: form conditional constraint from non-`Conditional` object
  • Loading branch information
aviatesk authored May 20, 2021
2 parents 79ee2b4 + 5868354 commit f77f4fe
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 f77f4fe

Please sign in to comment.