Skip to content

Commit cfd039b

Browse files
committed
Widen unions less agressively during inference
Use `typejoin` for unions growing too large instead of just returning `Any`.
1 parent caa7f04 commit cfd039b

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

base/inference.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2810,9 +2810,12 @@ function tmerge(@nospecialize(typea), @nospecialize(typeb))
28102810
end
28112811
end
28122812
u = Union{typea, typeb}
2813-
if unionlen(u) > MAX_TYPEUNION_LEN || type_too_complex(u, MAX_TYPE_DEPTH)
2814-
# don't let type unions get too big
2815-
# TODO: something smarter, like a common supertype
2813+
if unionlen(u) > MAX_TYPEUNION_LEN
2814+
u = typejoin(typea, typeb)
2815+
end
2816+
if type_too_complex(u, MAX_TYPE_DEPTH)
2817+
# helps convergence speed (large types that are changing value become very slow to
2818+
# work with very quickly)
28162819
return Any
28172820
end
28182821
return u

test/inference.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,3 +1291,6 @@ let T1 = Array{Float64}, T2 = Array{_1,2} where _1
12911291
rt = Base.return_types(g, (Union{Ref{Array{Float64}}, Ref{Array{Float32}}},))[1]
12921292
@test rt >: Union{Type{Array{Float64}}, Type{Array{Float32}}}
12931293
end
1294+
1295+
f_23077(x) = (Int8(0), Int16(0), Int32(0), Int64(0))[x]
1296+
@test Base.return_types(f_23077, Tuple{Int})[1] <: Signed

0 commit comments

Comments
 (0)