Skip to content

Commit c988bcc

Browse files
committed
fix #44705, fix tuple_tfunc on Union containing Type{...}
1 parent 89a613b commit c988bcc

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
@@ -1546,6 +1546,10 @@ function tuple_tfunc(argtypes::Vector{Any})
15461546
params[i] = typeof(x.val)
15471547
else
15481548
x = isvarargtype(x) ? x : widenconst(x)
1549+
# since there don't exist any values whose runtime type are `Tuple{Type{...}}`,
1550+
# here we should turn such `Type{...}`-parameters to valid parameters, e.g.
1551+
# (::Type{Int},) -> Tuple{DataType} (or PartialStruct for more accuracy)
1552+
# (::Union{Type{Int32},Type{Int64}}) -> Tuple{Type}
15491553
if isType(x)
15501554
anyinfo = true
15511555
xparam = x.parameters[1]
@@ -1554,6 +1558,8 @@ function tuple_tfunc(argtypes::Vector{Any})
15541558
else
15551559
params[i] = Type
15561560
end
1561+
elseif !isvarargtype(x) && hasintersect(x, Type)
1562+
params[i] = Union{x, Type}
15571563
else
15581564
params[i] = x
15591565
end

test/compiler/inference.jl

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

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

0 commit comments

Comments
 (0)