julia icon indicating copy to clipboard operation
julia copied to clipboard

Consider ADL-like MethodError warning

Open Keno opened this issue 4 months ago • 2 comments

Consider

julia> module Foo
       struct Bar; end
       length(::Bar) = 1
       end
Main.Foo

julia> length(Foo.Bar())
ERROR: MethodError: no method matching length(::Main.Foo.Bar)
The function `length` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  length(::Compiler.InstructionStream)
   @ Base ../usr/share/julia/Compiler/src/ssair/ir.jl:272
  length(::Compiler.DFSTree)
   @ Base ../usr/share/julia/Compiler/src/ssair/domtree.jl:121
  length(::Core.MethodTable)
   @ Base runtime_internals.jl:1746
  ...

Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

The error here is the lack of import of Base.length. However, nothing in the error message hints you at that. We do have the reverse error message:

julia> Foo.length(1)
ERROR: MethodError: no method matching length(::Int64)
You may have intended to import Base.length
The function `length` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  length(::Main.Foo.Bar)
   @ Main.Foo REPL[1]:3

Stacktrace:
 [1] top-level scope
   @ REPL[3]:1

I think it would be reasonable for the MethodError to look through all modules of types mentioned in the failed signature to see if they have a generic function matching the name of the failed generic function and hint that there may be a missing import.

Keno avatar Jun 10 '25 00:06 Keno