From 256814a350abb01b9a1f76016d888808fb256404 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Thu, 25 Apr 2019 17:02:23 -0700 Subject: [PATCH] Remove definitions for `reduce` over `&`/`|` over empty collections of unknown eltype. Fixes #31635/#31837. --- NEWS.md | 1 + base/reduce.jl | 10 ++++------ test/reduce.jl | 2 +- test/syntax.jl | 2 +- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/NEWS.md b/NEWS.md index e5d3619986d087..9c24f581104e63 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 ----------------------- diff --git a/base/reduce.jl b/base/reduce.jl index 4113f9a7368434..230d9d2c8b0dea 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -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) + # handling of single-element iterators """ diff --git a/test/reduce.jl b/test/reduce.jl index cc4ce66d2d7d43..2daa9903576ea6 100644 --- a/test/reduce.jl +++ b/test/reduce.jl @@ -489,7 +489,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 diff --git a/test/syntax.jl b/test/syntax.jl index 85951185f4a57d..2f2291197e8dcf 100644 --- a/test/syntax.jl +++ b/test/syntax.jl @@ -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])