Skip to content

Commit

Permalink
Fix failing ambiguity test cases as well as other failing test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Seelengrab committed Jun 9, 2022
1 parent 605440a commit e9a8dda
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 67 deletions.
34 changes: 17 additions & 17 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ end
striptype(::Type{T}) where {T} = T
striptype(::Any) = nothing

function showerror_ambiguous(io::IO, meth, f, args)
function showerror_ambiguous(io::IO, meths, f, args)
print(io, "MethodError: ")
show_signature_function(io, isa(f, Type) ? Type{f} : typeof(f))
print(io, "(")
Expand All @@ -342,23 +342,24 @@ function showerror_ambiguous(io::IO, meth, f, args)
print(io, "::", a)
i < length(p) && print(io, ", ")
end
print(io, ") is ambiguous. \nCandidates:")
println(io, ") is ambiguous.\n\nCandidates:")
sigfix = Any
for m in meth
print(io, "\n ", m)
for m in meths
print(io, " ")
show(io, m; digit_align_width=-2)
sigfix = typeintersect(m.sig, sigfix)
end
if isa(unwrap_unionall(sigfix), DataType) && sigfix <: Tuple
let sigfix=sigfix
if all(m->morespecific(sigfix, m.sig), meth)
if all(m->morespecific(sigfix, m.sig), meths)
print(io, "\nPossible fix, define\n ")
Base.show_tuple_as_call(io, :function, sigfix)
else
println(io)
print(io, "To resolve the ambiguity, try making one of the methods more specific, or ")
print(io, "adding a new method more specific than any of the existing applicable methods.")
end
end
println(io)
end
nothing
end
Expand Down Expand Up @@ -745,28 +746,27 @@ function print_stackframe(io, i, frame::StackFrame, n::Int, digit_align_width, m
printstyled(io, inlined ? " [inlined]" : "", color = :light_black)
end

function print_module_path_file(io, modul, file, line, modulecolor = :light_blac, digit_align_width = 0)
printstyled(io, " " ^ (digit_align_width + 2) * "@ ", color = :light_black)
function print_module_path_file(io, modul, file, line, modulecolor = :light_black, digit_align_width = 0)
printstyled(io, " " ^ (digit_align_width + 2) * "@", color = :light_black)

# module
if modul !== nothing && modulecolor !== nothing
printstyled(io, modul, color = modulecolor)
print(io, " ")
printstyled(io, modul, color = modulecolor)
end

# no file/line location info to print
iszero(line) && return

# filepath
stacktrace_expand_basepaths() && (file = something(find_source_file(file), file))
stacktrace_contract_userdir() && (file = contractuser(file))
pathparts = splitpath(file)
folderparts = pathparts[1:end-1]
if !isempty(folderparts)
printstyled(io, joinpath(folderparts...) * (Sys.iswindows() ? "\\" : "/"), color = :light_black)
end
print(io, " ")
dir = dirname(file)
!isempty(dir) && printstyled(io, dir, Filesystem.path_separator, color = :light_black)

# filename, separator, line
if line > 0
printstyled(io, pathparts[end], ":", line; color = :light_black, underline = true)
end
printstyled(io, basename(file), ":", line; color = :light_black, underline = true)
end

function show_backtrace(io::IO, t::Vector)
Expand Down
67 changes: 34 additions & 33 deletions base/methodshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ function arg_decl_parts(m::Method, html=false)
push!(tv, sig.var)
sig = sig.body
end
file = m.file
line = m.line
file, line = updated_methodloc(m)
argnames = method_argnames(m)
if length(argnames) >= m.nargs
show_env = ImmutableDict{Symbol, Any}()
Expand Down Expand Up @@ -211,38 +210,40 @@ function show(io::IO, m::Method; modulecolor = :light_black, digit_align_width =
sig = unwrap_unionall(m.sig)
if sig === Tuple
# Builtin
print(io, m.name, "(...) in ", m.module)
return
end
print(io, decls[1][2], "(")

# arguments
for (i,d) in enumerate(decls[2:end])
printstyled(io, d[1], color=:light_black)
if !isempty(d[2])
print(io, "::")
print_type_bicolor(io, d[2], color=:bold, inner_color=:normal)
print(io, m.name, "(...)")
file = "none"
line = 0
else
print(io, decls[1][2], "(")

# arguments
for (i,d) in enumerate(decls[2:end])
printstyled(io, d[1], color=:light_black)
if !isempty(d[2])
print(io, "::")
print_type_bicolor(io, d[2], color=:bold, inner_color=:normal)
end
i < length(decls)-1 && print(io, ", ")
end
i < length(decls)-1 && print(io, ", ")
end

kwargs = kwarg_decl(m)
if !isempty(kwargs)
print(io, "; ")
for kw in kwargs
skw = sym_to_string(kw)
if QuoteNode(kw) in m.roots # then it's required
printstyled(io, skw, color=:bold)
else
print(io, skw)
end
if kw != last(kwargs)
print(io, ", ")
kwargs = kwarg_decl(m)
if !isempty(kwargs)
print(io, "; ")
for kw in kwargs
skw = sym_to_string(kw)
if QuoteNode(kw) in m.roots # then it's required
printstyled(io, skw, color=:bold)
else
print(io, skw)
end
if kw != last(kwargs)
print(io, ", ")
end
end
end
print(io, ")")
show_method_params(io, tv)
end
print(io, ")")
show_method_params(io, tv)

# module & file, re-using function from errorshow.jl
println(io)
Expand All @@ -268,14 +269,14 @@ function show_method_list_header(io::IO, ms::MethodList, namefmt::Function)
"generic function")
print(io, " for ", what, " ", namedisplay, " from ")
printstyled(io, ms.mt.module, color=:blue)
print(io, ":")
elseif '#' in sname
print(io, " for anonymous function ", namedisplay, ":")
print(io, " for anonymous function ", namedisplay)
elseif mt === _TYPE_NAME.mt
print(io, " for type constructor:")
print(io, " for type constructor")
else
print(io, " for callable object:")
print(io, " for callable object")
end
!iszero(n) && print(io, ":")
end

const METHODLIST_MODULECOLORS = [:cyan, :green, :yellow]
Expand Down
16 changes: 6 additions & 10 deletions test/ambiguous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,11 @@ let err = try
end
io = IOBuffer()
Base.showerror(io, err)
lines = split(String(take!(io)), '\n')
ambig_checkline(str) = startswith(str, " ambig(x, y::Integer) in $curmod_str at") ||
startswith(str, " ambig(x::Integer, y) in $curmod_str at") ||
startswith(str, " ambig(x::Number, y) in $curmod_str at")
@test ambig_checkline(lines[2])
@test ambig_checkline(lines[3])
@test ambig_checkline(lines[4])
@test lines[5] == "Possible fix, define"
@test lines[6] == " ambig(::Integer, ::Integer)"
errstr = String(take!(io))
@test occursin(" ambig(x, y::Integer)\n @ $curmod_str", errstr)
@test occursin(" ambig(x::Integer, y)\n @ $curmod_str", errstr)
@test occursin(" ambig(x::Number, y)\n @ $curmod_str", errstr)
@test occursin("Possible fix, define\n ambig(::Integer, ::Integer)", errstr)
end

ambig_with_bounds(x, ::Int, ::T) where {T<:Integer,S} = 0
Expand All @@ -60,7 +56,7 @@ let err = try
io = IOBuffer()
Base.showerror(io, err)
lines = split(String(take!(io)), '\n')
@test lines[end] == " ambig_with_bounds(::$Int, ::$Int, ::T) where T<:Integer"
@test lines[end-1] == " ambig_with_bounds(::$Int, ::$Int, ::T) where T<:Integer"
end

## Other ways of accessing functions
Expand Down
5 changes: 4 additions & 1 deletion test/error.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

# for curmod_str
include("testenv.jl")

@testset "ExponentialBackOff" begin
@test length(ExponentialBackOff(n=10)) == 10
@test collect(ExponentialBackOff(n=10, first_delay=0.01))[1] == 0.01
Expand Down Expand Up @@ -93,6 +96,6 @@ end
f44319(1)
catch e
s = sprint(showerror, e)
@test s == "MethodError: no method matching f44319(::Int$(Sys.WORD_SIZE))\nClosest candidates are:\n f44319() at none:0"
@test s == "MethodError: no method matching f44319(::Int$(Sys.WORD_SIZE))\n\nClosest candidates are:\n f44319()\n @ $curmod_str none:0\n"
end
end
2 changes: 1 addition & 1 deletion test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Base.show_method_candidates(buf, Base.MethodError(method_c1,(1, 1, "")))
@test length(methods(method_c1)) <= 3 # because of '...' in candidate printing
Base.show_method_candidates(IOContext(buf, :color => true), Base.MethodError(method_c1,(1, 1, "")))

@test occursin("\n\n\e[0mClosest candidates are:\n\e[0m method_c1(\e[91m::Float64\e[39m, \e[91m::AbstractString...\e[39m)\n\e[0m\e[90m @ \e[39m\e[35m$modul\e[39m \e[90m$dname$sep\e[39m\e[90m\e[4m$fname:$c1line\e[24m\e[39m\n", String(take!(buf)))
@test occursin("\n\n\e[0mClosest candidates are:\n\e[0m method_c1(\e[91m::Float64\e[39m, \e[91m::AbstractString...\e[39m)\n\e[0m\e[90m @\e[39m \e[35m$modul\e[39m \e[90m$dname$sep\e[39m\e[90m\e[4m$fname:$c1line\e[24m\e[39m\n", String(take!(buf)))
Base.show_method_candidates(buf, Base.MethodError(method_c1,(1, "", "")))
@test occursin("\n\nClosest candidates are:\n method_c1(!Matched::Float64, ::AbstractString...)$cmod$cfile$c1line\n", String(take!(buf)))

Expand Down
4 changes: 2 additions & 2 deletions test/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ let ex = :(a + b)
end
foo13825(::Array{T, N}, ::Array, ::Vector) where {T, N} = nothing
@test startswith(string(first(methods(foo13825))),
"foo13825(::Array{T, N}, ::Array, ::Vector) where {T, N} in")
"foo13825(::Array{T, N}, ::Array, ::Vector) where {T, N}\n")

