Skip to content

Commit 204b0b9

Browse files
committed
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 242f704 commit 204b0b9

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
@@ -134,8 +134,8 @@ function instanceof_tfunc(@nospecialize(t), @nospecialize(troot) = t)
134134
end
135135
return tr, isexact, isconcrete, istype
136136
elseif isa(t, Union)
137-
ta, isexact_a, isconcrete_a, istype_a = instanceof_tfunc(t.a, troot)
138-
tb, isexact_b, isconcrete_b, istype_b = instanceof_tfunc(t.b, troot)
137+
ta, isexact_a, isconcrete_a, istype_a = instanceof_tfunc(unwraptv(t.a), troot)
138+
tb, isexact_b, isconcrete_b, istype_b = instanceof_tfunc(unwraptv(t.b), troot)
139139
isconcrete = isconcrete_a && isconcrete_b
140140
istype = istype_a && istype_b
141141
# most users already handle the Union case, so here we assume that
@@ -536,9 +536,9 @@ add_tfunc(Core.sizeof, 1, 1, sizeof_tfunc, 1)
536536
end
537537
end
538538
if isa(x, Union)
539-
na = nfields_tfunc(𝕃, x.a)
539+
na = nfields_tfunc(𝕃, unwraptv(x.a))
540540
na === Int && return Int
541-
return tmerge(na, nfields_tfunc(𝕃, x.b))
541+
return tmerge(𝕃, na, nfields_tfunc(𝕃, unwraptv(x.b)))
542542
end
543543
return Int
544544
end

test/compiler/inference.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5161,3 +5161,6 @@ let x = 1, _Any = Any
51615161
foo27031() = bar27031((x, 1.0), Val{_Any})
51625162
@test foo27031() == "OK"
51635163
end
5164+
5165+
issue55882_nfields(x::Union{T,Nothing}) where T<:Number = nfields(x)
5166+
@test only(Base.return_types(issue55882_nfields)) <: Int

0 commit comments

Comments
 (0)