Skip to content

Commit

Permalink
Some broadcast fixes (JuliaLang#18200)
Browse files Browse the repository at this point in the history
* Fix JuliaLang#18176 (broadcast over mixtures of arrays and numeric scalars)

* Fix JuliaLang#17984 (broadcast behavior over empty arrays)
  • Loading branch information
pabloferz authored and mfasi committed Sep 5, 2016
1 parent a142b99 commit 1d792f7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
3 changes: 2 additions & 1 deletion base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1656,12 +1656,13 @@ function mapslices(f, A::AbstractArray, dims::AbstractVector)
end

# These are needed because map(eltype, As) is not inferrable
promote_eltype_op(::Any) = (@_pure_meta; Bottom)
promote_eltype_op(::Any) = (@_pure_meta; Any)
promote_eltype_op(op, A) = (@_pure_meta; promote_op(op, eltype(A)))
promote_eltype_op{T}(op, ::AbstractArray{T}) = (@_pure_meta; promote_op(op, T))
promote_eltype_op{T}(op, ::AbstractArray{T}, A) = (@_pure_meta; promote_op(op, T, eltype(A)))
promote_eltype_op{T}(op, A, ::AbstractArray{T}) = (@_pure_meta; promote_op(op, eltype(A), T))
promote_eltype_op{R,S}(op, ::AbstractArray{R}, ::AbstractArray{S}) = (@_pure_meta; promote_op(op, R, S))
promote_eltype_op(op, A, B) = (@_pure_meta; promote_op(op, eltype(A), eltype(B)))
promote_eltype_op(op, A, B, C, D...) = (@_pure_meta; promote_eltype_op(op, promote_eltype_op(op, A, B), C, D...))

## 1 argument
Expand Down
2 changes: 1 addition & 1 deletion base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ function broadcast_t(f, ::Type{Any}, As...)
shape = broadcast_shape(As...)
iter = CartesianRange(shape)
if isempty(iter)
return similar(Array{Union{}}, shape)
return similar(Array{Any}, shape)
end
nargs = length(As)
keeps, Idefaults = map_newindexer(shape, As)
Expand Down
9 changes: 9 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -746,3 +746,12 @@ end
@test ndims(Diagonal(rand(1:5,5))) == 2
@test ndims(Diagonal{Float64}) == 2
@test Base.elsize(Diagonal(rand(1:5,5))) == sizeof(Int)

# Issue #17811
let A17811 = Integer[]
I = [abs(x) for x in A17811]
@test isa(I, Array{Any,1})
push!(I, 1)
@test I == Any[1]
@test isa(map(abs, A17811), Array{Any,1})
end
12 changes: 11 additions & 1 deletion test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,19 @@ end
let f17314 = x -> x < 0 ? false : x
@test eltype(broadcast(f17314, 1:3)) === Int
@test eltype(broadcast(f17314, -1:1)) === Integer
@test eltype(broadcast(f17314, Int[])) === Union{}
@test eltype(broadcast(f17314, Int[])) === Any
end
let io = IOBuffer()
broadcast(x->print(io,x), 1:5) # broadcast with side effects
@test takebuf_array(io) == [0x31,0x32,0x33,0x34,0x35]
end

# Issue 18176
let f18176(a, b, c) = a + b + c
@test f18176.(1.0:2, 3, 4) == f18176.(3.0, 1.0:2, 4.0) == broadcast(f18176, 3, 4, 1.0:2)
end

# Issue #17984
let A17984 = []
@test isa(abs.(A17984), Array{Any,1})
end
8 changes: 0 additions & 8 deletions test/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,6 @@ let f(x) = (x===nothing) ? 1 : 1.0
@test Base.return_types(f, (Void,)) == Any[Int]
end

# Issue #17811
let I = Integer[]
I = abs(I)
@test typeof(I) == Array{Any,1}
push!(I, 1)
@test I == Any[1]
end

# issue #16530
type Foo16530a{dim}
c::Vector{NTuple{dim, Float64}}
Expand Down

0 comments on commit 1d792f7

Please sign in to comment.