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

Create overloadable function for string representations of types #537

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
30 changes: 29 additions & 1 deletion TypedSyntax/src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,40 @@ function type_annotation_mode(node, @nospecialize(T); type_annotations::Bool, hi
return type_annotate, pre, pre2, post
end

"""
type_string(type)

Overload this function to implement custom annotations for types. For example,
`type_string(::Type{Float32}) = "F32"` would result in `F32` being used to annotate
any instance of a `Float32`.
"""
function type_string(T)
if T isa DataType
if isempty(T.parameters)
return string(T)
else
wrapper = Base.typename(T).wrapper
return string(wrapper, '{', join(map(type_string, T.parameters), ','), '}')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit wary of this code mapping over T.parameters as there were quite a few issues doing a similar thing elsewhere.
In that code we had to check for whether each element in T.parameters was assigned, I don't know if that's a concern here or not.
See https://github.com/JuliaDebug/Cthulhu.jl/pull/508/files for that code and some issues with previous versions of that code are #491, #492, #494.

end
elseif T isa Union
params = union_to_tuple(T)
return string("Union{" * join(map(type_string, params), ",") * "}")
elseif T isa UnionAll
return string(type_string(T.body), " where ", string(T.var))
end
return string(T)
end
union_to_tuple(T) = (T,)
function union_to_tuple(U::Union)
return (union_to_tuple(U.a)..., union_to_tuple(U.b)...)
end

function show_annotation(io, @nospecialize(T), post, node, position; iswarn::Bool)
diagnostics = get(io, :diagnostics, nothing)
inlay_hints = get(io, :inlay_hints, nothing)

print(io, post)
T_str = string(T)
T_str = type_string(T)
MilesCranmer marked this conversation as resolved.
Show resolved Hide resolved
if iswarn && is_type_unstable(T)
color = is_small_union_or_tunion(T) ? :yellow : :red
printstyled(io, "::", T_str; color)
Expand Down
1 change: 1 addition & 0 deletions src/Cthulhu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ using REPL: REPL, AbstractTerminal
using JuliaSyntax
using JuliaSyntax: SyntaxNode, AbstractSyntaxNode, child, children
using TypedSyntax
using TypedSyntax: type_string
using WidthLimitedIO

using Core: MethodInstance, MethodMatch
Expand Down
27 changes: 27 additions & 0 deletions test/test_codeview.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module test_codeview

using Cthulhu, Test, Revise
import Cthulhu: type_string

include("setup.jl")

Expand Down Expand Up @@ -113,4 +114,30 @@ end
end
end

@testset "type aliases" begin
let
@gensym MyType
(; src, infos, mi, rt, exct, effects, slottypes) = @eval Module() begin
import Cthulhu: type_string
struct $MyType{T}
a::T
end
type_string(::Type{$MyType{T}}) where {T} = "Abracadabra{" * type_string(T) * ",5}"
$cthulhu_info() do
x = Union{$MyType{Float64},Missing}[]
return x
end
end
function prints(; kwargs...)
io = IOBuffer()
Cthulhu.cthulhu_typed(io, :none, src, rt, exct, effects, mi; kwargs...)
return String(take!(io))
end
let
s = prints()
@test occursin("Abracadabra{Float64,5}", s)
end
end
end

end # module test_codeview