From ed4fc24d13bd775ec6000f03f670f12fbcb2bed5 Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Thu, 1 Jun 2017 17:59:55 -0400 Subject: [PATCH] Revert "Fix broadcast_indices (#22130)" This reverts commit fb81c34af8cda41c1f4294182ce817135e064b33. And adds a test for the bug this causes. --- base/broadcast.jl | 11 ++++++----- base/sparse/higherorderfns.jl | 7 ++++--- test/broadcast.jl | 7 +++++-- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/base/broadcast.jl b/base/broadcast.jl index b4fd9126727c4..3d0381aabbfa0 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -45,12 +45,13 @@ promote_containertype(::Type{T}, ::Type{T}) where {T} = T ## Calculate the broadcast indices of the arguments, or error if incompatible # array inputs broadcast_indices() = () -broadcast_indices(A) = broadcast_indices(containertype(A), A) +broadcast_indices(A) = _broadcast_indices(containertype(A), A) @inline broadcast_indices(A, B...) = broadcast_shape(broadcast_indices(A), broadcast_indices(B...)) -broadcast_indices(::ScalarType, A) = () -broadcast_indices(::Type{Tuple}, A) = (OneTo(length(A)),) -broadcast_indices(::Type{Array}, A::Ref) = () -broadcast_indices(::Type{Array}, A) = indices(A) +_broadcast_indices(::Type, A) = error("_containertype definition requires a corresponding _broadcast_indices definition") +_broadcast_indices(::ScalarType, A) = () +_broadcast_indices(::Type{Tuple}, A) = (OneTo(length(A)),) +_broadcast_indices(::Type{Array}, A::Ref) = () +_broadcast_indices(::Type{Array}, A) = indices(A) # shape (i.e., tuple-of-indices) inputs broadcast_shape(shape::Tuple) = shape diff --git a/base/sparse/higherorderfns.jl b/base/sparse/higherorderfns.jl index a51a606b2f13e..87c83069e3299 100644 --- a/base/sparse/higherorderfns.jl +++ b/base/sparse/higherorderfns.jl @@ -6,7 +6,8 @@ module HigherOrderFns # particularly map[!]/broadcast[!] for SparseVectors and SparseMatrixCSCs at present. import Base: map, map!, broadcast, broadcast! import Base.Broadcast: _containertype, promote_containertype, - broadcast_indices, broadcast_c, broadcast_c! + broadcast_indices, _broadcast_indices, + broadcast_c, broadcast_c! using Base: front, tail, to_shape using ..SparseArrays: SparseVector, SparseMatrixCSC, AbstractSparseVector, @@ -900,7 +901,7 @@ end # (10) broadcast[!] over combinations of broadcast scalars and sparse vectors/matrices # broadcast shape promotion for combinations of sparse arrays and other types -broadcast_indices(::Type{AbstractSparseArray}, A) = indices(A) +_broadcast_indices(::Type{AbstractSparseArray}, A) = indices(A) # broadcast container type promotion for combinations of sparse arrays and other types _containertype(::Type{<:SparseVecOrMat}) = AbstractSparseArray # combinations of sparse arrays with broadcast scalars should yield sparse arrays @@ -984,7 +985,7 @@ struct PromoteToSparse end # broadcast containertype definitions for structured matrices StructuredMatrix = Union{Diagonal,Bidiagonal,Tridiagonal,SymTridiagonal} _containertype(::Type{<:StructuredMatrix}) = PromoteToSparse -broadcast_indices(::Type{PromoteToSparse}, A) = indices(A) +_broadcast_indices(::Type{PromoteToSparse}, A) = indices(A) # combinations explicitly involving Tuples and PromoteToSparse collections # divert to the generic AbstractArray broadcast code diff --git a/test/broadcast.jl b/test/broadcast.jl index 4368462f4e7b5..9f3a53916d2ca 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -430,8 +430,8 @@ Base.Broadcast.promote_containertype(::Type{Array19745}, ct) = A Base.Broadcast.promote_containertype(::Type{Array}, ::Type{Array19745}) = Array19745 Base.Broadcast.promote_containertype(ct, ::Type{Array19745}) = Array19745 -Base.Broadcast.broadcast_indices(::Type{Array19745}, A) = indices(A) -Base.Broadcast.broadcast_indices(::Type{Array19745}, A::Ref) = () +Base.Broadcast._broadcast_indices(::Type{Array19745}, A) = indices(A) +Base.Broadcast._broadcast_indices(::Type{Array19745}, A::Ref) = () getfield19745(x::Array19745) = x.data getfield19745(x) = x @@ -520,3 +520,6 @@ let t = (0, 1, 2) o = 1 @test @inferred(broadcast(+, t, o)) == (1, 2, 3) end + +# issue #22130 +@test convert.(Any, [1, 2]) == [1, 2]