Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve typesubtract for tuples #35600

Merged
merged 3 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions base/compiler/typeutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ function typesubtract(@nospecialize(a), @nospecialize(b))
if isa(a, Union)
return Union{typesubtract(a.a, b),
typesubtract(a.b, b)}
elseif a isa DataType && b isa DataType && a.name === b.name === Tuple.name &&
length(a.types) == length(b.types)
tkf marked this conversation as resolved.
Show resolved Hide resolved
ta = switchtupleunion(a)
if length(ta) > 1
return typesubtract(Union{ta...}, b)
end
tkf marked this conversation as resolved.
Show resolved Hide resolved
end
return a # TODO: improve this bound?
end
Expand Down
41 changes: 41 additions & 0 deletions test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2545,3 +2545,44 @@ end
@test only(Base.code_typed(pickvarnames, (Vector{Any},), optimize=false))[2] == Tuple{Vararg{Union{Symbol, Tuple{Vararg{Union{Symbol, Tuple}}}}}}

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

# issue 35566
module Issue35566
function step(acc, x)
xs, = acc
y = x > 0.0 ? x : missing
if y isa eltype(xs)
ys = push!(xs, y)
else
ys = vcat(xs, [y])
end
return (ys,)
end

function probe(y)
if y isa Tuple{Vector{Missing}}
return Val(:missing)
else
return Val(:expected)
end
end

function _foldl_iter(rf, val::T, iter, state) where {T}
while true
ret = iterate(iter, state)
ret === nothing && break
x, state = ret
y = rf(val, x)
if y isa T
val = y
else
return probe(y)
end
end
return Val(:expected)
end

f() = _foldl_iter(step, (Missing[],), [0.0], 1)
end
@test Core.Compiler.typesubtract(Tuple{Union{Int,Char}}, Tuple{Char}) == Tuple{Int}
@test Base.return_types(Issue35566.f) == [Val{:expected}]