Skip to content

Commit cf0c3e0

Browse files
authored
Improve typesubtract for tuples (#35600)
Implements this: ``` julia> Core.Compiler.typesubtract(Tuple{Union{Int, Char}}, Tuple{Char}) Tuple{Int64} ```
1 parent 6248170 commit cf0c3e0

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
@@ -2545,3 +2545,44 @@ end
25452545
@test only(Base.code_typed(pickvarnames, (Vector{Any},), optimize=false))[2] == Tuple{Vararg{Union{Symbol, Tuple{Vararg{Union{Symbol, Tuple}}}}}}
25462546

25472547
@test map(>:, [Int], [Int]) == [true]
2548+
2549+
# issue 35566
2550+
module Issue35566
2551+
function step(acc, x)
2552+
xs, = acc
2553+
y = x > 0.0 ? x : missing
2554+
if y isa eltype(xs)
2555+
ys = push!(xs, y)
2556+
else
2557+
ys = vcat(xs, [y])
2558+
end
2559+
return (ys,)
2560+
end
2561+
2562+
function probe(y)
2563+
if y isa Tuple{Vector{Missing}}
2564+
return Val(:missing)
2565+
else
2566+
return Val(:expected)
2567+
end
2568+
end
2569+
2570+
function _foldl_iter(rf, val::T, iter, state) where {T}
2571+
while true
2572+
ret = iterate(iter, state)
2573+
ret === nothing && break
2574+
x, state = ret
2575+
y = rf(val, x)
2576+
if y isa T
2577+
val = y
2578+
else
2579+
return probe(y)
2580+
end
2581+
end
2582+
return Val(:expected)
2583+
end
2584+
2585+
f() = _foldl_iter(step, (Missing[],), [0.0], 1)
2586+
end
2587+
@test Core.Compiler.typesubtract(Tuple{Union{Int,Char}}, Tuple{Char}) == Tuple{Int}
2588+
@test Base.return_types(Issue35566.f) == [Val{:expected}]

0 commit comments

Comments
 (0)