Skip to content

Commit

Permalink
Deprecate array of arrays special casing in .= (#24095)
Browse files Browse the repository at this point in the history
This commit deprecates the special-casing for arrays of arrays when using `A[I...] .=` with entirely scalar indices.  Once this deprecation goes away, `A[1] .= 0` will *always* modify the element that is stored at `A[1]`.  The outer array `A` will not be modified, regardless of its element type.  I have chosen to use the same internal machinery from `@views` for this; when the deprecation is removed the syntactic transform will behave like `broadcast!(identity, @views A[I...], X)`.

fixup!
  • Loading branch information
mbauman authored Oct 20, 2017
1 parent 8d02f16 commit 182d843
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,9 @@ This section lists changes that do not have deprecation warnings.
functions yielding a boolean result. If you want `Array{Bool}`, use
`broadcast!` or `.=` ([#17623]).
* Broadcast `A[I...] .= X` with entirely scalar indices `I` is deprecated as
its behavior will change in the future. Use `A[I...] = X` instead.
* Operations like `.+` and `.*` on `Range` objects are now generic
`broadcast` calls (see [above](#language-changes)) and produce an `Array`.
If you want a `Range` result, use `+` and `*`, etcetera ([#17623]).
Expand Down
5 changes: 1 addition & 4 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -534,10 +534,7 @@ end
# explicit calls to view. (All of this can go away if slices
# are changed to generate views by default.)

Base.@propagate_inbounds dotview(args...) = getindex(args...)
Base.@propagate_inbounds dotview(A::AbstractArray, args...) = view(A, args...)
Base.@propagate_inbounds dotview(A::AbstractArray{<:AbstractArray}, args::Integer...) = getindex(A, args...)

Base.@propagate_inbounds dotview(args...) = Base.maybeview(args...)

############################################################
# The parser turns @. into a call to the __dot__ macro,
Expand Down
8 changes: 8 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1815,6 +1815,14 @@ function toc()
return t
end

# A[I...] .= with scalar indices should modify the element at A[I...]
function Broadcast.dotview(A::AbstractArray, args::Number...)
depwarn("the behavior of `A[I...] .= X` with scalar indices will change in the future. Use `A[I...] = X` instead.", :broadcast!)
view(A, args...)
end
Broadcast.dotview(A::AbstractArray{<:AbstractArray}, args::Integer...) = getindex(A, args...)
# Upon removing deprecations, also enable the @testset "scalar .=" in test/broadcast.jl

# PR #23816: deprecation of gradient
export gradient
@eval Base.LinAlg begin
Expand Down
13 changes: 13 additions & 0 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -525,3 +525,16 @@ let t = (0, 1, 2)
o = 1
@test @inferred(broadcast(+, t, o)) == (1, 2, 3)
end

# TODO: Enable after deprecations introduced in 0.7 are removed.
# @testset "scalar .=" begin
# A = [[1,2,3],4:5,6]
# A[1] .= 0
# @test A[1] == [0,0,0]
# @test_throws ErrorException A[2] .= 0
# @test_throws MethodError A[3] .= 0
# A = [[1,2,3],4:5]
# A[1] .= 0
# @test A[1] == [0,0,0]
# @test_throws ErrorException A[2] .= 0
# end
2 changes: 1 addition & 1 deletion test/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ end
@test x == [5,6,35,4]
x[Y[2:3]] .= 7:8
@test x == [5,8,7,4]
@. x[(3,)..., ()...] += 3 # @. should convert to .+=, test compatibility with @views
x[(3,)..., ()...] += 3
@test x == [5,8,10,4]
i = Int[]
# test that lhs expressions in update operations are evaluated only once:
Expand Down

0 comments on commit 182d843

Please sign in to comment.