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

Remove definitions for reduce over &/| for unknown eltype collections #31839

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ New language features
Language changes
----------------

* `reduce`/`mapreduce` using `&` and `|` as an operator over empty collections of unknown element type will throw an error instead of returning `true`/`false` ([#31635], [#31837]).

Multi-threading changes
-----------------------
Expand Down
10 changes: 4 additions & 6 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,12 @@ mapreduce_empty(f, op, T) = _empty_reduce_error()
mapreduce_empty(::typeof(identity), op, T) = reduce_empty(op, T)
mapreduce_empty(::typeof(abs), op, T) = abs(reduce_empty(op, T))
mapreduce_empty(::typeof(abs2), op, T) = abs2(reduce_empty(op, T))

mapreduce_empty(f::typeof(abs), ::typeof(max), T) = abs(zero(T))
mapreduce_empty(f::typeof(abs2), ::typeof(max), T) = abs2(zero(T))
mapreduce_empty(::typeof(abs), ::typeof(max), T) = abs(zero(T))
mapreduce_empty(::typeof(abs2), ::typeof(max), T) = abs2(zero(T))

mapreduce_empty_iter(f, op, itr, ::HasEltype) = mapreduce_empty(f, op, eltype(itr))
mapreduce_empty_iter(f, op::typeof(&), itr, ::EltypeUnknown) = true
mapreduce_empty_iter(f, op::typeof(|), itr, ::EltypeUnknown) = false
mapreduce_empty_iter(f, op, itr, ::EltypeUnknown) = _empty_reduce_error()
mapreduce_empty_iter(f, op, itr, ::EltypeUnknown) = mapreduce_empty(f, op, Any)
simonbyrne marked this conversation as resolved.
Show resolved Hide resolved


# handling of single-element iterators
"""
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Statistics/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ end
@test mean(skipmissing(Int[])) isa Float64
@test_throws MethodError mean([])
@test_throws MethodError mean(skipmissing([]))
@test_throws ArgumentError mean((1 for i in 2:1))
@test_throws MethodError mean((1 for i in 2:1))

# Check that small types are accumulated using wider type
for T in (Int8, UInt8)
Expand Down
8 changes: 7 additions & 1 deletion test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ A = circshift(reshape(1:24,2,3,4), (0,1,1))
@test @inferred all(x->x>0, [4]) == true
@test @inferred all(x->x>0, [-3, 4, 5]) == false

# issues #31635/#31837
@test_throws ArgumentError reduce(&,[])
@test reduce(&,Bool[]) == true
@test_throws ArgumentError reduce(|,[])
@test reduce(|,Bool[]) == false

@test reduce((a, b) -> a .| b, fill(trues(5), 24)) == trues(5)
@test reduce((a, b) -> a .| b, fill(falses(5), 24)) == falses(5)
@test reduce((a, b) -> a .& b, fill(trues(5), 24)) == trues(5)
Expand Down Expand Up @@ -489,7 +495,7 @@ end
# issue #18695
test18695(r) = sum( t^2 for t in r )
@test @inferred(test18695([1.0,2.0,3.0,4.0])) == 30.0
@test_throws ArgumentError test18695(Any[])
@test_throws MethodError test18695(Any[])

# issue #21107
@test foldr(-,2:2) == 2
Expand Down
2 changes: 1 addition & 1 deletion test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ call2(f,x,y) = f(x,y)
@test (call2(42,1) do x,y; x+y+1 end) == 44

# definitions using comparison syntax
let a⊂b = reduce(&, x ∈ b for x in a) && length(b)>length(a)
let a⊂b = all(x ∈ b for x in a) && length(b)>length(a)
@test [1,2] ⊂ [1,2,3,4]
@test !([1,2] ⊂ [1,3,4])
@test !([1,2] ⊂ [1,2])
Expand Down