Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix method ambiguities in SparseArrays #30120

Merged
merged 3 commits into from
Nov 23, 2018

Conversation

tkf
Copy link
Member

@tkf tkf commented Nov 22, 2018

I added a few methods to resolve method ambiguity in SparseArrays.jl. I also included the test using detect_ambiguities as I did for LinearAlgebra.jl in #28749 and #28816.

@inline capturescalars(f, mixedargs::Tuple{SparseVecOrMat, Ref{Type{T}}, Vararg{Any}}) where {T} =
capturescalars((a1, args...)->f(a1, T, args...), (mixedargs[1], Base.tail(Base.tail(mixedargs))...))
@inline capturescalars(f, mixedargs::Tuple{Union{Ref,AbstractArray{0}}, Ref{Type{T}}, Vararg{Any}}) where {T} =
@inline capturescalars(f, mixedargs::Tuple{Union{Ref,AbstractArray{<:Any,0}}, Ref{Type{T}}, Vararg{Any}}) where {T} =
Copy link
Member Author

Choose a reason for hiding this comment

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

This was irrelevant to this PR but I suppose AbstractArray{0} was a typo?

@test ((_, x) -> x).(Int, spzeros(3)) == spzeros(3)
@test ((_, _, x) -> x).(Int, Int, spzeros(3)) == spzeros(3)
@test ((_, _, _, x) -> x).(Int, Int, Int, spzeros(3)) == spzeros(3)
@test_broken ((_, _, _, _, x) -> x).(Int, Int, Int, Int, spzeros(3)) == spzeros(3)
Copy link
Member Author

Choose a reason for hiding this comment

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

It looks like capturescalars has type inference issue when it has to recurse a lot (> 3). I wounder if capturescalars could be implemented more naturally (for humans and maybe for Julia as well?) if you used @generated function. Any reason why capturescalars is implemented based on recursion?

@@ -1006,10 +1007,6 @@ end
_copyto!(parevalf, dest, passedsrcargstup...)
end

struct CapturedScalars{F, Args, Order}
Copy link
Member Author

Choose a reason for hiding this comment

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

git grep CapturedScalars shows me that there is no code using it. Maybe some remnant from experimenting with capturescalars? This was irrelevant to this PR but I thought it's OK to remove it here (as I'm touching capturescalars).

Copy link
Sponsor Member

@timholy timholy left a comment

Choose a reason for hiding this comment

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

Thanks for doing this.

@@ -973,6 +973,7 @@ function _copy(f, args...)
parevalf, passedargstup = capturescalars(f, args)
return _copy(parevalf, passedargstup...)
end
_copy(f) = throw(MethodError(_copy, (f))) # avoid method ambiguity
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

(f) === f, should be (f,)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for catching this. I fixed it and added a test.

@andreasnoack andreasnoack merged commit f10530e into JuliaLang:master Nov 23, 2018
@KristofferC KristofferC mentioned this pull request Nov 28, 2018
61 tasks
@KristofferC KristofferC mentioned this pull request Dec 30, 2018
53 tasks
KristofferC pushed a commit that referenced this pull request Dec 30, 2018
* Remove unused struct CapturedScalars

* Fix method ambiguities in SparseArrays

* Fix HigherOrderFns._copy(f)

(cherry picked from commit f10530e)
KristofferC pushed a commit that referenced this pull request Dec 30, 2018
* Remove unused struct CapturedScalars

* Fix method ambiguities in SparseArrays

* Fix HigherOrderFns._copy(f)

(cherry picked from commit f10530e)
KristofferC pushed a commit that referenced this pull request Dec 31, 2018
* Remove unused struct CapturedScalars

* Fix method ambiguities in SparseArrays

* Fix HigherOrderFns._copy(f)

(cherry picked from commit f10530e)
@StefanKarpinski StefanKarpinski added status:triage This should be discussed on a triage call backport 1.0 and removed status:triage This should be discussed on a triage call labels Jan 31, 2019
@JeffBezanson JeffBezanson removed backport 1.0 status:triage This should be discussed on a triage call labels Jan 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants