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

Faulty assumptions in _might_return_reduced #537

Closed
MasonProtter opened this issue Oct 5, 2022 · 0 comments · Fixed by #536
Closed

Faulty assumptions in _might_return_reduced #537

MasonProtter opened this issue Oct 5, 2022 · 0 comments · Fixed by #536

Comments

@MasonProtter
Copy link
Member

The code in

# The output of `foldxt` is correct regardless of the value of
# `stoppable`. Thus, we can use `return_type` here purely for
# optimization.
_might_return_reduced(rf, init, coll) =
Base.typeintersect(
Core.Compiler.return_type(
_reduce_dummy, # simulate the output type of `_reduce`
typeof((rf, init, coll)),
),
Reduced,
) !== Union{}
_reduce_dummy(rf, init, coll) =
__reduce_dummy(rf, init, SizedReducible(maybe_collect(coll), 1))
function __reduce_dummy(rf, init, reducible)
if issmall(reducible)
return _reduce_basecase(rf, init, reducible)
else
left, right = halve(reducible)
a = _reduce_dummy(rf, init, left)
b = _reduce_dummy(rf, init, right)
a isa Reduced && return a
b isa Reduced && return combine_right_reduced(rf, a, b)
return combine(rf, a, b)
end
end
relies on Julia's type inference algorithm not realizing that the code always errors. 1.9's inference is now smart enough that this no longer works:

1.8.0:

julia> using Transducers

julia> Transducers._might_return_reduced(reducingfunction(ReduceIf(ismissing), +), 0, (0, missing))
true

julia> Core.Compiler.return_type(Transducers._reduce_dummy,  # simulate the output type of `_reduce`
                   typeof((reducingfunction(ReduceIf(ismissing), +), 0, (0, missing))),
               )
Reduced{Missing}

julia> Transducers._reduce_dummy(reducingfunction(ReduceIf(ismissing), +), 0, (0, missing))
ERROR: MethodError: no method matching halve(::Transducers.SizedReducible{Tuple{Int64, Missing}, Int64})
Closest candidates are:
...

Julia Master realizes this will error and gives us a Union{}

julia> using Transducers

julia> Core.Compiler.return_type(Transducers._reduce_dummy,  # simulate the output type of `_reduce`
                   typeof((reducingfunction(ReduceIf(ismissing), +), 0, (0, missing))),
               )
Union{}

which makes it so that _might_return_reduced gives false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant