Skip to content

Consider ADL-like MethodError warning #58682

Open
@Keno

Description

@Keno

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions