Skip to content

Commit b92e23e

Browse files
aviateskKristofferC
authored andcommitted
fix tuple_tfunc on Union containing Type{...} (#44725)
fix #44705 (cherry picked from commit 4115686)
1 parent d7819e2 commit b92e23e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

base/compiler/tfuncs.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,10 @@ function tuple_tfunc(argtypes::Vector{Any})
15151515
params[i] = typeof(x.val)
15161516
else
15171517
x = isvarargtype(x) ? x : widenconst(x)
1518+
# since there don't exist any values whose runtime type are `Tuple{Type{...}}`,
1519+
# here we should turn such `Type{...}`-parameters to valid parameters, e.g.
1520+
# (::Type{Int},) -> Tuple{DataType} (or PartialStruct for more accuracy)
1521+
# (::Union{Type{Int32},Type{Int64}}) -> Tuple{Type}
15181522
if isType(x)
15191523
anyinfo = true
15201524
xparam = x.parameters[1]
@@ -1523,6 +1527,8 @@ function tuple_tfunc(argtypes::Vector{Any})
15231527
else
15241528
params[i] = Type
15251529
end
1530+
elseif !isvarargtype(x) && hasintersect(x, Type)
1531+
params[i] = Union{x, Type}
15261532
else
15271533
params[i] = x
15281534
end

test/compiler/inference.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,15 @@ end
15641564
@test arraysize_tfunc(Vector, Float64) === Union{}
15651565
@test arraysize_tfunc(String, Int) === Union{}
15661566

1567+
let tuple_tfunc
1568+
function tuple_tfunc(@nospecialize xs...)
1569+
return Core.Compiler.tuple_tfunc(Any[xs...])
1570+
end
1571+
@test Core.Compiler.widenconst(tuple_tfunc(Type{Int})) === Tuple{DataType}
1572+
# https://github.com/JuliaLang/julia/issues/44705
1573+
@test tuple_tfunc(Union{Type{Int32},Type{Int64}}) === Tuple{Type}
1574+
end
1575+
15671576
function f23024(::Type{T}, ::Int) where T
15681577
1 + 1
15691578
end
@@ -2084,7 +2093,7 @@ let M = Module()
20842093
obj = $(Expr(:new, M.BePartialStruct, 42, :cond))
20852094
r1 = getfield(obj, :cond) ? 0 : a # r1::Union{Nothing,Int}, not r1::Int (because PartialStruct doesn't wrap Conditional)
20862095
a = $(gensym(:anyvar))::Any
2087-
r2 = getfield(obj, :cond) ? a : nothing # r2::Any, not r2::Const(nothing) (we don't need to worry about constrait invalidation here)
2096+
r2 = getfield(obj, :cond) ? a : nothing # r2::Any, not r2::Const(nothing) (we don't need to worry about constraint invalidation here)
20882097
return r1, r2 # ::Tuple{Union{Nothing,Int},Any}
20892098
end |> only
20902099
end

0 commit comments

Comments
 (0)