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

Check Type has parameters field with an element #493

Merged
merged 2 commits into from
Sep 5, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions TypedSyntax/src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,17 @@ function is_show_annotation(@nospecialize(T); type_annotations::Bool, hide_type_
return isa(T, Type) && is_type_unstable(T)
end

extract_inner_type(x) = nothing
extract_inner_type(::Type{Type{T}}) where T = T
function type_annotation_mode(node, @nospecialize(T); type_annotations::Bool, hide_type_stable::Bool)
kind(node) == K"return" && return false, "", "", ""
type_annotate = is_show_annotation(T; type_annotations, hide_type_stable)
pre = pre2 = post = ""
if type_annotate
if T isa Type && T <: Type
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I guess this should have been T <: Type{S} where S or something?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Union{} <: Type{S} where S is true so I don't think that would have fixed it either.

Copy link
Contributor

Choose a reason for hiding this comment

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

It should be T isa DataType or Base.isType(T)

# Don't annotate `String::Type{String}`
if replace(sourcetext(node), r"\s" => "") == replace(sprint(show, T.parameters[1]), r"\s" => "")
return false, pre, pre2, post
end
end
inner_type = extract_inner_type(T)
if T !== nothing && replace(sourcetext(node), r"\s" => "") == replace(sprint(show, inner_type), r"\s" => "")
return false, pre, pre2, post
end
if kind(node) ∈ KSet":: where" || is_infix_op_call(node) || (is_prec_assignment(node) && kind(node) != K"=")
pre, post = "(", ")"
elseif is_prefix_op_call(node) # insert parens after prefix op and before type-annotating
Expand Down
16 changes: 16 additions & 0 deletions TypedSyntax/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,22 @@ include("test_module.jl")
@test_nowarn str = sprint(tsn; context=:color=>false) do io, obj
printstyled(io, obj; hide_type_stable=false)
end

# issue 491
tsn = TypedSyntaxNode(+, (Int, Int)) # need a node, not important what it is
@test_nowarn TypedSyntax.type_annotation_mode(tsn, Union{}; type_annotations=true, hide_type_stable=false)

# issue 492
tsn = TypedSyntaxNode(Base._tuple_unique_fieldtypes, (Any,))
@test_nowarn str = sprint(tsn; context=:color=>false) do io, obj
printstyled(io, obj; hide_type_stable=false)
end

# issue 493
tsn = TypedSyntaxNode(TSN.f493, ())
@test_nowarn str = sprint(tsn; context=:color=>false) do io, obj
printstyled(io, obj; hide_type_stable=false)
end
end

if parse(Bool, get(ENV, "CI", "false"))
Expand Down
5 changes: 5 additions & 0 deletions TypedSyntax/test/test_module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,9 @@ f482b(x) = Dict{String,Any}(x)
# Issue 487
f487(x) = 1

function f493()
T = rand() > 0.5 ? Int64 : Float64
sum(rand(T, 100))
end

end
Loading