Skip to content

Commit f72bb13

Browse files
committed
Let tmerge form a Union more often
Let tmerge decide whether to directly form a Union based on unioncomplexity, not concreteness.
1 parent 1f1b1b8 commit f72bb13

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

base/compiler/typelimits.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,10 @@ function tmerge(@nospecialize(typea), @nospecialize(typeb))
335335
# XXX: this should never happen
336336
return Any
337337
end
338-
# it's always ok to form a Union of two concrete types
339-
if (isconcretetype(typea) || isType(typea)) && (isconcretetype(typeb) || isType(typeb))
340-
return Union{typea, typeb}
338+
# it's always ok to form a Union of two Union-free types
339+
u = Union{typea, typeb}
340+
if unioncomplexity(u) <= 1
341+
return u
341342
end
342343
# collect the list of types from past tmerge calls returning Union
343344
# and then reduce over that list

test/compiler/compiler.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,12 @@ Base.iterate(::Iterator27434, ::Any) = nothing
17311731
@test @inferred splat27434(Iterator27434(1, 2, 3)) == (1, 2, 3)
17321732
@test Core.Compiler.return_type(splat27434, Tuple{typeof(Iterators.repeated(1))}) == Union{}
17331733

1734+
# PR #27843
1735+
bar27843(x, y::Bool) = fill(x, 0)
1736+
bar27843(x, y) = fill(x, ntuple(_ -> 0, y))::Array{typeof(x)}
1737+
foo27843(x, y) = bar27843(x, y)
1738+
@test Core.Compiler.return_type(foo27843, Tuple{Union{Float64,Int}, Any}) == Union{Array{Float64}, Array{Int}}
1739+
17341740
# issue #27078
17351741
f27078(T::Type{S}) where {S} = isa(T, UnionAll) ? f27078(T.body) : T
17361742
T27078 = Vector{Vector{T}} where T

0 commit comments

Comments
 (0)