Skip to content

Commit 4b27a16

Browse files
authored
inference: add missing TypeVar handling for instanceof_tfunc (#55884)
I thought these sort of problems had been addressed by d60f92c, but it seems some were missed. Specifically, `t.a` and `t.b` from `t::Union` could be `TypeVar`, and if they are passed to a subroutine or recursed without being unwrapped or rewrapped, errors like #55882 could occur. This commit resolves the issue by calling `unwraptv` in the `Union` handling within `instanceof_tfunc`. I also found a similar issue inside `nfields_tfunc`, so that has also been fixed, and test cases have been added. While I haven't been able to make up a test case specifically for the fix in `instanceof_tfunc`, I have confirmed that this commit certainly fixes the issue reported in #55882. - fixes #55882
1 parent 60be409 commit 4b27a16

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

base/compiler/tfuncs.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ function instanceof_tfunc(@nospecialize(t), astag::Bool=false, @nospecialize(tro
135135
end
136136
return tr, isexact, isconcrete, istype
137137
elseif isa(t, Union)
138-
ta, isexact_a, isconcrete_a, istype_a = instanceof_tfunc(t.a, astag, troot)
139-
tb, isexact_b, isconcrete_b, istype_b = instanceof_tfunc(t.b, astag, troot)
138+
ta, isexact_a, isconcrete_a, istype_a = instanceof_tfunc(unwraptv(t.a), astag, troot)
139+
tb, isexact_b, isconcrete_b, istype_b = instanceof_tfunc(unwraptv(t.b), astag, troot)
140140
isconcrete = isconcrete_a && isconcrete_b
141141
istype = istype_a && istype_b
142142
# most users already handle the Union case, so here we assume that
@@ -563,9 +563,9 @@ add_tfunc(Core.sizeof, 1, 1, sizeof_tfunc, 1)
563563
end
564564
end
565565
if isa(x, Union)
566-
na = nfields_tfunc(𝕃, x.a)
566+
na = nfields_tfunc(𝕃, unwraptv(x.a))
567567
na === Int && return Int
568-
return tmerge(na, nfields_tfunc(𝕃, x.b))
568+
return tmerge(𝕃, na, nfields_tfunc(𝕃, unwraptv(x.b)))
569569
end
570570
return Int
571571
end

test/compiler/inference.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6152,3 +6152,6 @@ end
61526152
t155751 = Union{AbstractArray{UInt8, 4}, Array{Float32, 4}, Grid55751{Float32, 3, _A} where _A}
61536153
t255751 = Array{Float32, 3}
61546154
@test Core.Compiler.tmerge_types_slow(t155751,t255751) == AbstractArray # shouldn't hang
6155+
6156+
issue55882_nfields(x::Union{T,Nothing}) where T<:Number = nfields(x)
6157+
@test Base.infer_return_type(issue55882_nfields) <: Int

0 commit comments

Comments
 (0)