Skip to content

Commit

Permalink
Fix some method ambiguities (#589)
Browse files Browse the repository at this point in the history
* Fix some method ambiguities

* Fixes

* Update src/tangent_arithmetic.jl
  • Loading branch information
devmotion authored Sep 23, 2022
1 parent f6123ee commit 9c8fcd2
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRulesCore"
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
version = "1.15.5"
version = "1.15.6"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand Down
5 changes: 5 additions & 0 deletions src/tangent_types/notimplemented.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ Base.:/(x::NotImplemented, ::Any) = throw(NotImplementedException(x))
Base.:/(::Any, x::NotImplemented) = throw(NotImplementedException(x))
Base.:/(x::NotImplemented, ::NotImplemented) = throw(NotImplementedException(x))

# Fix method ambiguity errors (#589)
Base.:/(x::AbstractZero, ::NotImplemented) = x
Base.:/(x::NotImplemented, ::AbstractThunk) = throw(NotImplementedException(x))
Base.:/(::AbstractThunk, x::NotImplemented) = throw(NotImplementedException(x))

Base.zero(x::NotImplemented) = throw(NotImplementedException(x))
function Base.zero(::Type{<:NotImplemented})
return throw(
Expand Down
5 changes: 5 additions & 0 deletions src/tangent_types/thunks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ Base.:(==)(a::AbstractThunk, b::AbstractThunk) = unthunk(a) == unthunk(b)
Base.:(-)(a::AbstractThunk) = -unthunk(a)
Base.:(-)(a::AbstractThunk, b) = unthunk(a) - b
Base.:(-)(a, b::AbstractThunk) = a - unthunk(b)
Base.:(-)(a::AbstractThunk, b::AbstractThunk) = unthunk(a) - unthunk(b)
Base.:(/)(a::AbstractThunk, b) = unthunk(a) / b
Base.:(/)(a, b::AbstractThunk) = a / unthunk(b)
Base.:(/)(a::AbstractThunk, b::AbstractThunk) = unthunk(a) / unthunk(b)

# Fix method ambiguity issue
Base.:/(a::AbstractZero, ::AbstractThunk) = a

Base.real(a::AbstractThunk) = real(unthunk(a))
Base.imag(a::AbstractThunk) = imag(unthunk(a))
Expand Down
9 changes: 6 additions & 3 deletions test/tangent_types/notimplemented.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
@test ni + ni2 === ni
@test ni2 + ni === ni2

# multiplication and dot product
# multiplication, division, and dot product
@test -ni == ni
for a in (true, x, thunk)
@test ni * a === ni
Expand All @@ -32,6 +32,7 @@
for a in (NoTangent(), ZeroTangent())
@test ni * a === a
@test a * ni === a
@test a / ni === a
@test dot(ni, a) === a
@test dot(a, ni) === a
end
Expand All @@ -52,8 +53,10 @@
@test_throws E a - ni
end
@test_throws E ni - ni2
@test_throws E ni / x
@test_throws E x / ni
for a in (true, x, thunk)
@test_throws E ni / a
@test_throws E a / ni
end
@test_throws E ni / ni2
@test_throws E zero(ni)
@test_throws E zero(typeof(ni))
Expand Down
7 changes: 7 additions & 0 deletions test/tangent_types/thunks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,15 @@
@test 1 == -@thunk(-1)
@test 1 == @thunk(2) - 1
@test 1 == 2 - @thunk(1)
@test 1 == @thunk(2) - @thunk(1)
@test 1.0 == @thunk(1) / 1.0
@test 1.0 == 1.0 / @thunk(1)
@test 1 == @thunk(1) / @thunk(1)

# check method ambiguities (#589)
for a in (ZeroTangent(), NoTangent())
@test a / @thunk(2) === a
end

@test 1 == real(@thunk(1 + 1im))
@test 1 == imag(@thunk(1 + 1im))
Expand Down

2 comments on commit 9c8fcd2

@devmotion
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/68820

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.15.6 -m "<description of version>" 9c8fcd22ee1432bb1b1707215e09af4a1ae3b072
git push origin v1.15.6

Please sign in to comment.