Skip to content

Commit

Permalink
Fix show for MethodList when methods are from another module
Browse files Browse the repository at this point in the history
When a type is defined in one module but its methods are defined
elsewhere, `show_method_table` errors due to an incorrect lookup of the
defining module used to determine colors for printing. In particular,
the code had been assuming that the type is defined in the module in
which its constructor's first method (in the sense of
`first(methods())`) is defined, which isn't always true.

To fix this, we can look through the available methods and choose the
first in which the type is defined in the method's module, falling back
to the method table's module otherwise.

Fixes #49382
Fixes #49403
Fixes #52043
  • Loading branch information
ararslan committed Nov 30, 2023
1 parent bac3ba5 commit 2fcd96d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion base/methodshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ function show_method_table(io::IO, ms::MethodList, max::Int=-1, header::Bool=tru
last_shown_line_infos === nothing || empty!(last_shown_line_infos)

modul = if mt === _TYPE_NAME.mt && length(ms) > 0 # type constructor
which(ms.ms[1].module, ms.ms[1].name)
i = findfirst(m -> isdefined(m.module, m.name), ms.ms)
i === nothing ? mt.module : which(ms.ms[i].module, ms.ms[i].name)
else
mt.module
end
Expand Down
7 changes: 7 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2652,3 +2652,10 @@ let buf = IOBuffer()
Base.show_tuple_as_call(buf, Symbol(""), Tuple{Function,Any})
@test String(take!(buf)) == "(::Function)(::Any)"
end

module Issue49382
abstract type Type49382 end
end
using .Issue49382
(::Type{Issue49382.Type49382})() = 1
@test sprint(show, methods(Issue49382.Type49382)) isa String

0 comments on commit 2fcd96d

Please sign in to comment.