From 8ea7ffcd23be53d7cd7816edef962eb908904440 Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Thu, 31 Aug 2023 19:03:01 +0000 Subject: [PATCH] broadcast: use recursion rather than ntuple to map over a tuple Inference seems to have trouble with the anonymous function version, so go back to the recursive version. Fixes #51129 Probably also fixes #50859 --- base/broadcast.jl | 4 ++-- test/broadcast.jl | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/base/broadcast.jl b/base/broadcast.jl index fd330a7f2cb67..43044f9b7d6ed 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -714,8 +714,8 @@ _broadcast_getindex_eltype(A) = eltype(A) # Tuple, Array, etc. eltypes(::Tuple{}) = Tuple{} eltypes(t::Tuple{Any}) = Iterators.TupleOrBottom(_broadcast_getindex_eltype(t[1])) eltypes(t::Tuple{Any,Any}) = Iterators.TupleOrBottom(_broadcast_getindex_eltype(t[1]), _broadcast_getindex_eltype(t[2])) -# eltypes(t::Tuple) = (TT = eltypes(tail(t)); TT === Union{} ? Union{} : Iterators.TupleOrBottom(_broadcast_getindex_eltype(t[1]), TT.parameters...)) -eltypes(t::Tuple) = Iterators.TupleOrBottom(ntuple(i -> _broadcast_getindex_eltype(t[i]), Val(length(t)))...) +eltypes(t::Tuple) = (TT = eltypes(tail(t)); TT === Union{} ? Union{} : Iterators.TupleOrBottom(_broadcast_getindex_eltype(t[1]), TT.parameters...)) +# eltypes(t::Tuple) = Iterators.TupleOrBottom(ntuple(i -> _broadcast_getindex_eltype(t[i]), Val(length(t)))...) # Inferred eltype of result of broadcast(f, args...) function combine_eltypes(f, args::Tuple) diff --git a/test/broadcast.jl b/test/broadcast.jl index 73c01b1c0ee4d..269adc9f7276d 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -1159,3 +1159,6 @@ end import Base.Broadcast: BroadcastStyle, DefaultArrayStyle @test Base.infer_effects(BroadcastStyle, (DefaultArrayStyle{1},DefaultArrayStyle{2},)) |> Core.Compiler.is_foldable + +f51129(v, x) = (1 .- (v ./ x) .^ 2) +@test @inferred(f51129([13.0], 6.5)) == [-3.0]