Skip to content

Commit c556734

Browse files
authored
Merge pull request #322 from JuliaDiff/mz/canonicalize
canonicalize Composite{Any}
2 parents 63de708 + e09329b commit c556734

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
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 = "0.9.31"
3+
version = "0.9.32"
44

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

src/differentials/composite.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ canonicalize(comp::Composite{<:Tuple, <:Tuple}) = comp
180180
# Dict composite are always in their canonical form.
181181
canonicalize(comp::Composite{<:Any, <:AbstractDict}) = comp
182182

183+
# Composites of unspecified primal types (indicated by specifying exactly `Any`)
184+
# all combinations of type-params are specified here to avoid ambiguities
185+
canonicalize(comp::Composite{Any, <:NamedTuple{L}}) where {L} = comp
186+
canonicalize(comp::Composite{Any, <:Tuple}) where {L} = comp
187+
canonicalize(comp::Composite{Any, <:AbstractDict}) where {L} = comp
188+
183189
"""
184190
_zeroed_backing(P)
185191

test/differentials/composite.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ end
153153
@test canonicalize(CFoo(y=10)) == CFoo(x=Zero(), y=10)
154154

155155
@test_throws ArgumentError canonicalize(CFoo(q=99.0, x=2.5))
156+
157+
@testset "unspecified primal type" begin
158+
c1 = Composite{Any}(;a=1, b=2)
159+
c2 = Composite{Any}(1, 2)
160+
c3 = Composite{Any}(Dict(4 => 3))
161+
162+
@test c1 == canonicalize(c1)
163+
@test c2 == canonicalize(c2)
164+
@test c3 == canonicalize(c3)
165+
end
156166
end
157167

158168
@testset "+ with other composites" begin

0 commit comments

Comments
 (0)