Skip to content

Commit 6c1824d

Browse files
Let tmerge form a Union more often (#27843)
Let tmerge decide whether to directly form a Union based on unioncomplexity, not concreteness.
1 parent a3369df commit 6c1824d

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

base/compiler/typelimits.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,10 @@ function tmerge(@nospecialize(typea), @nospecialize(typeb))
355355
return Any
356356
end
357357
typea == typeb && return typea
358-
# it's always ok to form a Union of two concrete types
359-
if (isconcretetype(typea) || isType(typea)) && (isconcretetype(typeb) || isType(typeb))
360-
return Union{typea, typeb}
358+
# it's always ok to form a Union of two Union-free types
359+
u = Union{typea, typeb}
360+
if unioncomplexity(u) <= 1
361+
return u
361362
end
362363
# collect the list of types from past tmerge calls returning Union
363364
# and then reduce over that list

test/compiler/inference.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,6 +2041,16 @@ let rt = Base.return_types(splat27434, (NamedTuple{(:x,), Tuple{T}} where T,))
20412041
@test !Base.has_free_typevars(rt[1])
20422042
end
20432043

2044+
# PR #27843
2045+
bar27843(x, y::Bool) = fill(x, 0)
2046+
bar27843(x, y) = fill(x, ntuple(_ -> 0, y))::Array{typeof(x)}
2047+
foo27843(x, y) = bar27843(x, y)
2048+
@test Core.Compiler.return_type(foo27843, Tuple{Union{Float64,Int}, Any}) == Union{Array{Float64}, Array{Int}}
2049+
let atypes1 = Tuple{Union{IndexCartesian, IndexLinear}, Any, Union{Tuple{}, Tuple{Any,Vararg{Any,N}} where N}, Tuple},
2050+
atypes2 = Tuple{Union{IndexCartesian, IndexLinear}, Any, Tuple, Tuple}
2051+
@test Core.Compiler.return_type(SubArray, atypes1) <: Core.Compiler.return_type(SubArray, atypes2)
2052+
end
2053+
20442054
# issue #27078
20452055
f27078(T::Type{S}) where {S} = isa(T, UnionAll) ? f27078(T.body) : T
20462056
T27078 = Vector{Vector{T}} where T

0 commit comments

Comments
 (0)