Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion TypedSyntax/src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,15 @@ end
function show_annotation(io, @nospecialize(T), post, node, position; iswarn::Bool)
diagnostics = get(io, :diagnostics, nothing)
inlay_hints = get(io, :inlay_hints, nothing)
maxtypedepth = get(io, :maxtypedepth, nothing)

print(io, post)
if isa(T, Core.Const) && isa(T.val, Type)
T = Type{T.val}
end
T_str = string(T)
T_str_long = string(T)
sz = get(io, :displaysize, displaysize(io))::Tuple{Int, Int}
T_str = Base.type_depth_limit(T_str_long, max(sz[2], 120); maxdepth=maxtypedepth)
if iswarn && is_type_unstable(T)
color = is_small_union_or_tunion(T) ? :yellow : :red
printstyled(io, "::", T_str; color)
Expand Down
4 changes: 3 additions & 1 deletion src/Cthulhu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Base.@kwdef mutable struct CthulhuConfig
inlay_types_vscode::Bool = true
diagnostics_vscode::Bool = true
jump_always::Bool = false
type_depth_limit::Union{Nothing, Int} = 2
end

"""
Expand Down Expand Up @@ -486,6 +487,7 @@ function _descend(term::AbstractTerminal, interp::AbstractInterpreter, curs::Abs
inlay_types_vscode::Bool = CONFIG.inlay_types_vscode, # default is true
diagnostics_vscode::Bool = CONFIG.diagnostics_vscode, # default is true
jump_always::Bool = CONFIG.jump_always, # default is false
type_depth_limit::Union{Nothing, Int} = CONFIG.type_depth_limit, # default is 2
)

if isnothing(hide_type_stable)
Expand Down Expand Up @@ -726,7 +728,7 @@ function _descend(term::AbstractTerminal, interp::AbstractInterpreter, curs::Abs
remarks, with_effects, exception_type, inline_cost,
type_annotations, annotate_source,
inlay_types_vscode, diagnostics_vscode,
jump_always)
jump_always, type_depth_limit)

elseif toggle === :warn
iswarn ⊻= true
Expand Down
16 changes: 10 additions & 6 deletions src/codeview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,
debuginfo = IRShow.debuginfo(debuginfo)
lineprinter = __debuginfo[debuginfo]
rettype = ignorelimited(rt)
lambda_io = IOContext(io, :limit=>true)
maxtypedepth = CONFIG.type_depth_limit
lambda_io = IOContext(io, :limit=>true, :maxtypedepth => maxtypedepth)

if annotate_source && isa(src, CodeInfo)
tsn, _ = get_typed_sourcetext(mi, src, rt)
Expand All @@ -176,7 +177,8 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,
vscode_io = IOContext(
jump_always && inlay_types_vscode ? devnull : lambda_io,
:inlay_hints => inlay_types_vscode ? Dict{String,Vector{TypedSyntax.InlayHint}}() : nothing ,
:diagnostics => diagnostics_vscode ? TypedSyntax.Diagnostic[] : nothing
:diagnostics => diagnostics_vscode ? TypedSyntax.Diagnostic[] : nothing,
:maxtypedepth => maxtypedepth
)

if istruncated
Expand All @@ -187,7 +189,7 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,

callsite_diagnostics = TypedSyntax.Diagnostic[]
if (diagnostics_vscode || inlay_types_vscode)
vscode_io = IOContext(devnull, :inlay_hints=>vscode_io[:inlay_hints], :diagnostics=>vscode_io[:diagnostics])
vscode_io = IOContext(devnull, :inlay_hints=>vscode_io[:inlay_hints], :diagnostics=>vscode_io[:diagnostics], :maxtypedepth=>CONFIG.type_depth_limit)
callsite_mis = Dict() # type annotation is a bit long so I skipped it, doesn't seem to affect performance
visited_mis = Set{MethodInstance}((mi,))
add_callsites!(callsite_mis, visited_mis, callsite_diagnostics, mi; optimize, annotate_source, interp)
Expand Down Expand Up @@ -219,6 +221,7 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,
show_variables(io, src, slotnames)
end
end
maxtypedepth = CONFIG.type_depth_limit

# preprinter configuration
preprinter = if inline_cost
Expand Down Expand Up @@ -248,7 +251,7 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,
idx == -1 ? lpad(total_cost, nd+1) :
" "^(nd+1)
str = sprint(; context=:color=>true) do @nospecialize io
printstyled(io, str; color=:green)
printstyled(io, str; color=:green, maxtypedepth)
end
if debuginfo === :source
str *= " "
Expand Down Expand Up @@ -296,7 +299,7 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,
function (io::IO; idx::Int, @nospecialize(kws...))
_postprinter(io; idx, kws...)
for i = searchsorted(pc2remarks, idx=>"", by=((idx,msg),)->idx)
printstyled(io, ' ', pc2remarks[i].second; color=:light_black)
printstyled(io, ' ', pc2remarks[i].second; color=:light_black, maxtypedepth)
end
end
else
Expand Down Expand Up @@ -334,8 +337,9 @@ function descend_into_callsite!(io::IO, tsn::TypedSyntaxNode;
# We empty the body when filling kwargs
istruncated = isempty(children(body))
idxend = istruncated ? JuliaSyntax.last_byte(sig) : lastindex(tsn.source)
maxtypedepth = CONFIG.type_depth_limit
if !istruncated # If method only fills in default arguments
printstyled(io, tsn; type_annotations, iswarn, hide_type_stable, idxend)
printstyled(io, tsn; type_annotations, iswarn, hide_type_stable, idxend, maxtypedepth)
end
end

Expand Down
3 changes: 2 additions & 1 deletion src/ui.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ function build_options(callsites, with_effects::Bool, exception_type::Bool, opti
end
str = string(chomp(
sprint(node; context=:color=>true) do io, node
limiter = TextWidthLimiter(io, reduced_displaysize)
ioc = IOContext(io, :maxtypedepth=>CONFIG.type_depth_limit)
limiter = TextWidthLimiter(ioc, reduced_displaysize)
if TypedSyntax.is_runtime(node)
if iswarn
printstyled(limiter, "runtime "; color=:red)
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Test, PerformanceTestTools
using Core: Const # allows correct printing as `Const` instead of `Core.Const`

using Cthulhu
Cthulhu.CONFIG.type_depth_limit = nothing # disable type-depth limit printing in tests

@testset "runtests.jl" begin
@testset "test_Cthulhu.jl" begin
include("test_Cthulhu.jl")
Expand Down
48 changes: 48 additions & 0 deletions test/test_depth_limited_type_printing.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#=
using Revise; include(joinpath("test", "test_depth_limited_type_printing.jl"))
=#
import Cthulhu

using Cthulhu
Cthulhu.CONFIG.type_depth_limit = 2

Base.@kwdef struct Nested{A,B}
num::Int = 1
end
bar(x) = rand() > 0.5 ? x : Any[0][1]
mysum(x) = sum(y-> bar(x.num), 1:5; init=0)
nest_val(na, nb, ::Val{1}) = Nested{na, nb}()
nest_val(na, nb, ::Val{n}) where {n} = nest_val(Nested{na, nb}, Nested{na, nb}, Val(n-1))
nest_val(na, nb, n::Int) = nest_val(na, nb, Val(n))
nest_val(n) = nest_val(1, 1, n)
const NV = nest_val(5)

# f = nest_val(5)
# a = Any[f];
# mysum(a[1]) # make sure it runs
# Cthulhu.@descend mysum(a[1]) # navigate to sum -> sum, and Nested will be there
using Test
include("setup.jl")
@testset "hide type-stable statements" begin
let # optimize code
# f = nest_val(5)
# a = Any[f];
# mysum(a[1]) # make sure it runs
# Cthulhu.@descend mysum(a[1]) # navigate to sum -> sum, and Nested will be there
(; src, infos, mi, rt, exct, effects, slottypes) = @eval Module() begin
$cthulhu_info($mysum, ($(typeof(NV)),))
end;
function prints(; kwargs...)
io = IOBuffer()
ioc = IOContext(io, :maxtypedepth => Cthulhu.CONFIG.type_depth_limit)
Cthulhu.cthulhu_typed(ioc, :none, src, rt, exct, effects, mi; kwargs...)
return String(take!(io))
end;

let # by default, should print every statement
s = prints()
println(s)
# @test occursin("::Nested{Nested{…}, Nested{…}}", s)
end
end
end