Skip to content

Commit d3964b6

Browse files
authored
broadcast: align ndims implementation with intent behind code (#56999)
The `N<:Integer` constraint was nonsensical, given that `(N === Any) || (N isa Int)`. N5N3 noticed this back in 2022: #44061 (comment) Follow up on #44061. Also xref #45477.
1 parent 6ac351a commit d3964b6

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

base/broadcast.jl

+7-2
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,14 @@ Base.@propagate_inbounds function Base.iterate(bc::Broadcasted, s)
280280
end
281281

282282
Base.IteratorSize(::Type{T}) where {T<:Broadcasted} = Base.HasShape{ndims(T)}()
283-
Base.ndims(BC::Type{<:Broadcasted{<:Any,Nothing}}) = _maxndims(fieldtype(BC, :args))
284-
Base.ndims(::Type{<:Broadcasted{<:AbstractArrayStyle{N},Nothing}}) where {N<:Integer} = N
283+
Base.ndims(BC::Type{<:Broadcasted{<:Any,Nothing}}) = _maxndims_broadcasted(BC)
284+
# the `AbstractArrayStyle` type parameter is required to be either equal to `Any` or be an `Int` value
285+
Base.ndims(BC::Type{<:Broadcasted{<:AbstractArrayStyle{Any},Nothing}}) = _maxndims_broadcasted(BC)
286+
Base.ndims(::Type{<:Broadcasted{<:AbstractArrayStyle{N},Nothing}}) where {N} = N::Int
285287

288+
function _maxndims_broadcasted(BC::Type{<:Broadcasted})
289+
_maxndims(fieldtype(BC, :args))
290+
end
286291
_maxndims(::Type{T}) where {T<:Tuple} = reduce(max, ntuple(n -> (F = fieldtype(T, n); F <: Tuple ? 1 : ndims(F)), Base._counttuple(T)))
287292
_maxndims(::Type{<:Tuple{T}}) where {T} = T <: Tuple ? 1 : ndims(T)
288293
function _maxndims(::Type{<:Tuple{T, S}}) where {T, S}

test/broadcast.jl

+2
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,8 @@ let
930930

931931
@test @inferred(Base.IteratorSize(Broadcast.broadcasted(+, (1,2,3), a1, zeros(3,3,3)))) === Base.HasShape{3}()
932932

933+
@test @inferred(Base.IteratorSize(Base.broadcasted(randn))) === Base.HasShape{0}()
934+
933935
# inference on nested
934936
bc = Base.broadcasted(+, AD1(randn(3)), AD1(randn(3)))
935937
bc_nest = Base.broadcasted(+, bc , bc)

0 commit comments

Comments
 (0)