Skip to content

Commit

Permalink
inference: handle Union with TypeVar properly within `isdefined_t…
Browse files Browse the repository at this point in the history
…func` (#46534)

Improves the robustness of `isdefined_tfunc` when it splits `Union`
with `TypeVar`s.
Originally reported at <aviatesk/JET.jl#379>.
  • Loading branch information
aviatesk authored Aug 29, 2022
1 parent 431071b commit 6cd2a9d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
12 changes: 6 additions & 6 deletions base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,14 @@ end
isdefined_tfunc(arg1, sym, order) = (@nospecialize; isdefined_tfunc(arg1, sym))
function isdefined_tfunc(@nospecialize(arg1), @nospecialize(sym))
if isa(arg1, Const)
a1 = typeof(arg1.val)
arg1t = typeof(arg1.val)
else
a1 = widenconst(arg1)
arg1t = widenconst(arg1)
end
if isType(a1)
if isType(arg1t)
return Bool
end
a1 = unwrap_unionall(a1)
a1 = unwrap_unionall(arg1t)
if isa(a1, DataType) && !isabstracttype(a1)
if a1 === Module
hasintersect(widenconst(sym), Symbol) || return Bottom
Expand Down Expand Up @@ -307,8 +307,8 @@ function isdefined_tfunc(@nospecialize(arg1), @nospecialize(sym))
end
end
elseif isa(a1, Union)
return tmerge(isdefined_tfunc(a1.a, sym),
isdefined_tfunc(a1.b, sym))
return tmerge(isdefined_tfunc(rewrap_unionall(a1.a, arg1t), sym),
isdefined_tfunc(rewrap_unionall(a1.b, arg1t), sym))
end
return Bool
end
Expand Down
3 changes: 3 additions & 0 deletions test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,9 @@ struct UnionIsdefinedB; x; end
@test isdefined_tfunc(Union{UnionIsdefinedA,UnionIsdefinedB}, Const(:x)) === Const(true)
@test isdefined_tfunc(Union{UnionIsdefinedA,UnionIsdefinedB}, Const(:y)) === Const(false)
@test isdefined_tfunc(Union{UnionIsdefinedA,Nothing}, Const(:x)) === Bool
# https://github.com/aviatesk/JET.jl/issues/379
fJET379(x::Union{Complex{T}, T}) where T = isdefined(x, :im)
@test only(Base.return_types(fJET379)) === Bool

@noinline map3_22347(f, t::Tuple{}) = ()
@noinline map3_22347(f, t::Tuple) = (f(t[1]), map3_22347(f, Base.tail(t))...)
Expand Down

0 comments on commit 6cd2a9d

Please sign in to comment.