mutable struct TLayout
x::Int8
Expand Down Expand Up @@ -425,7 +425,7 @@ let li = typeof(fieldtype).name.mt.cache.func::Core.MethodInstance,
mmime = repr("text/plain", li.def)

@test lrepr == lmime == "MethodInstance for fieldtype(...)"
@test mrepr == mmime == "fieldtype(...) in Core"
@test mrepr == mmime == "fieldtype(...)\n @ Core"
end


Expand Down
6 changes: 3 additions & 3 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1891,16 +1891,16 @@ function _methodsstr(@nospecialize f)
end

@testset "show function methods" begin
@test occursin("methods for generic function \"sin\":\n", _methodsstr(sin))
@test occursin("methods for generic function \"sin\" from Base:\n", _methodsstr(sin))
end
@testset "show macro methods" begin
@test startswith(_methodsstr(getfield(Base,Symbol("@show"))), "# 1 method for macro \"@show\":\n")
@test startswith(_methodsstr(getfield(Base,Symbol("@show"))), "# 1 method for macro \"@show\" from Base:\n")
end
@testset "show constructor methods" begin
@test occursin(" methods for type constructor:\n", _methodsstr(Vector))
end
@testset "show builtin methods" begin
@test startswith(_methodsstr(typeof), "# 1 method for builtin function \"typeof\":\n")
@test startswith(_methodsstr(typeof), "# 1 method for builtin function \"typeof\" from Core:\n")
end
@testset "show callable object methods" begin
@test occursin("methods for callable object:\n", _methodsstr(:))
Expand Down

0 comments on commit e9a8dda

Please sign in to comment.