Hi all,
I think I may have encountered a possible bug in Julia’s method dispatch. But I would like to post this here first in case anyone can spot something that I have missed. I was able to reproduce the problem in a simple MWE modeled after my actual use case:
abstract type ParamType end
struct ParamImpl <: ParamType end
struct Foo end
struct Bar end
abstract type TestType{T1<:ParamType,T2<:Union{Nothing,<:Foo},T3<:Union{Nothing,<:Bar}} end
Base.@kwdef struct TestTypeImpl{T1,T2,T3} <: TestType{T1,T2,T3}
val1::T1 = ParamImpl()
val2::T2 = Foo()
val3::T3 = nothing
end
testfunc(::TestType{<:ParamImpl}, x) = x
testfunc(::TestType{<:ParamImpl,T2,<:Bar}, x) where {T2} = x*x
testfunc(TestTypeImpl(val3=Bar()), 2.0)
@which testfunc(TestTypeImpl(val3=Bar()), 2.0)
@assert typeof(TestTypeImpl(val3=Bar())) <: TestType{<:ParamImpl,T2,<:Bar} where {T2}
The call to testfunc
then invokes the wrong method:
julia> testfunc(TestTypeImpl(val3=Bar()), 2.0)
2.0
The expected behavior here is that the second method of testfunc
should be called, as the assertion proves that this is the correct and more specific dispatch. I haven’t been able to narrow down yet exactly what breaks this, but I’d like to get a second opinion before I spend more time on it.
Julia version info:
Julia Version 1.8.5
Commit 17cfb8e65ea (2023-01-08 06:45 UTC)
Platform Info:
OS: Linux (x86_64-linux-gnu)
CPU: 8 × Intel(R) Core(TM) i5-8265U CPU @ 1.60GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-13.0.1 (ORCJIT, skylake)
Threads: 1 on 8 virtual cores
It appears to happen on v1.9 as well.
Thanks!