Skip to content

Commit

Permalink
Fixes bug in type parameter extraction (#508)
Browse files Browse the repository at this point in the history
Fixes #506 which was due to calling `extract_inner_type` with `Type{AbstractArray{var"#s91"<:Real, 1}}`. Uses suggestions from #493, this also removes the specialisation compared to the current implementation.
  • Loading branch information
Zentrik authored Oct 12, 2023
1 parent 2cc7f7d commit f12d409
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
11 changes: 5 additions & 6 deletions TypedSyntax/src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,16 @@ 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
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 T isa DataType && T <: Type && isassigned(T.parameters, 1)
if replace(sourcetext(node), r"\s" => "") == replace(sprint(show, T.parameters[1]), r"\s" => "")
return false, pre, pre2, post
end
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 @@ -666,6 +666,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 506
tsn, _ = TypedSyntax.tsn_and_mappings(collect, (typeof(Base.Generator(+, 1:2)),))
@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

0 comments on commit f12d409

Please sign in to comment.