From 30ec3509f1ade57af820e3f54da376211a57977b Mon Sep 17 00:00:00 2001 From: Zentrik Date: Sun, 3 Sep 2023 16:58:32 +0100 Subject: [PATCH] More robust solution Fixes type Union has no field parameters #494 --- TypedSyntax/src/show.jl | 12 ++++++------ TypedSyntax/test/runtests.jl | 6 ++++++ TypedSyntax/test/test_module.jl | 5 +++++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/TypedSyntax/src/show.jl b/TypedSyntax/src/show.jl index d1965f95..5ea83586 100644 --- a/TypedSyntax/src/show.jl +++ b/TypedSyntax/src/show.jl @@ -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 diff --git a/TypedSyntax/test/runtests.jl b/TypedSyntax/test/runtests.jl index c192744d..87f71e26 100644 --- a/TypedSyntax/test/runtests.jl +++ b/TypedSyntax/test/runtests.jl @@ -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")) diff --git a/TypedSyntax/test/test_module.jl b/TypedSyntax/test/test_module.jl index 2cfc430b..1b7b7dfe 100644 --- a/TypedSyntax/test/test_module.jl +++ b/TypedSyntax/test/test_module.jl @@ -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