Skip to content

Commit 23776a7

Browse files
committed
Revert "Fix broadcast_indices (#22130)"
This reverts commit fb81c34. And adds a test for the bug this causes.
1 parent 5c90d2e commit 23776a7

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

base/broadcast.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ promote_containertype(::Type{T}, ::Type{T}) where {T} = T
4545
## Calculate the broadcast indices of the arguments, or error if incompatible
4646
# array inputs
4747
broadcast_indices() = ()
48-
broadcast_indices(A) = broadcast_indices(containertype(A), A)
48+
broadcast_indices(A) = _broadcast_indices(containertype(A), A)
4949
@inline broadcast_indices(A, B...) = broadcast_shape(broadcast_indices(A), broadcast_indices(B...))
50-
broadcast_indices(::ScalarType, A) = ()
51-
broadcast_indices(::Type{Tuple}, A) = (OneTo(length(A)),)
52-
broadcast_indices(::Type{Array}, A::Ref) = ()
53-
broadcast_indices(::Type{Array}, A) = indices(A)
50+
_broadcast_indices(::Type, A) = error("_containertype definition requires a corresponding _broadcast_indices definition")
51+
_broadcast_indices(::ScalarType, A) = ()
52+
_broadcast_indices(::Type{Tuple}, A) = (OneTo(length(A)),)
53+
_broadcast_indices(::Type{Array}, A::Ref) = ()
54+
_broadcast_indices(::Type{Array}, A) = indices(A)
5455

5556
# shape (i.e., tuple-of-indices) inputs
5657
broadcast_shape(shape::Tuple) = shape

base/sparse/higherorderfns.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ module HigherOrderFns
66
# particularly map[!]/broadcast[!] for SparseVectors and SparseMatrixCSCs at present.
77
import Base: map, map!, broadcast, broadcast!
88
import Base.Broadcast: _containertype, promote_containertype,
9-
broadcast_indices, broadcast_c, broadcast_c!
9+
broadcast_indices, _broadcast_indices,
10+
broadcast_c, broadcast_c!
1011

1112
using Base: front, tail, to_shape
1213
using ..SparseArrays: SparseVector, SparseMatrixCSC, AbstractSparseVector,
@@ -900,7 +901,7 @@ end
900901
# (10) broadcast[!] over combinations of broadcast scalars and sparse vectors/matrices
901902

902903
# broadcast shape promotion for combinations of sparse arrays and other types
903-
broadcast_indices(::Type{AbstractSparseArray}, A) = indices(A)
904+
_broadcast_indices(::Type{AbstractSparseArray}, A) = indices(A)
904905
# broadcast container type promotion for combinations of sparse arrays and other types
905906
_containertype(::Type{<:SparseVecOrMat}) = AbstractSparseArray
906907
# combinations of sparse arrays with broadcast scalars should yield sparse arrays
@@ -984,7 +985,7 @@ struct PromoteToSparse end
984985
# broadcast containertype definitions for structured matrices
985986
StructuredMatrix = Union{Diagonal,Bidiagonal,Tridiagonal,SymTridiagonal}
986987
_containertype(::Type{<:StructuredMatrix}) = PromoteToSparse
987-
broadcast_indices(::Type{PromoteToSparse}, A) = indices(A)
988+
_broadcast_indices(::Type{PromoteToSparse}, A) = indices(A)
988989

989990
# combinations explicitly involving Tuples and PromoteToSparse collections
990991
# divert to the generic AbstractArray broadcast code

test/broadcast.jl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,6 @@ struct Array19745{T,N} <: AbstractArray{T,N}
419419
data::Array{T,N}
420420
end
421421
Base.getindex(A::Array19745, i::Integer...) = A.data[i...]
422-
Base.setindex!(A::Array19745, v::Any, i::Integer...) = setindex!(A.data, v, i...)
423422
Base.size(A::Array19745) = size(A.data)
424423

425424
Base.Broadcast._containertype{T<:Array19745}(::Type{T}) = Array19745
@@ -436,12 +435,8 @@ Base.Broadcast.broadcast_indices(::Type{Array19745}, A::Ref) = ()
436435
getfield19745(x::Array19745) = x.data
437436
getfield19745(x) = x
438437

439-
function Base.Broadcast.broadcast_c(f, ::Type{Array19745}, A, Bs...)
440-
T = Base.Broadcast._broadcast_eltype(f, A, Bs...)
441-
shape = Base.Broadcast.broadcast_indices(A, Bs...)
442-
dest = Array19745(Array{T}(Base.index_lengths(shape...)))
443-
return broadcast!(f, dest, A, Bs...)
444-
end
438+
Base.Broadcast.broadcast_c(f, ::Type{Array19745}, A, Bs...) =
439+
Array19745(Base.Broadcast.broadcast_c(f, Array, getfield19745(A), map(getfield19745, Bs)...))
445440

446441
@testset "broadcasting for custom AbstractArray" begin
447442
a = randn(10)
@@ -520,3 +515,6 @@ let t = (0, 1, 2)
520515
o = 1
521516
@test @inferred(broadcast(+, t, o)) == (1, 2, 3)
522517
end
518+
519+
# issue #22130
520+
@test convert.(Any, [1, 2]) == [1, 2]

0 commit comments

Comments
 (0)