Skip to content

Dispatch error in v1.12 #58479

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

Open
JonasIsensee opened this issue May 21, 2025 · 3 comments
Open

Dispatch error in v1.12 #58479

JonasIsensee opened this issue May 21, 2025 · 3 comments
Assignees
Labels
regression Regression in behavior compared to a previous version regression 1.12 Regression in the 1.12 release types and dispatch Types, subtyping and method dispatch

Comments

@JonasIsensee
Copy link
Contributor

Hi,

this popped up in JLD2 JuliaIO/JLD2.jl#645.

using JLD2 # v0.5.13
struct Foo end
which(JLD2.h5fieldtype, Tuple{JLD2.JLDFile{JLD2.MmapIO}, Type{Type{Foo}}, Type{Type{Foo}}, Type{Val{false}}})

On v1.11 and earlier this gives:

h5fieldtype(f::JLD2.JLDFile, ::Type{T}, readas::Type, ::Union{Type{Val{true}}, Type{Val{false}}}) where T<:DataType
     @ JLD2 /data.lmp/isensee/.julia/dev/JLD2/src/data/writing_datatypes.jl:382

On v1.12:

h5fieldtype(f::JLD2.JLDFile, writeas, readas::Type, initialized::Union{Type{Val{true}}, Type{Val{false}}})
     @ JLD2 /data.lmp/isensee/.julia/dev/JLD2/src/data/writing_datatypes.jl:91

The method in line 382 still exists but cannot be dispatched to
When removing the definition in line 91, I get an error: (pasted as image for colors)

Image

Oddly enough: When tested in isolation, this works just fine.

julia> testfun(::JLD2.JLDFile, writeas::Type{T}, readas::Type, initialized::JLD2.Initialized) where T<:DataType = "works"
testfun (generic function with 1 method)

julia> which(testfun, Tuple{JLD2.JLDFile{JLD2.MmapIO}, Type{Type{Foo}}, Type{Type{Foo}}, Type{Val{false}}})
testfun(::JLD2.JLDFile, writeas::Type{T}, readas::Type, initialized::Union{Type{Val{true}}, Type{Val{false}}}) where T<:DataType
     @ Main REPL[22]:1
@Keno Keno added the types and dispatch Types, subtyping and method dispatch label May 21, 2025
@JeffBezanson JeffBezanson added regression Regression in behavior compared to a previous version regression 1.12 Regression in the 1.12 release labels May 21, 2025
@vtjnash
Copy link
Member

vtjnash commented May 29, 2025

smaller MWE

julia> for i = 1:40; @eval f(::Val{$i}) = $i; end

julia> f(::Type{<:DataType}) = 0;

julia> f(::Type) = -1;

julia> f(Type{Int}) # should == 0
-1

Note that relying on kind matching in type parameters is likely to be very unwise and cause you future problems like this, so you should consider rewriting the code not to do that (though we also plan to fix this particular regression for the v1.12 release)

@vtjnash vtjnash self-assigned this May 29, 2025
@vtjnash
Copy link
Member

vtjnash commented May 29, 2025

Some examples of the mistakes you'll encounter include in your code today, with correct dispatch:

julia> f(Type{Vector{T}} where T) # dispatches to Type{<:DataType}, despite being !concrete
0

julia> f(Type{AbstractString}) # dispatches to Type{<:DataType}, despite being abstract
0

julia> f(DataType) # dispatches to Type{<:DataType}, despite not even being Type
0

and also type normalization.

@j-fu
Copy link

j-fu commented May 29, 2025

We stumbled upon this when storing instances of our grid type to disk with JLD2, see JuliaIO/JLD2.jl#645 referenced at the start of this issue.

This is its definition:
https://github.com/WIAS-PDELib/ExtendableGrids.jl/blob/783fdd36af555d93f4d8ff2b4bfe52c434477a9a/src/extendablegrid.jl#L16

The core component is a Dict{Type{<:AbstractGridComponent}, Any} with types as keys which allows to nicely dispatch between different getindex etc methods like
instantiate(grid, ::Type{NumCellRegions}) = maximum(grid[CellRegions])

So we indeed seem to do dispatch using what you call kind matching. Is there any reference which explains why you say that this is unwise ? If this is really a serious issue, we could probably rewrite the code by using singleton instances instead of the types. @pjaap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
regression Regression in behavior compared to a previous version regression 1.12 Regression in the 1.12 release types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

No branches or pull requests

5 participants