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

Specificity alteration under precompilation #22002

Closed
timholy opened this issue May 21, 2017 · 1 comment
Closed

Specificity alteration under precompilation #22002

timholy opened this issue May 21, 2017 · 1 comment
Labels
types and dispatch Types, subtyping and method dispatch

Comments

@timholy
Copy link
Member

timholy commented May 21, 2017

Noticed in JuliaGraphics/Gtk.jl#311. Here's a reasonably short repro:

__precompile__(false)

module Convert

import Base: convert

export GBoxed

abstract type GBoxed  end

convert(::Type{GBoxed}, unbox::Ptr{GBoxed}) = GBoxedUnkown(unbox)
convert{T <: GBoxed}(::Type{GBoxed}, unbox::Ptr{T}) = GBoxedUnkown(unbox)
convert{T <: GBoxed}(::Type{T}, unbox::Ptr{GBoxed}) = convert(T, convert(Ptr{T}, unbox))
convert{T <: GBoxed}(::Type{T}, unbox::Ptr{T}) = T(unbox)

abstract type GdkEvent <: GBoxed end

convert(::Type{GdkEvent}, evt::Ptr{GdkEvent}) = -1

end

Because the method tables for convert are very long, this filter_gboxed.jl script is handy:

using Convert

isgboxed_method(m::Method) = _isgboxed(Base.unwrap_unionall(m.sig).parameters[2])
_isgboxed(::Type{Type{T}}) where T<:GBoxed = true
function _isgboxed(t::Type)
    p = Base.unwrap_unionall(t).parameters
    length(p) != 1 && return false
    _isgboxed(p[1])
end
_isgboxed(TV::TypeVar) = TV.ub <: GBoxed
_isgboxed(arg) = false

mt = methods(convert)
collect(Iterators.filter(isgboxed_method, mt))

As written, the output is

5-element Array{Any,1}:
 convert(::Type{Convert.GdkEvent}, evt::Ptr{Convert.GdkEvent}) in Convert at /tmp/Convert.jl:18         
 convert(::Type{T}, unbox::Ptr{T}) where T<:Convert.GBoxed in Convert at /tmp/Convert.jl:14             
 convert(::Type{Convert.GBoxed}, unbox::Ptr{Convert.GBoxed}) in Convert at /tmp/Convert.jl:11           
 convert(::Type{Convert.GBoxed}, unbox::Ptr{T}) where T<:Convert.GBoxed in Convert at /tmp/Convert.jl:12
 convert(::Type{T}, unbox::Ptr{Convert.GBoxed}) where T<:Convert.GBoxed in Convert at /tmp/Convert.jl:13

However, if you change it to __precompile__(true) then I get

 convert(::Type{Convert.GBoxed}, unbox::Ptr{Convert.GBoxed}) in Convert at /tmp/Convert.jl:11           
 convert(::Type{T}, unbox::Ptr{T}) where T<:Convert.GBoxed in Convert at /tmp/Convert.jl:14             
 convert(::Type{Convert.GdkEvent}, evt::Ptr{Convert.GdkEvent}) in Convert at /tmp/Convert.jl:18         
 convert(::Type{Convert.GBoxed}, unbox::Ptr{T}) where T<:Convert.GBoxed in Convert at /tmp/Convert.jl:12
 convert(::Type{T}, unbox::Ptr{Convert.GBoxed}) where T<:Convert.GBoxed in Convert at /tmp/Convert.jl:13

The second method should come after the third.

@timholy
Copy link
Member Author

timholy commented May 21, 2017

Seems similar to #21382.

@timholy timholy added the types and dispatch Types, subtyping and method dispatch label May 21, 2017
tkelman pushed a commit that referenced this issue Jun 5, 2017
fix #22164, specificity issue in ForwardDiff

(cherry picked from commit e92eb58)
partial backport of #22162
tkelman added a commit that referenced this issue Jun 5, 2017
[release-0.6] fix #22164 and #22002, method sorting problems
maleadt added a commit to JuliaGPU/CUDAnative.jl that referenced this issue Aug 2, 2017
maleadt added a commit to JuliaGPU/CUDAnative.jl that referenced this issue Aug 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

No branches or pull requests

2 participants