From 9483de85a3e59f017330f6f36a083e7951262e87 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Sun, 6 Oct 2024 13:22:02 +0200 Subject: [PATCH] Remove `Base.convert` methods between `AbstractString` and `Kind` (#500) * 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. --- src/kinds.jl | 22 +++++++++------------- test/tokenize.jl | 2 +- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/kinds.jl b/src/kinds.jl index 29b8120b..554dc08d 100644 --- a/src/kinds.jl +++ b/src/kinds.jl @@ -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) @@ -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 """ @@ -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...),) @@ -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 diff --git a/test/tokenize.jl b/test/tokenize.jl index 38ff3568..e2d069da 100644 --- a/test/tokenize.jl +++ b/test/tokenize.jl @@ -334,7 +334,7 @@ end "type", "var"] - @test kind(tok(kw)) == convert(Kind, kw) + @test kind(tok(kw)) == Kind(kw) end end