Skip to content

Commit 6cd2a9d

Browse files
authored
inference: handle Union with TypeVar properly within isdefined_tfunc (#46534)
Improves the robustness of `isdefined_tfunc` when it splits `Union` with `TypeVar`s. Originally reported at <aviatesk/JET.jl#379>.
1 parent 431071b commit 6cd2a9d

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

base/compiler/tfuncs.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,14 @@ end
257257
isdefined_tfunc(arg1, sym, order) = (@nospecialize; isdefined_tfunc(arg1, sym))
258258
function isdefined_tfunc(@nospecialize(arg1), @nospecialize(sym))
259259
if isa(arg1, Const)
260-
a1 = typeof(arg1.val)
260+
arg1t = typeof(arg1.val)
261261
else
262-
a1 = widenconst(arg1)
262+
arg1t = widenconst(arg1)
263263
end
264-
if isType(a1)
264+
if isType(arg1t)
265265
return Bool
266266
end
267-
a1 = unwrap_unionall(a1)
267+
a1 = unwrap_unionall(arg1t)
268268
if isa(a1, DataType) && !isabstracttype(a1)
269269
if a1 === Module
270270
hasintersect(widenconst(sym), Symbol) || return Bottom
@@ -307,8 +307,8 @@ function isdefined_tfunc(@nospecialize(arg1), @nospecialize(sym))
307307
end
308308
end
309309
elseif isa(a1, Union)
310-
return tmerge(isdefined_tfunc(a1.a, sym),
311-
isdefined_tfunc(a1.b, sym))
310+
return tmerge(isdefined_tfunc(rewrap_unionall(a1.a, arg1t), sym),
311+
isdefined_tfunc(rewrap_unionall(a1.b, arg1t), sym))
312312
end
313313
return Bool
314314
end

test/compiler/inference.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,9 @@ struct UnionIsdefinedB; x; end
11601160
@test isdefined_tfunc(Union{UnionIsdefinedA,UnionIsdefinedB}, Const(:x)) === Const(true)
11611161
@test isdefined_tfunc(Union{UnionIsdefinedA,UnionIsdefinedB}, Const(:y)) === Const(false)
11621162
@test isdefined_tfunc(Union{UnionIsdefinedA,Nothing}, Const(:x)) === Bool
1163+
# https://github.com/aviatesk/JET.jl/issues/379
1164+
fJET379(x::Union{Complex{T}, T}) where T = isdefined(x, :im)
1165+
@test only(Base.return_types(fJET379)) === Bool
11631166

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

0 commit comments

Comments
 (0)