Skip to content

Commit

Permalink
More robust solution
Browse files Browse the repository at this point in the history
Fixes type Union has no field parameters #494
  • Loading branch information
Zentrik committed Sep 3, 2023
1 parent b833b5e commit 30ec350
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
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 && T !== Union{} && isassigned(T.parameters, 1)
# 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
6 changes: 6 additions & 0 deletions TypedSyntax/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,12 @@ include("test_module.jl")
@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

4 comments on commit 30ec350

@jishnub
Copy link
Contributor

@jishnub jishnub commented on 30ec350 Sep 6, 2023

Choose a reason for hiding this comment

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

Gentle bump: Could a release be tagged to include the recent changes? The master branch of TypedSyntax fixes #499

@Zentrik
Copy link
Collaborator Author

@Zentrik Zentrik commented on 30ec350 Sep 6, 2023

Choose a reason for hiding this comment

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

Not sure if anyone else got notified, @timholy could you tag a new release.

@timholy
Copy link
Member

@timholy timholy commented on 30ec350 Sep 6, 2023

Choose a reason for hiding this comment

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

Done. Sorry for the delay.

@Zentrik, not to diminsh the fantastic contributions you're making here, but could future commit summaries be more specific? Something like "Fix errors in type-parameter extraction" or something?

@Zentrik
Copy link
Collaborator Author

@Zentrik Zentrik commented on 30ec350 Sep 7, 2023

Choose a reason for hiding this comment

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

Yep of course sorry.

Please sign in to comment.