Skip to content

Commit 811b3a3

Browse files
committed
Revert "Revert "Improve typesubtract for tuples (#35600)" (#37562)"
This reverts commit b18647e.
1 parent af6542e commit 811b3a3

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

base/compiler/typeutils.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ function typesubtract(@nospecialize(a), @nospecialize(b))
7070
if isa(a, Union)
7171
return Union{typesubtract(a.a, b),
7272
typesubtract(a.b, b)}
73+
elseif a isa DataType
74+
if b isa DataType
75+
if a.name === b.name === Tuple.name && length(a.types) == length(b.types)
76+
ta = switchtupleunion(a)
77+
if length(ta) > 1
78+
return typesubtract(Union{ta...}, b)
79+
end
80+
end
81+
end
7382
end
7483
return a # TODO: improve this bound?
7584
end

test/compiler/inference.jl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2592,6 +2592,47 @@ end
25922592

25932593
@test map(>:, [Int], [Int]) == [true]
25942594

2595+
# issue 35566
2596+
module Issue35566
2597+
function step(acc, x)
2598+
xs, = acc
2599+
y = x > 0.0 ? x : missing
2600+
if y isa eltype(xs)
2601+
ys = push!(xs, y)
2602+
else
2603+
ys = vcat(xs, [y])
2604+
end
2605+
return (ys,)
2606+
end
2607+
2608+
function probe(y)
2609+
if y isa Tuple{Vector{Missing}}
2610+
return Val(:missing)
2611+
else
2612+
return Val(:expected)
2613+
end
2614+
end
2615+
2616+
function _foldl_iter(rf, val::T, iter, state) where {T}
2617+
while true
2618+
ret = iterate(iter, state)
2619+
ret === nothing && break
2620+
x, state = ret
2621+
y = rf(val, x)
2622+
if y isa T
2623+
val = y
2624+
else
2625+
return probe(y)
2626+
end
2627+
end
2628+
return Val(:expected)
2629+
end
2630+
2631+
f() = _foldl_iter(step, (Missing[],), [0.0], 1)
2632+
end
2633+
@test Core.Compiler.typesubtract(Tuple{Union{Int,Char}}, Tuple{Char}) == Tuple{Int}
2634+
@test Base.return_types(Issue35566.f) == [Val{:expected}]
2635+
25952636
# constant prop through keyword arguments
25962637
_unstable_kw(;x=1,y=2) = x == 1 ? 0 : ""
25972638
_use_unstable_kw_1() = _unstable_kw(x = 2)

0 commit comments

Comments
 (0)