Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions base/compiler/typelimits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,10 @@ function tmerge(@nospecialize(typea), @nospecialize(typeb))
return Any
end
typea == typeb && return typea
# 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think an alternate literal translation of this would be isa(unwrap_unionall(T), DataType)

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
10 changes: 10 additions & 0 deletions test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2033,6 +2033,16 @@ let rt = Base.return_types(splat27434, (NamedTuple{(:x,), Tuple{T}} where T,))
@test !Base.has_free_typevars(rt[1])
end

# 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}}
let atypes1 = Tuple{Union{IndexCartesian, IndexLinear}, Any, Union{Tuple{}, Tuple{Any,Vararg{Any,N}} where N}, Tuple},
atypes2 = Tuple{Union{IndexCartesian, IndexLinear}, Any, Tuple, Tuple}
@test Core.Compiler.return_type(SubArray, atypes1) <: Core.Compiler.return_type(SubArray, atypes2)
end

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