Skip to content

Commit

Permalink
Let tmerge form a Union more often
Browse files Browse the repository at this point in the history
Let tmerge decide whether to directly form a Union based on unioncomplexity, not
concreteness.
  • Loading branch information
martinholters committed Jul 13, 2018
1 parent 1f1b1b8 commit f72bb13
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 4 additions & 3 deletions base/compiler/typelimits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,10 @@ function tmerge(@nospecialize(typea), @nospecialize(typeb))
# XXX: this should never happen
return Any
end
# it's always ok to form a Union of two concrete types
if (isconcretetype(typea) || isType(typea)) && (isconcretetype(typeb) || isType(typeb))
return Union{typea, typeb}
# it's always ok to form a Union of two Union-free types
u = Union{typea, typeb}
if unioncomplexity(u) <= 1
return u
end
# collect the list of types from past tmerge calls returning Union
# and then reduce over that list
Expand Down
6 changes: 6 additions & 0 deletions test/compiler/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1731,6 +1731,12 @@ Base.iterate(::Iterator27434, ::Any) = nothing
@test @inferred splat27434(Iterator27434(1, 2, 3)) == (1, 2, 3)
@test Core.Compiler.return_type(splat27434, Tuple{typeof(Iterators.repeated(1))}) == Union{}

# PR #27843
bar27843(x, y::Bool) = fill(x, 0)
bar27843(x, y) = fill(x, ntuple(_ -> 0, y))::Array{typeof(x)}
foo27843(x, y) = bar27843(x, y)
@test Core.Compiler.return_type(foo27843, Tuple{Union{Float64,Int}, Any}) == Union{Array{Float64}, Array{Int}}

# issue #27078
f27078(T::Type{S}) where {S} = isa(T, UnionAll) ? f27078(T.body) : T
T27078 = Vector{Vector{T}} where T
Expand Down

0 comments on commit f72bb13

Please sign in to comment.