how to debug a dispatch error

62 views
Skip to first unread message

Mauro

unread,
May 19, 2015, 11:52:28 AM5/19/15
to juli...@googlegroups.com
I need some tips on how to debug a dispatch bug for which I cannot make
a simple test case.

The bug occurs in a setup like so:
```
find_tvar{T<:Tuple}(sig::Type{T}) = 1
find_tvar(sig) = 2

find_tvar(Tuple{Type, Vararg{Any}}) # this works fine in the bug
find_tvar(Tuple{Type{Dict}} ) # in the bug this would return 2 instead of 1
```
However, this example does not reproduce it, nor the more complicated
ones I tried.

The bug occurs at this call to find_tvars:
https://github.com/mauro3/Traits.jl/blob/m3/dispatch-bug/src/Traits.jl#L281
One time it works fine but the second time it doesn't with a call with
signature:
find_tvar(Tuple{Type{Dict{K,V}}}, TypeVar(:K))

It ends up at this method:
https://github.com/mauro3/Traits.jl/blob/m3/dispatch-bug/src/Traits.jl#L511
instead of this method:
https://github.com/mauro3/Traits.jl/blob/m3/dispatch-bug/src/Traits.jl#L500

This Travis build shows the error:
https://travis-ci.org/mauro3/Traits.jl/builds/63190833#L227
(Note that in line L225 there is a `@show` of a call which dispatched to
the right method)

How should I go about debugging this? Thanks!

Yichao Yu

unread,
May 19, 2015, 1:24:49 PM5/19/15
to Julia Dev
To reproduce without `Traits.jl`

```julia
function find_tvar2{T<:Tuple}(sig::Type{T}, tv)
ns = length(sig.parameters)
out = Int[]
for i = 1:ns
if length(find_tvar2(sig.parameters[i], tv)) > 0
push!(out, i)
end
end
return out
end
find_tvar2(sig::TypeVar, tv) = Int[]
function find_tvar2(arg::DataType, tv)
if arg<:Tuple
@show arg, typeof(arg), tv
error("Dispatch error!")
end
return Int[]
end

K = TypeVar(:K, true)
V = TypeVar(:V, true)
fm = Tuple{Type{Dict{K, V}}}

println(1)
find_tvar2(fm, K)
println(2)
find_tvar2(fm, K)

```

Yichao Yu

unread,
May 19, 2015, 1:26:10 PM5/19/15
to Julia Dev
This is done by cutting done your test case and inlining functions
from `Traits.jl`

Yichao Yu

unread,
May 19, 2015, 1:32:03 PM5/19/15
to Julia Dev
Even smaller repro:

Seems to be related to bounded `TypeVar`
Not specifying `K` or make it not bounded makes the problem disappers.

```julia
function find_tvar3{T<:Tuple}(sig::Type{T})
println(1)
find_tvar3(sig.parameters[1])
end
function find_tvar3(arg::DataType)
println(2)
if arg <: Tuple
error("Dispatch error!")
end
end

K = TypeVar(:K, true)
fm = Tuple{Type{Dict{K}}}

println("First time")
find_tvar3(fm)
println("Second time")
find_tvar3(fm)
```

Output:
```
yuyichao% JULIA_LOAD_PATH=${PWD}/.. julia -f runtests.jl
First time
1
2
Second time
2
ERROR: LoadError: Dispatch error!
in find_tvar3 at /home/yuyichao/projects/mirrors/Traits.jl/runtests.jl:10
in include_from_node1 at ./loading.jl:134
while loading /home/yuyichao/projects/mirrors/Traits.jl/runtests.jl,
in expression
starting on line 20
```

Looks like a bug and probably worth a bug report.

Yichao Yu

unread,
May 19, 2015, 1:44:54 PM5/19/15
to Julia Dev
P.S. I think the current support for anonymous function in type
inference is not so great and in anycase you probably don't want to
create a new function every time you call a function just for
printing. It might be better to move `println_verb` out and pass
verbose as parameter.

Mauro

unread,
May 19, 2015, 1:52:32 PM5/19/15
to juli...@googlegroups.com
Thanks a lot! Good detective work! And you even beat me to file the
bug report: https://github.com/JuliaLang/julia/issues/11355

Yichao Yu

unread,
May 19, 2015, 1:59:04 PM5/19/15
to Julia Dev
On Tue, May 19, 2015 at 1:52 PM, Mauro <maur...@runbox.com> wrote:
> Thanks a lot! Good detective work! And you even beat me to file the
> bug report: https://github.com/JuliaLang/julia/issues/11355

LOL. I love hunting down this kind of issues. =)
Reply all
Reply to author
Forward
0 new messages