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

dispatch failure depending on order of imports #48295

Closed
stevengj opened this issue Jan 16, 2023 · 3 comments
Closed

dispatch failure depending on order of imports #48295

stevengj opened this issue Jan 16, 2023 · 3 comments
Labels
bug Indicates an unexpected problem or unintended behavior types and dispatch Types, subtyping and method dispatch

Comments

@stevengj
Copy link
Member

stevengj commented Jan 16, 2023

I encountered an interesting situation in JuliaMath/QuadGK.jl#63. If I run the following code:

using Unitful, QuadGK
f(t) = 1.0u"J"
quadgk(f, 0.0u"s", 10.0u"s")

in Julia 1.8.2 (x86_64-apple-darwin21.4.0, QuadGK 2.6.0, and Unitful v1.12.2) then it works fine, but if I change the order of the using statements:

using QuadGK, Unitful
f(t) = 1.0u"J"
quadgk(f, 0.0u"s", 10.0u"s")

then it gives a bizarre MethodError:

julia> quadgk(f, 0.0u"s", 10.0u"s")
ERROR: MethodError: no method matching Quantity{Float64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}(::Int64)
The applicable method may be too new: running in world age 32427, while current world is 32430.
Closest candidates are:
  Quantity{T, D, U}(::Number) where {T, D, U} at ~/.julia/packages/Unitful/fbiNW/src/types.jl:151 (method too new to be called from this world context.)
  (::Type{T})(::T) where T<:Number at boot.jl:772
  (::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number} at char.jl:50
  ...
Stacktrace:
  [1] convert(#unused#::Type{Quantity{Float64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}}, x::Int64)
    @ Base ./number.jl:7
  [2] one(#unused#::Type{Quantity{Float64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}})
    @ Base ./number.jl:334
  [3] #s14#1
    @ ~/.julia/packages/QuadGK/kf0xA/src/gausskronrod.jl:260 [inlined]
  [4] var"#s14#1"(T::Any, ::Any, #unused#::Any, n::Any)
    @ QuadGK ./none:0
  [5] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any})
    @ Core ./boot.jl:582
  [6] do_quadgk(f::typeof(f), s::Tuple{Quantity{Float64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}, Quantity{Float64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}}, n::Int64, atol::Nothing, rtol::Nothing, maxevals::Int64, nrm::typeof(LinearAlgebra.norm), segbuf::Nothing)
    @ QuadGK ~/.julia/packages/QuadGK/kf0xA/src/adapt.jl:7
  [7] #28
    @ ~/.julia/packages/QuadGK/kf0xA/src/adapt.jl:186 [inlined]
  [8] handle_infinities(workfunc::QuadGK.var"#28#29"{Nothing, Nothing, Int64, Int64, typeof(LinearAlgebra.norm), Nothing}, f::typeof(f), s::Tuple{Quantity{Float64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}, Quantity{Float64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}})
    @ QuadGK ~/.julia/packages/QuadGK/kf0xA/src/adapt.jl:115
  [9] quadgk(::Function, ::Quantity{Float64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}, ::Vararg{Quantity{Float64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}}; atol::Nothing, rtol::Nothing, maxevals::Int64, order::Int64, norm::Function, segbuf::Nothing)
    @ QuadGK ~/.julia/packages/QuadGK/kf0xA/src/adapt.jl:185
 [10] quadgk(::Function, ::Quantity{Float64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}, ::Vararg{Quantity{Float64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}})
    @ QuadGK ~/.julia/packages/QuadGK/kf0xA/src/adapt.jl:183
 [11] top-level scope
    @ REPL[3]:2

And note from the stacktrace that it is calling this fallback method of one(T), which should not be the case. It should be calling the one method in Unitful.jl:

julia> T = typeof(0.0u"s")
Quantity{Float64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}

julia> one(T)
1.0

julia> @which one(T)
one(x::Type{<:Unitful.AbstractQuantity}) in Unitful at /Users/stevenj/.julia/packages/Unitful/fbiNW/src/quantities.jl:422

Seems like there is some kind of stale code where it is calling one from an old version (an old "world age") of the method table?

@stevengj stevengj added bug Indicates an unexpected problem or unintended behavior types and dispatch Types, subtyping and method dispatch labels Jan 16, 2023
@stevengj
Copy link
Member Author

@mikeingold reproduced in Julia 1.9.0-beta2 (JuliaMath/QuadGK.jl#63 (comment)).

@vtjnash
Copy link
Member

vtjnash commented Jan 16, 2023

This seems like probably a design error in one of the package, since we see it getting called from an @generated function, but those are not supposed to be written to have any dependency on world ages. It looks like QuadGK/kf0xA/src/gausskronrod.jl:260 is being blamed there for calling one, which is not a valid function to call from a function generator since it is 'generic' (expected to be overloaded)

@stevengj
Copy link
Member Author

Got it, closing this and will fix it in QuadGK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

No branches or pull requests

2 participants