Skip to content

Conversation

@MasonProtter
Copy link
Contributor

@MasonProtter MasonProtter commented Nov 21, 2025

Currently, we have some special-cased logic in the printing of MethodError that combines the method candidates of (::Type)(args...) with the method candidates for convert(::Type, args...). This is because these things are frequently confused with eachother, but IMO it's a bit misleading because those are not actually candidates!

i.e.

julia> struct Foo; x::Int; end

julia> convert(Foo, 1)
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Foo
The function `convert` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  Foo(::Int64)
   @ Main REPL[3]:2
  Foo(::Any)
   @ Main REPL[3]:2
  convert(::Type{T}, ::T) where T
   @ Base Base_compiler.jl:133

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

Foo(::Int) and Foo(::Any) weren't actually dispatch candidates here and it's kinda weird to list them.


The current implementation preceeds us having error hints, but I think this is a good use-case for those hints. This PR uses hints to turn the error messages into

julia> convert(Foo, 1)
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Foo
The function `convert` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  convert(::Type{T}, ::T) where T
   @ Base Base_compiler.jl:136

Hint: Did you mean to call Foo(::Int64) ?

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

and likewise, when the user calls a type on arguments, if there's an applicable convert method, we also suggest it:

julia> Base.convert(::Type{String}, f::Foo) = string(f);

julia> String(Foo(1))
ERROR: MethodError: no method matching String(::Foo)
The type `String` exists, but no method is defined for this combination of argument types when trying to construct it.

Closest candidates are:
  String(::LazyString)
   @ Base strings/lazy.jl:80
  String(::Vector{UInt8})
   @ Base strings/string.jl:66
  String(::SubString{String})
   @ Base strings/substring.jl:75
  ...

Hint: Did you mean to call convert(String, ::Foo) ?

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

@MasonProtter MasonProtter added the error messages Better, more actionable error messages label Nov 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

error messages Better, more actionable error messages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants