Skip to content

Commit

Permalink
Merge pull request #14132 from JuliaLang/teh/methoderror_convert
Browse files Browse the repository at this point in the history
Clarify error messages for `convert` (fixes #13033)
  • Loading branch information
jakebolewski committed Dec 8, 2015
2 parents 8039d1b + 9227c05 commit 73342cd
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions base/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,26 @@ function showerror(io::IO, ex::MethodError)
f = ex.f
end
name = isgeneric(f) ? f.env.name : :anonymous
if isa(f, DataType)
print(io, "`$(f)` has no method matching $(f)(")
if f == Base.convert && length(arg_types_param) == 2 && !is_arg_types
# See #13033
T = striptype(ex.args[1])
if T == nothing
print(io, "First argument to `convert` must be a Type, got $(ex.args[1])")
else
print(io, "Cannot `convert` an object of type $(arg_types_param[2]) to an object of type $T")
end
else
print(io, "`$(name)` has no method matching $(name)(")
end
for (i, typ) in enumerate(arg_types_param)
print(io, "::$typ")
i == length(arg_types_param) || print(io, ", ")
if isa(f, DataType)
print(io, "`$(f)` has no method matching $(f)(")
else
print(io, "`$(name)` has no method matching $(name)(")
end
for (i, typ) in enumerate(arg_types_param)
print(io, "::$typ")
i == length(arg_types_param) || print(io, ", ")
end
print(io, ")")
end
print(io, ")")
# Check for local functions that shadow methods in Base
if isdefined(Base, name)
basef = eval(Base, name)
Expand Down Expand Up @@ -218,6 +228,9 @@ function showerror(io::IO, ex::MethodError)
end
end

striptype{T}(::Type{T}) = T
striptype(::Any) = nothing

#Show an error by directly calling jl_printf.
#Useful in Base submodule __init__ functions where STDERR isn't defined yet.
function showerror_nostdio(err, msg::AbstractString)
Expand Down

0 comments on commit 73342cd

Please sign in to comment.