333333striptype (:: Type{T} ) where {T} = T
334334striptype (:: Any ) = nothing
335335
336- function showerror_ambiguous (io:: IO , meth , f, args)
336+ function showerror_ambiguous (io:: IO , meths , f, args)
337337 print (io, " MethodError: " )
338338 show_signature_function (io, isa (f, Type) ? Type{f} : typeof (f))
339339 print (io, " (" )
@@ -342,23 +342,25 @@ function showerror_ambiguous(io::IO, meth, f, args)
342342 print (io, " ::" , a)
343343 i < length (p) && print (io, " , " )
344344 end
345- print (io, " ) is ambiguous. Candidates :" )
345+ println (io, " ) is ambiguous.\n\n Candidates :" )
346346 sigfix = Any
347- for m in meth
348- print (io, " \n " , m)
347+ for m in meths
348+ print (io, " " )
349+ show (io, m; digit_align_width= - 2 )
350+ println (io)
349351 sigfix = typeintersect (m. sig, sigfix)
350352 end
351353 if isa (unwrap_unionall (sigfix), DataType) && sigfix <: Tuple
352354 let sigfix= sigfix
353- if all (m-> morespecific (sigfix, m. sig), meth )
355+ if all (m-> morespecific (sigfix, m. sig), meths )
354356 print (io, " \n Possible fix, define\n " )
355357 Base. show_tuple_as_call (io, :function , sigfix)
356358 else
357- println (io)
358359 print (io, " To resolve the ambiguity, try making one of the methods more specific, or " )
359360 print (io, " adding a new method more specific than any of the existing applicable methods." )
360361 end
361362 end
363+ println (io)
362364 end
363365 nothing
364366end
@@ -516,7 +518,7 @@ function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=()
516518 file = string (method. file)
517519 end
518520 stacktrace_contract_userdir () && (file = contractuser (file))
519- print (iob, " at " , file, " : " , line)
521+
520522 if ! isempty (kwargs):: Bool
521523 unexpected = Symbol[]
522524 if isempty (kwords) || ! (any (endswith (string (kword), " ..." ) for kword in kwords))
@@ -538,6 +540,12 @@ function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=()
538540 elseif ex. world > reinterpret (UInt, method. deleted_world)
539541 print (iob, " (method deleted before this world age.)" )
540542 end
543+ println (iob)
544+
545+ m = parentmodule_before_main (method. module)
546+ color = get! (() -> popfirst! (STACKTRACE_MODULECOLORS), STACKTRACE_FIXEDCOLORS, m)
547+ print_module_path_file (iob, m, string (file), line, color, 1 )
548+
541549 # TODO : indicate if it's in the wrong world
542550 push! (lines, (buf, right_matches))
543551 end
@@ -546,7 +554,7 @@ function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=()
546554
547555 if ! isempty (lines) # Display up to three closest candidates
548556 Base. with_output_color (:normal , io) do io
549- print (io, " \n Closest candidates are:" )
557+ print (io, " \n\ n Closest candidates are:" )
550558 sort! (lines, by = x -> - x[2 ])
551559 i = 0
552560 for line in lines
@@ -558,6 +566,7 @@ function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=()
558566 i += 1
559567 print (io, String (take! (line[1 ])))
560568 end
569+ println (io) # extra newline for spacing to stacktrace
561570 end
562571 end
563572end
@@ -573,20 +582,17 @@ end
573582# replace `sf` as needed.
574583const update_stackframes_callback = Ref {Function} (identity)
575584
576- const STACKTRACE_MODULECOLORS = [:magenta , :cyan , :green , :yellow ]
585+ const STACKTRACE_MODULECOLORS = Iterators . Stateful (Iterators . cycle ( [:magenta , :cyan , :green , :yellow ]))
577586const STACKTRACE_FIXEDCOLORS = IdDict (Base => :light_black , Core => :light_black )
578587
579588function show_full_backtrace (io:: IO , trace:: Vector ; print_linebreaks:: Bool )
580589 num_frames = length (trace)
581590 ndigits_max = ndigits (num_frames)
582591
583- modulecolordict = copy (STACKTRACE_FIXEDCOLORS)
584- modulecolorcycler = Iterators. Stateful (Iterators. cycle (STACKTRACE_MODULECOLORS))
585-
586592 println (io, " \n Stacktrace:" )
587593
588594 for (i, (frame, n)) in enumerate (trace)
589- print_stackframe (io, i, frame, n, ndigits_max, modulecolordict, modulecolorcycler )
595+ print_stackframe (io, i, frame, n, ndigits_max, STACKTRACE_FIXEDCOLORS, STACKTRACE_MODULECOLORS )
590596 if i < num_frames
591597 println (io)
592598 print_linebreaks && println (io)
@@ -646,15 +652,12 @@ function show_reduced_backtrace(io::IO, t::Vector)
646652
647653 ndigits_max = ndigits (length (t))
648654
649- modulecolordict = Dict {Module, Symbol} ()
650- modulecolorcycler = Iterators. Stateful (Iterators. cycle (STACKTRACE_MODULECOLORS))
651-
652655 push! (repeated_cycle, (0 ,0 ,0 )) # repeated_cycle is never empty
653656 frame_counter = 1
654657 for i in 1 : length (displayed_stackframes)
655658 (frame, n) = displayed_stackframes[i]
656659
657- print_stackframe (io, frame_counter, frame, n, ndigits_max, modulecolordict, modulecolorcycler )
660+ print_stackframe (io, frame_counter, frame, n, ndigits_max, STACKTRACE_FIXEDCOLORS, STACKTRACE_MODULECOLORS )
658661
659662 if i < length (displayed_stackframes)
660663 println (io)
@@ -684,22 +687,24 @@ end
684687# from `modulecolorcycler`.
685688function print_stackframe (io, i, frame:: StackFrame , n:: Int , digit_align_width, modulecolordict, modulecolorcycler)
686689 m = Base. parentmodule (frame)
687- if m != = nothing
688- while parentmodule (m) != = m
689- pm = parentmodule (m)
690- pm == Main && break
691- m = pm
692- end
693- if ! haskey (modulecolordict, m)
694- modulecolordict[m] = popfirst! (modulecolorcycler)
695- end
696- modulecolor = modulecolordict[m]
690+ modulecolor = if m != = nothing
691+ m = parentmodule_before_main (m)
692+ get! (() -> popfirst! (modulecolorcycler), modulecolordict, m)
697693 else
698- modulecolor = :default
694+ :default
699695 end
700696 print_stackframe (io, i, frame, n, digit_align_width, modulecolor)
701697end
702698
699+ # Gets the topmost parent module that isn't Main
700+ function parentmodule_before_main (m)
701+ while parentmodule (m) != = m
702+ pm = parentmodule (m)
703+ pm == Main && break
704+ m = pm
705+ end
706+ m
707+ end
703708
704709# Print a stack frame where the module color is set manually with `modulecolor`.
705710function print_stackframe (io, i, frame:: StackFrame , n:: Int , digit_align_width, modulecolor)
@@ -727,32 +732,33 @@ function print_stackframe(io, i, frame::StackFrame, n::Int, digit_align_width, m
727732 end
728733 println (io)
729734
730- # @
731- printstyled (io, " " ^ (digit_align_width + 2 ) * " @ " , color = :light_black )
735+ # @ Module path / file : line
736+ print_module_path_file (io, modul, file, line, modulecolor, digit_align_width)
737+
738+ # inlined
739+ printstyled (io, inlined ? " [inlined]" : " " , color = :light_black )
740+ end
741+
742+ function print_module_path_file (io, modul, file, line, modulecolor = :light_black , digit_align_width = 0 )
743+ printstyled (io, " " ^ (digit_align_width + 2 ) * " @" , color = :light_black )
732744
733745 # module
734- if modul != = nothing
735- printstyled (io, modul, color = modulecolor)
746+ if modul != = nothing && modulecolor != = nothing
736747 print (io, " " )
748+ printstyled (io, modul, color = modulecolor)
737749 end
738750
739751 # filepath
740- pathparts = splitpath ( file)
741- folderparts = pathparts[ 1 : end - 1 ]
742- if ! isempty (folderparts )
743- printstyled (io, joinpath (folderparts ... ) * (Sys . iswindows () ? " \\ " : " / " ), color = :light_black )
744- end
752+ stacktrace_expand_basepaths () && (file = something ( find_source_file ( file), file) )
753+ stacktrace_contract_userdir () && (file = contractuser (file))
754+ print (io, " " )
755+ dir = dirname (file )
756+ ! isempty (dir) && printstyled (io, dir, Filesystem . path_separator, color = :light_black )
745757
746758 # filename, separator, line
747- # use escape codes for formatting, printstyled can't do underlined and color
748- # codes are bright black (90) and underlined (4)
749- printstyled (io, pathparts[end ], " :" , line; color = :light_black , underline = true )
750-
751- # inlined
752- printstyled (io, inlined ? " [inlined]" : " " , color = :light_black )
759+ printstyled (io, basename (file), " :" , line; color = :light_black , underline = true )
753760end
754761
755-
756762function show_backtrace (io:: IO , t:: Vector )
757763 if haskey (io, :last_shown_line_infos )
758764 empty! (io[:last_shown_line_infos ])
0 commit comments