Skip to content

Commit 66780fd

Browse files
authored
Improve MethodError message for anonymous functions (#57340)
If a MethodError arises on a anonyomous function, the words "anonymous function" are printed in the error like so: ```julia g=(x,y)->x+y g(1,2,3) ``` ``` ERROR: MethodError: no method of the anonymous function var"#5#6" matching (::var"#5#6")(::Int64, ::Int64, ::Int64) The function `#5` exists, but no method is defined for this combination of argument types. Closest candidates are: (::var"#5#6")(::Any, ::Any) @ Main REPL[4]:1 ``` See the [original pull request](#57319) and [issue](#56325) #56325
1 parent 4c9bede commit 66780fd

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

base/errorshow.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,9 @@ function showerror(io::IO, ex::MethodError)
346346
curworld = get_world_counter()
347347
print(io, "\nThe applicable method may be too new: running in world age $(ex.world), while current world is $(curworld).")
348348
elseif f isa Function
349-
print(io, "\nThe function `$f` exists, but no method is defined for this combination of argument types.")
349+
print(io, "\nThe ")
350+
isgensym(nameof(f)) && print(io, "anonymous ")
351+
print(io, "function `$f` exists, but no method is defined for this combination of argument types.")
350352
elseif f isa Type
351353
print(io, "\nThe type `$f` exists, but no method is defined for this combination of argument types when trying to construct it.")
352354
else

test/errorshow.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,3 +1262,10 @@ end
12621262

12631263
f33793(x::Float32, y::Float32) = 1
12641264
@test_throws "\nClosest candidates are:\n f33793(!Matched::Float32, !Matched::Float32)\n" f33793(Float64(0.0), Float64(0.0))
1265+
1266+
# https://github.com/JuliaLang/julia/issues/56325
1267+
let err_str
1268+
f56325 = x->x+1
1269+
err_str = @except_str f56325(1,2) MethodError
1270+
@test occursin("The anonymous function", err_str)
1271+
end

0 commit comments

Comments
 (0)