Skip to content

Commit 543002d

Browse files
committed
Use broadcasting instead of map for non-numeric array projectors
This improves type stability when elements hit `ProjectTo(::Any)`.
1 parent 117910b commit 543002d

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ChainRulesCore"
22
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
3-
version = "1.15.0"
3+
version = "1.15.1"
44

55
[deps]
66
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"

src/projection.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ _eltype_projectto(::Type{<:Irrational}) = ProjectTo{Real}()
207207

208208
# In other cases, store a projector per element:
209209
function ProjectTo(xs::AbstractArray)
210-
elements = map(ProjectTo, xs)
210+
elements = ProjectTo.(xs)
211211
if elements isa AbstractArray{<:ProjectTo{<:AbstractZero}}
212212
return ProjectTo{NoTangent}() # short-circuit if all elements project to zero
213213
else

test/projection.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ struct NoSuperType end
107107
@test Tuple(ProjectTo(Any[1, 2 + 3im])(1:2)) === (1.0, 2.0 + 0.0im)
108108
@test ProjectTo(Any[true, false]) isa ProjectTo{NoTangent}
109109

110+
# projecting other things should still infer
111+
@inferred ProjectTo([one, one])
112+
@inferred ProjectTo(["x", "y"])
113+
110114
# empty arrays
111115
@test isempty(ProjectTo([])(1:0))
112116
@test_throws DimensionMismatch ProjectTo(Int[])([2])

0 commit comments

Comments
 (0)