Skip to content

Commit ffd5a34

Browse files
Merge pull request #7714 from jhasse/patch-nomethod
Show closest matching methods on MethodError
2 parents 54511c3 + 3c80864 commit ffd5a34

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

base/replutil.jl

+47
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,53 @@ function showerror(io::IO, e::MethodError)
143143
print(io, "\nNote the difference between 1d column vector [1,2,3] and 2d row vector [1 2 3].")
144144
print(io, "\nYou can convert to a column vector with the vec() function.")
145145
end
146+
147+
# Display up to three closest candidates
148+
lines = Array((IOBuffer, Int), 0)
149+
for method in methods(e.f)
150+
n = length(e.args)
151+
if n != length(method.sig)
152+
continue
153+
end
154+
buf = IOBuffer()
155+
print(buf, " $(e.f.env.name)(")
156+
first = true
157+
right_matches = 0
158+
for (arg, sigtype) in zip(e.args, method.sig)
159+
if first
160+
first = false
161+
else
162+
print(buf, ", ")
163+
end
164+
if typeof(arg) <: sigtype
165+
right_matches += 1
166+
print(buf, "::$(sigtype)")
167+
else
168+
Base.with_output_color(:red, buf) do buf
169+
print(buf, "::$(sigtype)")
170+
end
171+
end
172+
end
173+
if right_matches > 0
174+
print(buf, ")")
175+
push!(lines, (buf, right_matches))
176+
end
177+
end
178+
if length(lines) != 0
179+
Base.with_output_color(:normal, io) do io
180+
println(io, "\nClosest candidates are:")
181+
sort!(lines, by = x -> -x[2])
182+
i = 0
183+
for line in lines
184+
if i >= 3
185+
println(io, " ...")
186+
break
187+
end
188+
i += 1
189+
println(io, takebuf_string(line[1]))
190+
end
191+
end
192+
end
146193
end
147194

148195
function show_trace_entry(io, fname, file, line, n)

0 commit comments

Comments
 (0)