Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit type-printing in MethodError #50809

Merged
merged 4 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ scrub_repl_backtrace(stack::ExceptionStack) =
ExceptionStack(Any[(;x.exception, backtrace = scrub_repl_backtrace(x.backtrace)) for x in stack])

istrivialerror(stack::ExceptionStack) =
length(stack) == 1 && length(stack[1].backtrace) ≤ 1
# frame 1 = top level; assumes already went through scrub_repl_backtrace
length(stack) == 1 && length(stack[1].backtrace) ≤ 1 && !isa(stack[1].exception, MethodError)
# frame 1 = top level; assumes already went through scrub_repl_backtrace; MethodError see #50803

function display_error(io::IO, stack::ExceptionStack)
printstyled(io, "ERROR: "; bold=true, color=Base.error_color())
Expand Down
20 changes: 12 additions & 8 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,20 +268,24 @@ function showerror(io::IO, ex::MethodError)
f_is_function = true
end
print(io, "no method matching ")
show_signature_function(io, isa(f, Type) ? Type{f} : typeof(f))
print(io, "(")
iob = IOContext(IOBuffer(), io) # for type abbreviation as in #49795; some, like `convert(T, x)`, should not abbreviate
show_signature_function(iob, isa(f, Type) ? Type{f} : typeof(f))
print(iob, "(")
for (i, typ) in enumerate(arg_types_param)
print(io, "::", typ)
i == length(arg_types_param) || print(io, ", ")
print(iob, "::", typ)
i == length(arg_types_param) || print(iob, ", ")
end
if !isempty(kwargs)
print(io, "; ")
print(iob, "; ")
for (i, (k, v)) in enumerate(kwargs)
print(io, k, "::", typeof(v))
i == length(kwargs)::Int || print(io, ", ")
print(iob, k, "::", typeof(v))
i == length(kwargs)::Int || print(iob, ", ")
end
end
print(io, ")")
print(iob, ")")
str = String(take!(unwrapcontext(iob)[1]))
str = type_limited_string_from_context(io, str)
print(io, str)
end
# catch the two common cases of element-wise addition and subtraction
if (f === Base.:+ || f === Base.:-) && length(arg_types_param) == 2
Expand Down
11 changes: 8 additions & 3 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2559,17 +2559,22 @@ function show_tuple_as_call(out::IO, name::Symbol, sig::Type;
print_within_stacktrace(io, ")", bold=true)
show_method_params(io, tv)
str = String(take!(unwrapcontext(io)[1]))
str = type_limited_string_from_context(out, str)
print(out, str)
nothing
end

function type_limited_string_from_context(out::IO, str::String)
typelimitflag = get(out, :stacktrace_types_limited, nothing)
if typelimitflag isa RefValue{Bool}
sz = get(out, :displaysize, (typemax(Int), typemax(Int)))::Tuple{Int, Int}
sz = get(out, :displaysize, displaysize(out))::Tuple{Int, Int}
str_lim = type_depth_limit(str, max(sz[2], 120))
if sizeof(str_lim) < sizeof(str)
typelimitflag[] = true
end
str = str_lim
end
print(out, str)
nothing
return str
end

# limit nesting depth of `{ }` until string textwidth is less than `n`
Expand Down
19 changes: 19 additions & 0 deletions test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,25 @@ foo_9965(x::Int) = 2x
@test occursin("got unsupported keyword argument \"w\"", String(take!(io)))
end

@testset "MethodError with long types (#50803)" begin
a = view(reinterpret(reshape, UInt8, PermutedDimsArray(rand(5, 7), (2, 1))), 2:3, 2:4, 1:4) # a mildly-complex type
function f50803 end
ex50803 = try
f50803(a, a, a, a, a, a)
catch e
e
end::MethodError
tlf = Ref(false)
str = sprint(Base.showerror, ex50803; context=(:displaysize=>(1000, 120), :stacktrace_types_limited=>tlf))
@test tlf[]
@test occursin("::SubArray{…}", str)
tlf[] = false
str = sprint(Base.showerror, ex50803; context=(:displaysize=>(1000, 10000), :stacktrace_types_limited=>tlf))
@test !tlf[]
str = sprint(Base.showerror, ex50803; context=(:displaysize=>(1000, 120)))
@test !occursin("::SubArray{…}", str)
end

# Issue #20556
import REPL
module EnclosingModule
Expand Down