Skip to content

Commit 9d13e16

Browse files
aviateskKristofferC
authored andcommitted
inference: fix #42090, make sure not to wrap Conditional in PartialStruct (#42091)
(cherry picked from commit 03e7b23)
1 parent ef28749 commit 9d13e16

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@ function abstract_eval_statement(interp::AbstractInterpreter, @nospecialize(e),
11901190
anyconst = false
11911191
allconst = true
11921192
for i = 2:length(e.args)
1193-
at = abstract_eval_value(interp, e.args[i], vtypes, sv)
1193+
at = widenconditional(abstract_eval_value(interp, e.args[i], vtypes, sv))
11941194
if !anyconst
11951195
anyconst = has_nontrivial_const_info(at)
11961196
end

test/compiler/inference.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,29 @@ for expr25261 in opt25261[i:end]
17191719
end
17201720
@test foundslot
17211721

1722+
# https://github.com/JuliaLang/julia/issues/42090#issuecomment-911824851
1723+
# `PartialStruct` shoudln't wrap `Conditional`
1724+
let M = Module()
1725+
@eval M begin
1726+
struct BePartialStruct
1727+
val::Int
1728+
cond
1729+
end
1730+
end
1731+
1732+
rt = @eval M begin
1733+
Base.return_types((Union{Nothing,Int},)) do a
1734+
cond = a === nothing
1735+
obj = $(Expr(:new, M.BePartialStruct, 42, :cond))
1736+
r1 = getfield(obj, :cond) ? 0 : a # r1::Union{Nothing,Int}, not r1::Int (because PartialStruct doesn't wrap Conditional)
1737+
a = $(gensym(:anyvar))::Any
1738+
r2 = getfield(obj, :cond) ? a : nothing # r2::Any, not r2::Const(nothing) (we don't need to worry about constrait invalidation here)
1739+
return r1, r2 # ::Tuple{Union{Nothing,Int},Any}
1740+
end |> only
1741+
end
1742+
@test rt == Tuple{Union{Nothing,Int},Any}
1743+
end
1744+
17221745
function f25579(g)
17231746
h = g[]
17241747
t = (h === nothing)

0 commit comments

Comments
 (0)