Skip to content

Commit 0e0b1a7

Browse files
author
oscarddssmith
committed
when widening tuple types in tmerge, only widen the complex parts
1 parent 0b190b3 commit 0e0b1a7

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

base/compiler/typelimits.jl

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -764,23 +764,26 @@ end
764764
return u
765765
end
766766
# don't let the slow widening of Tuple cause the whole type to grow too fast
767+
# Specifically widen Tuple{..., Union{lots of stuff}...} to Tuple{..., Any, ...}
767768
for i in 1:length(types)
769+
issimpleenoughtype(types[i]) && continue
770+
# this Tuple is too complicated, so widen any of it's complicated parameters
768771
if typenames[i] === Tuple.name
769-
widen = unwrap_unionall(types[i])
770-
if isa(widen, DataType) && !isvatuple(widen)
771-
widen = NTuple{length(widen.parameters), Any}
772-
else
773-
widen = Tuple
774-
end
775-
types[i] = widen
776-
u = Union{types...}
777-
if issimpleenoughtype(u)
778-
return u
772+
ti = types[i]
773+
tip = (unwrap_unionall(types[i])::DataType).parameters
774+
lt = length(tip)
775+
p = Vector{Any}(undef, lt)
776+
for j = 1:lt
777+
ui = tip[j]
778+
p[j] = issimpleenoughtype(ui) ? ui : isvarargtype(ui) ? Vararg : Any
779779
end
780-
break
780+
types[i] = rewrap_unionall(Tuple{p...}, ti)
781781
end
782782
end
783-
# finally, just return the widest possible type
783+
u = Union{types...}
784+
if issimpleenoughtype(u)
785+
return u
786+
end
784787
return Any
785788
end
786789

test/compiler/inference.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ tmerge_test(Tuple{}, Tuple{Complex, Vararg{Union{ComplexF32, ComplexF64}}},
213213
@test Core.Compiler.tmerge(Base.BitIntegerType, Union{}) === Base.BitIntegerType
214214
@test Core.Compiler.tmerge(Union{}, Base.BitIntegerType) === Base.BitIntegerType
215215
@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)
216-
216+
# test issue behind https://github.com/JuliaLang/julia/issues/50458
217+
@test Core.Compiler.tmerge(Nothing, Tuple{Base.BitInteger, Int}) == Union{Nothing, Tuple{Any, Int}}
218+
@test Core.Compiler.tmerge(Nothing, Tuple{Integer, Int}) == Union{Nothing, Tuple{Integer, Int}}
217219
struct SomethingBits
218220
x::Base.BitIntegerType
219221
end

0 commit comments

Comments
 (0)