Skip to content

Commit

Permalink
Use depth-limited type printing
Browse files Browse the repository at this point in the history
  • Loading branch information
charleskawczynski committed Jan 29, 2024
1 parent 6bac54e commit 55b3941
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
10 changes: 5 additions & 5 deletions src/JET.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ struct JETConfigError <: Exception
JETConfigError(msg::AbstractString, key::Symbol, val) =
(@nospecialize msg val; new(msg, key, val))
end
Base.showerror(io::IO, err::JETConfigError) = print(io, "JETConfigError: ", err.msg)
Base.showerror(io::IO, err::JETConfigError) = print(depth_lim_cxt(io), "JETConfigError: ", err.msg)

# utils
# -----
Expand Down Expand Up @@ -615,7 +615,7 @@ function analyze_and_report_call!(analyzer::AbstractAnalyzer, @nospecialize(tt::
validate_configs(analyzer, jetconfigs)
analyzer, result = analyze_gf_by_type!(analyzer, tt)
analyzername = nameof(typeof(analyzer))
sig = LazyPrinter(io::IO->Base.show_tuple_as_call(io, Symbol(""), tt))
sig = LazyPrinter(io::IO->Base_show_tuple_as_call(io, Symbol(""), tt))
source = lazy"$analyzername: $sig"
return JETCallResult(result, analyzer, source; jetconfigs...)
end
Expand All @@ -624,7 +624,7 @@ function analyze_and_report_call!(analyzer::AbstractAnalyzer, mi::MethodInstance
validate_configs(analyzer, jetconfigs)
analyzer, result = analyze_method_instance!(analyzer, mi)
analyzername = nameof(typeof(analyzer))
sig = LazyPrinter(io::IO->Base.show_tuple_as_call(io, Symbol(""), mi.specTypes))
sig = LazyPrinter(io::IO->Base_show_tuple_as_call(io, Symbol(""), mi.specTypes))
source = lazy"$analyzername: $sig"
return JETCallResult(result, analyzer, source; jetconfigs...)
end
Expand All @@ -642,7 +642,7 @@ function find_single_match(@nospecialize(tt), analyzer::AbstractAnalyzer)
end

@noinline function single_match_error(@nospecialize tt)
sig = LazyPrinter(io::IO->Base.show_tuple_as_call(io, Symbol(""), tt))
sig = LazyPrinter(io::IO->Base_show_tuple_as_call(io, Symbol(""), tt))
error(lazy"Could not find single target method for `$sig`")
end

Expand Down Expand Up @@ -715,7 +715,7 @@ end

function apply_file_config(jetconfigs, filename::AbstractString)
isfile(filename) || throw(ArgumentError("$filename doesn't exist"))
jetconfigs = set_if_missing(jetconfigs, :toplevel_logger, IOContext(stdout, JET_LOGGER_LEVEL => DEFAULT_LOGGER_LEVEL))
jetconfigs = set_if_missing(jetconfigs, :toplevel_logger, IOContext(stdout, JET_LOGGER_LEVEL => DEFAULT_LOGGER_LEVEL, :limit => true))
configfile = find_config_file(dirname(abspath(filename)))
if !isnothing(configfile)
config = parse_config_file(configfile)
Expand Down
2 changes: 1 addition & 1 deletion src/abstractinterpret/abstractanalyzer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ end
@assert JETInferenceParams(InferenceParams(); max_methods=1).max_methods == 1
@assert !JETOptimizationParams(OptimizationParams(); inlining=false).inlining

Base.show(io::IO, state::AnalyzerState) = print(io, typeof(state), "(...)")
Base.show(io::IO, state::AnalyzerState) = print(depth_lim_cxt(io), typeof(state), "(...)")

@noinline function AnalyzerState(analyzer::AbstractAnalyzer)
AnalyzerType = nameof(typeof(analyzer))
Expand Down
2 changes: 1 addition & 1 deletion src/analyzers/jetanalyzer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ function print_report_message(io::IO, report::MethodErrorReport)
end
function print_callsig(io, @nospecialize(t))
print(io, '`')
Base.show_tuple_as_call(io, Symbol(""), t)
Base_show_tuple_as_call(io, Symbol(""), t)
print(io, '`')
end

Expand Down
28 changes: 24 additions & 4 deletions src/ui/print.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
function Base_type_depth_limit(io::IO)
sz = get(io, :displaysize, (typemax(Int), typemax(Int)))::Tuple{Int, Int}
return Base.type_depth_limit(String(take!(io)), max(sz[2], 120); maxdepth=1)
end
function Base_show_tuple_as_call(io::IO, name, tt; kwargs...)
io = IOContext(io, :limit => true)
Base.show_tuple_as_call(io, name, tt; kwargs...)
end
depth_lim_cxt(io) = IOContext(io, :limit => true)

# entry
# =====

Expand Down Expand Up @@ -83,7 +93,7 @@ end

function with_bufferring(f, args...)
buf = IOBuffer()
io = IOContext(buf, args...)
io = IOContext(buf, args..., :limit => true)
f(io)
return String(take!(buf))
end
Expand All @@ -107,12 +117,14 @@ function print_reports(io::IO,

arg = :color => get(io, :color, false)
with_bufferring(arg) do io
io = depth_lim_cxt(io)
s = string(pluralize(n, "toplevel error"), " found")
printlnstyled(io, LEFT_ROOF, s, RIGHT_ROOF; color = HEADER_COLOR)

color = ERROR_COLOR

rail = with_bufferring(arg) do io
io = depth_lim_cxt(io)
printstyled(io, ""; color)
end

Expand Down Expand Up @@ -183,6 +195,7 @@ function print_stack(io, report, config, wrote_linfos, depth = 1)
linfo_hash = hash(frame)
should_print = linfo_hash wrote_linfos
push!(wrote_linfos, linfo_hash)
io = depth_lim_cxt(io)

# print current frame and go into deeper
if should_print
Expand All @@ -200,10 +213,14 @@ end
function print_frame_sig(io, frame)
mi = frame.linfo
m = mi.def
io = depth_lim_cxt(io)
if m isa Module
Base.show_mi(io, mi, #=from_stackframe=#true)
else
Base.StackTraces.show_spec_sig(IOContext(io, :backtrace=>true), m, mi.specTypes)
buf = IOBuffer()
Base.StackTraces.show_spec_sig(buf, m, mi.specTypes)
io = IOContext(io, :backtrace=>true, :limit=>true)
write(io, Base_type_depth_limit(buf));
end
end

Expand All @@ -230,6 +247,7 @@ function print_error_frame(io, report, config, depth)
frame = report.vst[depth]
color = report_color(report)

io = depth_lim_cxt(io)
print_rails(io, depth-1)
printstyled(io, ""; color)
print_frame_sig(io, frame)
Expand Down Expand Up @@ -264,6 +282,7 @@ function print_signature(io, sig::Signature, config; kwargs...)
end
end
function _print_signature(io, @nospecialize(x), config; kwargs...)
io = IOContext(io, :limit=>true)
if isa(x, Type)
if x == pairs(NamedTuple)
if isdefined(Base, Symbol("@Kwargs"))
Expand All @@ -272,7 +291,7 @@ function _print_signature(io, @nospecialize(x), config; kwargs...)
printstyled(io, "::Base.Pairs{…}"; color = TYPE_ANNOTATION_COLOR, kwargs...)
end
elseif x !== Union{}
io = IOContext(io, :backtrace=>true)
io = IOContext(io, :backtrace=>true, :limit => true)
printstyled(io, "::", x; color = TYPE_ANNOTATION_COLOR, kwargs...)
end
elseif isa(x, Repr)
Expand Down Expand Up @@ -313,13 +332,14 @@ struct IgnoreMarker end
# adapted from https://github.com/JuliaLang/julia/blob/0f11a7bb07d2d0d8413da05dadd47441705bf0dd/base/show.jl#L989-L1011
function show_mi(io::IO, l::MethodInstance)
def = l.def
io = depth_lim_cxt(io)
if isa(def, Method)
if isdefined(def, :generator) && l === def.generator
# print(io, "MethodInstance generator for ")
show(io, def)
else
# print(io, "MethodInstance for ")
Base.show_tuple_as_call(io, def.name, l.specTypes; qualified=true)
Base_show_tuple_as_call(io, def.name, l.specTypes; qualified=true)
end
else
# print(io, "Toplevel MethodInstance thunk")
Expand Down
4 changes: 2 additions & 2 deletions test/setup.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ include("interactive_utils.jl")
function get_reports_with_test(res::JET.AnyJETResult)
reports = get_reports(res)
buf = IOBuffer()
print_reports(IOContext(buf, :color=>true), reports; res.jetconfigs...)
print_reports(IOContext(buf, :color=>true, :limit=>true), reports; res.jetconfigs...)
@test !isempty(String(take!(buf)))
return reports
end
Expand All @@ -21,7 +21,7 @@ const ERROR_REPORTS_FROM_SUM_OVER_STRING = let
get_reports_with_test(result)
end

get_msg(report::JET.InferenceErrorReport) = sprint(JET.print_report, report)
get_msg(report::JET.InferenceErrorReport) = sprint(JET.print_report, report, context=(:limit >= true,))

function test_sum_over_string(ers)
@test !isempty(ers)
Expand Down

0 comments on commit 55b3941

Please sign in to comment.