Skip to content

Commit 2fcd96d

Browse files
committed
Fix show for MethodList when methods are from another module
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
1 parent bac3ba5 commit 2fcd96d

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

base/methodshow.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ function show_method_table(io::IO, ms::MethodList, max::Int=-1, header::Bool=tru
301301
last_shown_line_infos === nothing || empty!(last_shown_line_infos)
302302

303303
modul = if mt === _TYPE_NAME.mt && length(ms) > 0 # type constructor
304-
which(ms.ms[1].module, ms.ms[1].name)
304+
i = findfirst(m -> isdefined(m.module, m.name), ms.ms)
305+
i === nothing ? mt.module : which(ms.ms[i].module, ms.ms[i].name)
305306
else
306307
mt.module
307308
end

test/show.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,3 +2652,10 @@ let buf = IOBuffer()
26522652
Base.show_tuple_as_call(buf, Symbol(""), Tuple{Function,Any})
26532653
@test String(take!(buf)) == "(::Function)(::Any)"
26542654
end
2655+
2656+
module Issue49382
2657+
abstract type Type49382 end
2658+
end
2659+
using .Issue49382
2660+
(::Type{Issue49382.Type49382})() = 1
2661+
@test sprint(show, methods(Issue49382.Type49382)) isa String

0 commit comments

Comments
 (0)