Skip to content

Commit

Permalink
when widening tuple types in tmerge, only widen the complex parts
Browse files Browse the repository at this point in the history
  • Loading branch information
oscardssmith committed Aug 15, 2023
1 parent 0b190b3 commit 972025f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
25 changes: 13 additions & 12 deletions base/compiler/typelimits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -764,23 +764,24 @@ end
return u
end
# don't let the slow widening of Tuple cause the whole type to grow too fast
# Specifically widen Tuple{..., Union{lots of stuff}...} to Tuple{..., Any, ...}
for i in 1:length(types)
if typenames[i] === Tuple.name
widen = unwrap_unionall(types[i])
if isa(widen, DataType) && !isvatuple(widen)
widen = NTuple{length(widen.parameters), Any}
else
widen = Tuple
end
types[i] = widen
u = Union{types...}
if issimpleenoughtype(u)
return u
ti = types[i]
tip = (unwrap_unionall(types[i])::DataType).parameters
lt = length(tip)
p = Vector{Any}(undef, lt)
for j = 1:lt
ui = tip[j]
p[j] = issimpleenoughtype(ui) ? ui : isvarargtype(ui) ? Vararg : Any
end
break
types[i] = rewrap_unionall(Tuple{p...}, ti)
end
end
# finally, just return the widest possible type
u = Union{types...}
if issimpleenoughtype(u)
return u
end
return Any
end

Expand Down
5 changes: 4 additions & 1 deletion test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,10 @@ tmerge_test(Tuple{}, Tuple{Complex, Vararg{Union{ComplexF32, ComplexF64}}},
@test Core.Compiler.tmerge(Base.BitIntegerType, Union{}) === Base.BitIntegerType
@test Core.Compiler.tmerge(Union{}, Base.BitIntegerType) === Base.BitIntegerType
@test Core.Compiler.tmerge(Core.Compiler.fallback_ipo_lattice, Core.Compiler.InterConditional(1, Int, Union{}), Core.Compiler.InterConditional(2, String, Union{})) === Core.Compiler.Const(true)

# test issue behind https://github.com/JuliaLang/julia/issues/50458
@test Core.Compiler.tmerge(Nothing, Tuple{Base.BitInteger, Int}) == Union{Nothing, Tuple{Any, Int}}
@test Core.Compiler.tmerge(Nothing, Tuple{Union{Char, String, SubString{String}, Symbol}, Int}) == Union{Nothing, Tuple{Any, Int}}
@test Core.Compiler.tmerge(Nothing, Tuple{Integer, Int}) == Union{Nothing, Tuple{Integer, Int}}
struct SomethingBits
x::Base.BitIntegerType
end
Expand Down

0 comments on commit 972025f

Please sign in to comment.