@@ -246,14 +246,15 @@ end
246246function showerror (io:: IO , ex:: MethodError )
247247 # ex.args is a tuple type if it was thrown from `invoke` and is
248248 # a tuple of the arguments otherwise.
249- is_arg_types = isa (ex. args, DataType)
250- arg_types = (is_arg_types ? ex. args : typesof (ex. args... )):: DataType
249+ is_arg_types = ! isa (ex. args, Tuple)
250+ arg_types = is_arg_types ? ex. args : typesof (ex. args... )
251+ arg_types_param:: SimpleVector = (unwrap_unionall (arg_types):: DataType ). parameters
252+ san_arg_types_param = Any[rewrap_unionall (a, arg_types) for a in arg_types_param]
251253 f = ex. f
252254 meth = methods_including_ambiguous (f, arg_types)
253255 if isa (meth, MethodList) && length (meth) > 1
254256 return showerror_ambiguous (io, meth, f, arg_types)
255257 end
256- arg_types_param:: SimpleVector = arg_types. parameters
257258 print (io, " MethodError: " )
258259 ft = typeof (f)
259260 f_is_function = false
@@ -262,10 +263,11 @@ function showerror(io::IO, ex::MethodError)
262263 f = (ex. args:: Tuple )[2 ]
263264 ft = typeof (f)
264265 arg_types_param = arg_types_param[3 : end ]
266+ san_arg_types_param = san_arg_types_param[3 : end ]
265267 kwargs = pairs (ex. args[1 ])
266268 ex = MethodError (f, ex. args[3 : end :: Int ], ex. world)
269+ arg_types = Tuple{arg_types_param... }
267270 end
268- name = ft. name. mt. name
269271 if f === Base. convert && length (arg_types_param) == 2 && ! is_arg_types
270272 f_is_function = true
271273 show_convert_error (io, ex, arg_types_param)
@@ -277,36 +279,28 @@ function showerror(io::IO, ex::MethodError)
277279 if ft <: Function && isempty (ft. parameters) && _isself (ft)
278280 f_is_function = true
279281 end
280- print (io, " no method matching " )
282+ if is_arg_types
283+ print (io, " no method matching invoke " )
284+ else
285+ print (io, " no method matching " )
286+ end
281287 buf = IOBuffer ()
282288 iob = IOContext (buf, io) # for type abbreviation as in #49795; some, like `convert(T, x)`, should not abbreviate
283289 show_signature_function (iob, isa (f, Type) ? Type{f} : typeof (f))
284- print (iob, " (" )
285- for (i, typ) in enumerate (arg_types_param)
286- print (iob, " ::" , typ)
287- i == length (arg_types_param) || print (iob, " , " )
288- end
289- if ! isempty (kwargs)
290- print (iob, " ; " )
291- for (i, (k, v)) in enumerate (kwargs)
292- print (iob, k, " ::" , typeof (v))
293- i == length (kwargs):: Int || print (iob, " , " )
294- end
295- end
296- print (iob, " )" )
290+ show_tuple_as_call (iob, :function , arg_types; hasfirst= false , kwargs = ! isempty (kwargs) ? Any[(k, typeof (v)) for (k, v) in kwargs] : nothing )
297291 str = String (take! (buf))
298292 str = type_limited_string_from_context (io, str)
299293 print (io, str)
300294 end
301295 # catch the two common cases of element-wise addition and subtraction
302- if (f === Base.:+ || f === Base.:- ) && length (arg_types_param ) == 2
296+ if (f === Base.:+ || f === Base.:- ) && length (san_arg_types_param ) == 2
303297 # we need one array of numbers and one number, in any order
304- if any (x -> x <: AbstractArray{<:Number} , arg_types_param ) &&
305- any (x -> x <: Number , arg_types_param )
298+ if any (x -> x <: AbstractArray{<:Number} , san_arg_types_param ) &&
299+ any (x -> x <: Number , san_arg_types_param )
306300
307301 nounf = f === Base.:+ ? " addition" : " subtraction"
308302 varnames = (" scalar" , " array" )
309- first, second = arg_types_param [1 ] <: Number ? varnames : reverse (varnames)
303+ first, second = san_arg_types_param [1 ] <: Number ? varnames : reverse (varnames)
310304 fstring = f === Base.:+ ? " +" : " -" # avoid depending on show_default for functions (invalidation)
311305 print (io, " \n For element-wise $nounf , use broadcasting with dot syntax: $first .$fstring $second " )
312306 end
@@ -315,17 +309,25 @@ function showerror(io::IO, ex::MethodError)
315309 print (io, " \n Use square brackets [] for indexing an Array." )
316310 end
317311 # Check for local functions that shadow methods in Base
318- if f_is_function && isdefined (Base, name)
319- basef = getfield (Base, name)
320- if basef != = ex. f && hasmethod (basef, arg_types)
321- print (io, " \n You may have intended to import " )
322- show_unquoted (io, Expr (:., :Base , QuoteNode (name)))
312+ let name = ft. name. mt. name
313+ if f_is_function && isdefined (Base, name)
314+ basef = getfield (Base, name)
315+ if basef != = f && hasmethod (basef, arg_types)
316+ print (io, " \n You may have intended to import " )
317+ show_unquoted (io, Expr (:., :Base , QuoteNode (name)))
318+ end
323319 end
324320 end
325- if (ex. world != typemax (UInt) && hasmethod (ex . f, arg_types) &&
326- ! hasmethod (ex . f, arg_types, world = ex. world))
321+ if (ex. world != typemax (UInt) && hasmethod (f, arg_types) &&
322+ ! hasmethod (f, arg_types, world = ex. world))
327323 curworld = get_world_counter ()
328324 print (io, " \n The applicable method may be too new: running in world age $(ex. world) , while current world is $(curworld) ." )
325+ elseif f isa Function
326+ print (io, " \n The function `$f ` exists, but no method is defined for this combination of argument types." )
327+ elseif f isa Type
328+ print (io, " \n The type `$f ` exists, but no method is defined for this combination of argument types when trying to construct it." )
329+ else
330+ print (io, " \n The object of type `$(typeof (f)) ` exists, but no method is defined for this combination of argument types when trying to treat it as a callable object." )
329331 end
330332 if ! is_arg_types
331333 # Check for row vectors used where a column vector is intended.
@@ -342,7 +344,7 @@ function showerror(io::IO, ex::MethodError)
342344 " \n You can convert to a column vector with the vec() function." )
343345 end
344346 end
345- Experimental. show_error_hints (io, ex, arg_types_param , kwargs)
347+ Experimental. show_error_hints (io, ex, san_arg_types_param , kwargs)
346348 try
347349 show_method_candidates (io, ex, kwargs)
348350 catch ex
@@ -354,16 +356,12 @@ end
354356striptype (:: Type{T} ) where {T} = T
355357striptype (:: Any ) = nothing
356358
357- function showerror_ambiguous (io:: IO , meths, f, args)
359+ function showerror_ambiguous (io:: IO , meths, f, args:: Type )
360+ @nospecialize f args
358361 print (io, " MethodError: " )
359362 show_signature_function (io, isa (f, Type) ? Type{f} : typeof (f))
360- print (io, " (" )
361- p = args. parameters
362- for (i,a) in enumerate (p)
363- print (io, " ::" , a)
364- i < length (p) && print (io, " , " )
365- end
366- println (io, " ) is ambiguous.\n\n Candidates:" )
363+ show_tuple_as_call (io, :var"" , args, hasfirst= false )
364+ println (io, " is ambiguous.\n\n Candidates:" )
367365 sigfix = Any
368366 for m in meths
369367 print (io, " " )
@@ -375,7 +373,7 @@ function showerror_ambiguous(io::IO, meths, f, args)
375373 let sigfix= sigfix
376374 if all (m-> morespecific (sigfix, m. sig), meths)
377375 print (io, " \n Possible fix, define\n " )
378- Base . show_tuple_as_call (io, :function , sigfix)
376+ show_tuple_as_call (io, :function , sigfix)
379377 else
380378 print (io, " To resolve the ambiguity, try making one of the methods more specific, or " )
381379 print (io, " adding a new method more specific than any of the existing applicable methods." )
@@ -401,9 +399,10 @@ stacktrace_contract_userdir()::Bool = Base.get_bool_env("JULIA_STACKTRACE_CONTRA
401399stacktrace_linebreaks ():: Bool = Base. get_bool_env (" JULIA_STACKTRACE_LINEBREAKS" , false ) === true
402400
403401function show_method_candidates (io:: IO , ex:: MethodError , @nospecialize kwargs= ())
404- is_arg_types = isa (ex. args, DataType )
402+ is_arg_types = ! isa (ex. args, Tuple )
405403 arg_types = is_arg_types ? ex. args : typesof (ex. args... )
406- arg_types_param = Any[arg_types. parameters... ]
404+ arg_types_param = Any[(unwrap_unionall (arg_types):: DataType ). parameters... ]
405+ arg_types_param = Any[rewrap_unionall (a, arg_types) for a in arg_types_param]
407406 # Displays the closest candidates of the given function by looping over the
408407 # functions methods and counting the number of matching arguments.
409408 f = ex. f
0 commit comments