Skip to content

Commit

Permalink
fix #33020, check axes for broadcasted assignment from tuples (#33080)
Browse files Browse the repository at this point in the history
We avoid computing axes for tuples -- which is a valuable optimization -- but when we explicitly construct a tuple broadcast with axes pre-set (for, e.g., broadcasted assignment), we need to check that those axes are compatible with the ones inside the broadcasted expression before accepting them.
  • Loading branch information
mbauman authored Aug 27, 2019
1 parent 9725fb4 commit b5c4e63
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
9 changes: 7 additions & 2 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,13 @@ of the `Broadcasted` object empty (populated with [`nothing`](@ref)).
end
return Broadcasted{Style}(bc.f, bc.args, axes)
end
instantiate(bc::Broadcasted{<:Union{AbstractArrayStyle{0}, Style{Tuple}}}) = bc

instantiate(bc::Broadcasted{<:AbstractArrayStyle{0}}) = bc
# Tuples don't need axes, but when they have axes (for .= assignment), we need to check them (#33020)
instantiate(bc::Broadcasted{Style{Tuple}, Nothing}) = bc
function instantiate(bc::Broadcasted{Style{Tuple}})
check_broadcast_axes(bc.axes, bc.args...)
return bc
end
## Flattening

"""
Expand Down
19 changes: 19 additions & 0 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,25 @@ end
@test_throws DimensionMismatch (1, 2) .+ (1, 2, 3)
end

@testset "broadcasted assignment from tuples and tuple styles (#33020)" begin
a = zeros(3)
@test_throws DimensionMismatch a .= (1,2)
@test_throws DimensionMismatch a .= sqrt.((1,2))
a .= (1,)
@test all(==(1), a)
a .= sqrt.((2,))
@test all(==(2), a)
a = zeros(3, 2)
@test_throws DimensionMismatch a .= (1,2)
@test_throws DimensionMismatch a .= sqrt.((1,2))
a .= (1,)
@test all(==(1), a)
a .= sqrt.((2,))
@test all(==(2), a)
a .= (1,2,3)
@test a == [1 1; 2 2; 3 3]
end

@testset "scalar .=" begin
A = [[1,2,3],4:5,6]
A[1] .= 0
Expand Down

0 comments on commit b5c4e63

Please sign in to comment.