Skip to content

Commit

Permalink
Remove Base.convert methods between AbstractString and Kind (#500)
Browse files Browse the repository at this point in the history
* Remove the method `convert(::Type{String}, ::Kind)`

This patch removes the method `convert(::Type{String}, ::Kind)` used for
converting kinds to strings and replaces it with the already existing
method of `Base.string`. There are two reason for this: i) the method
causes invalidations when loading the package and ii) `convert` is
called implicitly in e.g. constructors and should therefore typically
only be defined between similar enough types.

* Remove the method `Base.convert(::Type{Kind}, ::String)`

This patch removes the method `Base.convert(::Type{Kind}, ::AbstractString)`
and replaces it with a `Kind(::AbstractString)` constructor. The reason
for this is that `convert` is called implicitly in e.g. constructors and
should therefore typically only be defined between similar enough types.
  • Loading branch information
fredrikekre authored Oct 6, 2024
1 parent ad5e2b9 commit 9483de8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
22 changes: 9 additions & 13 deletions src/kinds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,36 +42,32 @@ function Kind(x::Integer)
return Base.bitcast(Kind, convert(UInt16, x))
end

function Base.convert(::Type{String}, k::Kind)
_kind_int_to_str[reinterpret(UInt16, k)]
end

function Base.convert(::Type{Kind}, s::AbstractString)
function Kind(s::AbstractString)
i = get(_kind_str_to_int, s) do
error("unknown Kind name $(repr(s))")
end
Kind(i)
end

Base.string(x::Kind) = convert(String, x)
Base.print(io::IO, x::Kind) = print(io, convert(String, x))
Base.string(x::Kind) = _kind_int_to_str[reinterpret(UInt16, x)]
Base.print(io::IO, x::Kind) = print(io, string(x))

Base.isless(x::Kind, y::Kind) = reinterpret(UInt16, x) < reinterpret(UInt16, y)

function Base.show(io::IO, k::Kind)
print(io, "K\"$(convert(String, k))\"")
print(io, "K\"", k, "\"")
end

# Save the string representation rather than the bit pattern so that kinds
# can be serialized and deserialized across different JuliaSyntax versions.
function Base.write(io::IO, k::Kind)
str = convert(String, k)
str = string(k)
write(io, UInt8(sizeof(str))) + write(io, str)
end
function Base.read(io::IO, ::Type{Kind})
len = read(io, UInt8)
str = String(read(io, len))
convert(Kind, str)
Kind(str)
end

function Base.parentmodule(k::Kind)
Expand Down Expand Up @@ -162,7 +158,7 @@ For example
* K"block" is the kind of a block of code (eg, statements within a begin-end).
"""
macro K_str(s)
convert(Kind, s)
Kind(s)
end

"""
Expand All @@ -171,7 +167,7 @@ A set of kinds which can be used with the `in` operator. For example
k in KSet"+ - *"
"""
macro KSet_str(str)
kinds = [convert(Kind, s) for s in split(str)]
kinds = [Kind(s) for s in split(str)]

quote
($(kinds...),)
Expand Down Expand Up @@ -1146,7 +1142,7 @@ function untokenize(k::Kind; unique=true)
if unique && k in _nonunique_kind_names
return nothing
else
return convert(String, k)
return string(k)
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/tokenize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ end
"type",
"var"]

@test kind(tok(kw)) == convert(Kind, kw)
@test kind(tok(kw)) == Kind(kw)
end
end

Expand Down

0 comments on commit 9483de8

Please sign in to comment.