Skip to content

Commit 2b2b4fd

Browse files
mbaumanJeffBezanson
authored andcommitted
Remove tricky array/broadcasting/indexing deprecations (#28458)
* Remove scalar .= deprecation * Remove to_index(::Bool) deprecation * broadcasting now falls back to iteration (fixes #23197, fixes #23746)
1 parent 7b140ee commit 2b2b4fd

File tree

8 files changed

+41
-84
lines changed

8 files changed

+41
-84
lines changed

base/broadcast.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,9 @@ broadcastable(x::Union{Symbol,AbstractString,Function,UndefInitializer,Nothing,R
605605
broadcastable(x::Ptr) = Ref{Ptr}(x) # Cannot use Ref(::Ptr) until ambiguous deprecation goes through
606606
broadcastable(::Type{T}) where {T} = Ref{Type{T}}(T)
607607
broadcastable(x::Union{AbstractArray,Number,Ref,Tuple,Broadcasted}) = x
608-
# In the future, default to collecting arguments. TODO: uncomment once deprecations are removed
609-
# broadcastable(x) = collect(x)
610-
# broadcastable(::Union{AbstractDict, NamedTuple}) = error("intentionally unimplemented to allow development in 1.x")
608+
# Default to collecting iterables — which will error for non-iterables
609+
broadcastable(x) = collect(x)
610+
broadcastable(::Union{AbstractDict, NamedTuple}) = throw(ArgumentError("broadcasting over dictionaries and `NamedTuple`s is reserved"))
611611

612612
## Computation of inferred result type, for empty and concretely inferred cases only
613613
_broadcast_getindex_eltype(bc::Broadcasted) = Base._return_type(bc.f, eltypes(bc.args))

base/deprecated.jl

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -575,33 +575,6 @@ function toc()
575575
return t
576576
end
577577

578-
# A[I...] .= with scalar indices should modify the element at A[I...]
579-
function Broadcast.dotview(A::AbstractArray, args::Number...)
580-
depwarn("the behavior of `A[I...] .= X` with scalar indices will change in the future. Use `A[I...] = X` instead.", :broadcast!)
581-
view(A, args...)
582-
end
583-
Broadcast.dotview(A::AbstractArray{<:AbstractArray}, args::Integer...) = getindex(A, args...)
584-
# Upon removing deprecations, also enable the @testset "scalar .=" in test/broadcast.jl
585-
586-
# indexing with A[true] will throw an argument error in the future
587-
function to_index(i::Bool)
588-
depwarn("indexing with Bool values is deprecated. Convert the index to an integer first with `Int(i)`.", (:getindex, :setindex!, :view))
589-
convert(Int,i)::Int
590-
end
591-
# After deprecation is removed, enable the @testset "indexing by Bool values" in test/arrayops.jl
592-
# Also un-comment the new definition in base/indices.jl
593-
594-
# Broadcast no longer defaults to treating its arguments as scalar (#)
595-
@noinline function Broadcast.broadcastable(x)
596-
depwarn("""
597-
broadcast will default to iterating over its arguments in the future. Wrap arguments of
598-
type `x::$(typeof(x))` with `Ref(x)` to ensure they broadcast as "scalar" elements.
599-
""", (:broadcast, :broadcast!))
600-
return Ref{typeof(x)}(x)
601-
end
602-
@eval Base.Broadcast Base.@deprecate_binding Scalar DefaultArrayStyle{0} false
603-
# After deprecation is removed, enable the fallback broadcastable definitions in base/broadcast.jl
604-
605578
# deprecate BitArray{...}(shape...) constructors to BitArray{...}(undef, shape...) equivalents
606579
@deprecate BitArray{N}(dims::Vararg{Int,N}) where {N} BitArray{N}(undef, dims)
607580
@deprecate BitArray(dims::NTuple{N,Int}) where {N} BitArray(undef, dims...)
@@ -1376,28 +1349,6 @@ function slicedim(A::AbstractVector, d::Integer, i::Number)
13761349
end
13771350
end
13781351

1379-
# PR #26347: Deprecate implicit scalar broadcasting in setindex!
1380-
_axes(::Ref) = ()
1381-
_axes(x) = axes(x)
1382-
setindex_shape_check(X::Base.Iterators.Repeated, I...) = nothing
1383-
function deprecate_scalar_setindex_broadcast_message(v, I...)
1384-
value = (_axes(Base.Broadcast.broadcastable(v)) == () ? "x" : "(x,)")
1385-
"using `A[I...] = x` to implicitly broadcast `x` across many locations is deprecated. Use `A[I...] .= $value` instead."
1386-
end
1387-
deprecate_scalar_setindex_broadcast_message(v, ::Colon, ::Vararg{Colon}) =
1388-
"using `A[:] = x` to implicitly broadcast `x` across many locations is deprecated. Use `fill!(A, x)` instead."
1389-
1390-
function _iterable(v, I...)
1391-
depwarn(deprecate_scalar_setindex_broadcast_message(v, I...), :setindex!)
1392-
Iterators.repeated(v)
1393-
end
1394-
function setindex!(B::BitArray, x, I0::Union{Colon,UnitRange{Int}}, I::Union{Int,UnitRange{Int},Colon}...)
1395-
depwarn(deprecate_scalar_setindex_broadcast_message(x, I0, I...), :setindex!)
1396-
B[I0, I...] .= (x,)
1397-
B
1398-
end
1399-
1400-
14011352
# PR #26283
14021353
@deprecate contains(haystack, needle) occursin(needle, haystack)
14031354
@deprecate contains(s::AbstractString, r::Regex, offset::Integer) occursin(r, s, offset=offset)
@@ -1493,15 +1444,6 @@ end
14931444
depwarn("using similar(f, shape...) to call `f` with axes `shape` is deprecated; call `f` directly and/or add methods such that it supports axes", :similar)
14941445
f(to_shape(dims))
14951446
end
1496-
# Deprecate non-integer/axis arguments to zeros/ones to match fill/trues/falses
1497-
@deprecate zeros(::Type{T}, dims...) where {T} zeros(T, convert(Dims, dims)...)
1498-
@deprecate zeros(dims...) zeros(convert(Dims, dims)...)
1499-
@deprecate zeros(::Type{T}, dims::NTuple{N, Any}) where {T, N} zeros(T, convert(Dims, dims))
1500-
@deprecate zeros(dims::Tuple) zeros(convert(Dims, dims))
1501-
@deprecate ones(::Type{T}, dims...) where {T} ones(T, convert(Dims, dims)...)
1502-
@deprecate ones(dims...) ones(convert(Dims, dims)...)
1503-
@deprecate ones(::Type{T}, dims::NTuple{N, Any}) where {T, N} ones(T, convert(Dims, dims))
1504-
@deprecate ones(dims::Tuple) ones(convert(Dims, dims))
15051447

15061448
# Deprecate varargs size: PR #26862
15071449
@deprecate size(x, d1::Integer, d2::Integer) (size(x, d1), size(x, d2))

base/indices.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,7 @@ indexing behaviors. This must return either an `Int` or an `AbstractArray` of
231231
`Int`s.
232232
"""
233233
to_index(i::Integer) = convert(Int,i)::Int
234-
# TODO: Enable this new definition after the deprecations introduced in 0.7 are removed
235-
# to_index(i::Bool) = throw(ArgumentError("invalid index: $i of type $(typeof(i))"))
234+
to_index(i::Bool) = throw(ArgumentError("invalid index: $i of type $(typeof(i))"))
236235
to_index(I::AbstractArray{Bool}) = LogicalIndex(I)
237236
to_index(I::AbstractArray) = I
238237
to_index(I::AbstractArray{<:Union{AbstractArray, Colon}}) =

base/multidimensional.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,11 +632,10 @@ function _setindex!(l::IndexStyle, A::AbstractArray, x, I::Union{Real, AbstractA
632632
A
633633
end
634634

635-
_iterable(X::AbstractArray, I...) = X
636635
@generated function _unsafe_setindex!(::IndexStyle, A::AbstractArray, x, I::Union{Real,AbstractArray}...)
637636
N = length(I)
638637
quote
639-
x′ = unalias(A, _iterable(x, I...))
638+
x′ = unalias(A, x)
640639
@nexprs $N d->(I_d = unalias(A, I[d]))
641640
idxlens = @ncall $N index_lengths I
642641
@ncall $N setindex_shape_check x′ (d->idxlens[d])

stdlib/LinearAlgebra/test/uniformscaling.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,10 @@ end
256256
@test (I - LL')\[[0], [0], [1]] == (I - LL)'\[[0], [0], [1]] == fill([1], 3)
257257
end
258258

259+
# Ensure broadcasting of I is an error (could be made to work in the future)
260+
@testset "broadcasting of I (#23197)" begin
261+
@test_throws MethodError I .+ 1
262+
@test_throws MethodError I .+ [1 1; 1 1]
263+
end
264+
259265
end # module TestUniformscaling

test/arrayops.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,7 +2388,8 @@ end
23882388
test_zeros(zeros(Int, (2, 3)), Matrix{Int}, (2,3))
23892389

23902390
# #19265"
2391-
@test_throws Any zeros(Float64, [1.]) # TODO: Tighten back up to MethodError once 0.7 deprecations are removed
2391+
@test_throws MethodError zeros(Float64, [1.])
2392+
@test_throws MethodError ones(Float64, [0, 0])
23922393
end
23932394

23942395
# issue #11053
@@ -2431,11 +2432,10 @@ end
24312432
@test_throws BoundsError checkbounds(zeros(2,3,0), 2, 3)
24322433
end
24332434

2434-
# TODO: Enable this testset after the deprecations introduced in 0.7 are removed
2435-
# @testset "indexing by Bool values" begin
2436-
# @test_throws ArgumentError zeros(2)[false]
2437-
# @test_throws ArgumentError zeros(2)[true]
2438-
# end
2435+
@testset "indexing by Bool values" begin
2436+
@test_throws ArgumentError zeros(Float64, 2)[false]
2437+
@test_throws ArgumentError zeros(Float64, 2)[true]
2438+
end
24392439

24402440
@testset "issue 24707" begin
24412441
@test eltype(Vector{Tuple{V}} where V<:Integer) >: Tuple{Integer}

test/broadcast.jl

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,18 @@ end
591591
@test broadcast(==, 1, AbstractArray) == false
592592
end
593593

594+
@testset "broadcasting falls back to iteration (issues #26421, #19577, #23746)" begin
595+
@test_throws ArgumentError broadcast(identity, Dict(1=>2))
596+
@test_throws ArgumentError broadcast(identity, (a=1, b=2))
597+
@test_throws ArgumentError length.(Dict(1 => BitSet(1:2), 2 => BitSet(1:3)))
598+
@test_throws MethodError broadcast(identity, Base)
599+
600+
@test broadcast(identity, Iterators.filter(iseven, 1:10)) == 2:2:10
601+
d = Dict([1,2] => 1.1, [3,2] => 0.1)
602+
@test length.(keys(d)) == [2,2]
603+
@test Set(exp.(Set([1,2,3]))) == Set(exp.([1,2,3]))
604+
end
605+
594606
# Test that broadcasting identity where the input and output Array shapes do not match
595607
# yields the correct result, not merely a partial copy. See pull request #19895 for discussion.
596608
let N = 5
@@ -651,18 +663,17 @@ end
651663
@test_throws DimensionMismatch (1, 2) .+ (1, 2, 3)
652664
end
653665

654-
# TODO: Enable after deprecations introduced in 0.7 are removed.
655-
# @testset "scalar .=" begin
656-
# A = [[1,2,3],4:5,6]
657-
# A[1] .= 0
658-
# @test A[1] == [0,0,0]
659-
# @test_throws ErrorException A[2] .= 0
660-
# @test_throws MethodError A[3] .= 0
661-
# A = [[1,2,3],4:5]
662-
# A[1] .= 0
663-
# @test A[1] == [0,0,0]
664-
# @test_throws ErrorException A[2] .= 0
665-
# end
666+
@testset "scalar .=" begin
667+
A = [[1,2,3],4:5,6]
668+
A[1] .= 0
669+
@test A[1] == [0,0,0]
670+
@test_throws ErrorException A[2] .= 0
671+
@test_throws MethodError A[3] .= 0
672+
A = [[1,2,3],4:5]
673+
A[1] .= 0
674+
@test A[1] == [0,0,0]
675+
@test_throws ErrorException A[2] .= 0
676+
end
666677

667678
# Issue #22180
668679
@test convert.(Any, [1, 2]) == [1, 2]

test/compiler/compiler.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ f5575() = zeros5575(Type[Float64][1], 1)
571571
@test Base.return_types(f5575, ())[1] == Vector
572572

573573
g5575() = zeros(Type[Float64][1], 1)
574-
@test_broken Base.return_types(g5575, ())[1] == Vector # This should be fixed by removing deprecations
574+
@test Base.return_types(g5575, ())[1] == Vector
575575

576576

577577
# make sure Tuple{unknown} handles the possibility that `unknown` is a Vararg

0 commit comments

Comments
 (0)