Skip to content

Commit

Permalink
inference: follow up JuliaLang#43603, better getfield_tfunc impl (J…
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk authored and LilithHafner committed Feb 22, 2022
1 parent 9c1b259 commit 4779826
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
26 changes: 13 additions & 13 deletions base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -795,17 +795,13 @@ function getfield_tfunc(s00, name, order, boundscheck)
end
getfield_tfunc(@nospecialize(s00), @nospecialize(name)) = _getfield_tfunc(s00, name, false)
function _getfield_tfunc(@nospecialize(s00), @nospecialize(name), setfield::Bool)
s = unwrap_unionall(s00)
if isa(s, Union)
return tmerge(getfield_tfunc(rewrap_unionall(s.a, s00), name),
getfield_tfunc(rewrap_unionall(s.b, s00), name))
elseif isa(s, Conditional)
if isa(s00, Conditional)
return Bottom # Bool has no fields
elseif isa(s, Const) || isconstType(s)
if !isa(s, Const)
sv = s.parameters[1]
elseif isa(s00, Const) || isconstType(s00)
if !isa(s00, Const)
sv = s00.parameters[1]
else
sv = s.val
sv = s00.val
end
if isa(name, Const)
nv = name.val
Expand Down Expand Up @@ -845,11 +841,15 @@ function _getfield_tfunc(@nospecialize(s00), @nospecialize(name), setfield::Bool
return unwrapva(s00.fields[nv])
end
end
else
s = unwrap_unionall(s00)
end
if isType(s) || !isa(s, DataType) || isabstracttype(s)
return Any
if isa(s, Union)
return tmerge(_getfield_tfunc(rewrap_unionall(s.a, s00), name, setfield),
_getfield_tfunc(rewrap_unionall(s.b, s00), name, setfield))
end
s = s::DataType
isa(s, DataType) || return Any
isabstracttype(s) && return Any
if s <: Tuple && !(Int <: widenconst(name))
return Bottom
end
Expand All @@ -873,7 +873,7 @@ function _getfield_tfunc(@nospecialize(s00), @nospecialize(name), setfield::Bool
if !(_ts <: Tuple)
return Any
end
return getfield_tfunc(_ts, name)
return _getfield_tfunc(_ts, name, setfield)
end
ftypes = datatype_fieldtypes(s)
nf = length(ftypes)
Expand Down
1 change: 1 addition & 0 deletions test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,7 @@ end
@test setfield!_tfunc(Const(@__MODULE__), Const(:v), Int) === Union{}
@test setfield!_tfunc(Const(@__MODULE__), Int, Int) === Union{}
@test setfield!_tfunc(Module, Const(:v), Int) === Union{}
@test setfield!_tfunc(Union{Module,Base.RefValue{Any}}, Const(:v), Int) === Union{}
@test setfield!_tfunc(ABCDconst, Const(:a), Any) === Union{}
@test setfield!_tfunc(ABCDconst, Const(:b), Any) === Union{}
@test setfield!_tfunc(ABCDconst, Const(:d), Any) === Union{}
Expand Down

0 comments on commit 4779826

Please sign in to comment.