Skip to content

Commit

Permalink
Remove final uses of Inference.return_type
Browse files Browse the repository at this point in the history
With this approach based on the eltype of tuple of computed elements, we
get the correct dynamic result independently of whether inference can
infer the return type.

This fixes some of the residual issues from #198.
  • Loading branch information
c42f committed Nov 3, 2017
1 parent 400fd9c commit 4231a73
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
6 changes: 2 additions & 4 deletions src/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,10 @@ end
end
end

eltype_exprs = [t <: AbstractArray ? :($(eltype(t))) : :($t) for t a]
newtype_expr = :(Core.Inference.return_type(f, Tuple{$(eltype_exprs...)}))

return quote
@_inline_meta
@inbounds return similar_type($first_staticarray, $newtype_expr, Size($newsize))(tuple($(exprs...)))
elements = tuple($(exprs...))
@inbounds return similar_type($first_staticarray, eltype(elements), Size($newsize))(elements)
end
end

Expand Down
10 changes: 4 additions & 6 deletions src/mapreduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ end
tmp = [:(a[$j][$i]) for j 1:length(a)]
exprs[i] = :(f($(tmp...)))
end
eltypes = [eltype(a[j]) for j 1:length(a)] # presumably, `eltype` is "hyperpure"?
newT = :(Core.Inference.return_type(f, Tuple{$(eltypes...)}))
return quote
@_inline_meta
@inbounds return similar_type(typeof(_first(a...)), $newT, Size(S))(tuple($(exprs...)))
elements = tuple($(exprs...))
@inbounds return similar_type(typeof(_first(a...)), eltype(elements), Size(S))(elements)
end
end

Expand Down Expand Up @@ -110,8 +109,6 @@ end
@generated function _mapreducedim(f, op, ::Size{S}, a::StaticArray, ::Type{Val{D}}) where {S,D}
N = length(S)
Snew = ([n==D ? 1 : S[n] for n = 1:N]...)
T0 = eltype(a)
T = :((T1 = Core.Inference.return_type(f, Tuple{$T0}); Core.Inference.return_type(op, Tuple{T1,T1})))

exprs = Array{Expr}(Snew)
itr = [1:n for n Snew]
Expand All @@ -128,7 +125,8 @@ end

return quote
@_inline_meta
@inbounds return similar_type(a, $T, Size($Snew))(tuple($(exprs...)))
elements = tuple($(exprs...))
@inbounds return similar_type(a, eltype(elements), Size($Snew))(elements)
end
end

Expand Down
24 changes: 12 additions & 12 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,22 @@ end
@testset "eltype after broadcast" begin
# test cases issue #198
let a = SVector{4, Number}(2, 2.0, 4//2, 2+0im)
@test_broken eltype(a + 2) == Number
@test_broken eltype(a - 2) == Number
@test_broken eltype(a * 2) == Number
@test_broken eltype(a / 2) == Number
@test eltype(a + 2) == Number
@test eltype(a - 2) == Number
@test eltype(a * 2) == Number
@test eltype(a / 2) == Number
end
let a = SVector{3, Real}(2, 2.0, 4//2)
@test_broken eltype(a + 2) == Real
@test_broken eltype(a - 2) == Real
@test_broken eltype(a * 2) == Real
@test_broken eltype(a / 2) == Real
@test eltype(a + 2) == Real
@test eltype(a - 2) == Real
@test eltype(a * 2) == Real
@test eltype(a / 2) == Real
end
let a = SVector{3, Real}(2, 2.0, 4//2)
@test_broken eltype(a + 2.0) == Float64
@test_broken eltype(a - 2.0) == Float64
@test_broken eltype(a * 2.0) == Float64
@test_broken eltype(a / 2.0) == Float64
@test eltype(a + 2.0) == Float64
@test eltype(a - 2.0) == Float64
@test eltype(a * 2.0) == Float64
@test eltype(a / 2.0) == Float64
end
let a = broadcast(Float32, SVector(3, 4, 5))
@test eltype(a) == Float32
Expand Down

0 comments on commit 4231a73

Please sign in to comment.