From 15abadd2c9394e7d0f9e04545301b04dc6037522 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Tue, 3 Sep 2024 22:53:08 +0200 Subject: [PATCH 1/2] 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. --- src/kinds.jl | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/kinds.jl b/src/kinds.jl index c5b43e9e..98e91e01 100644 --- a/src/kinds.jl +++ b/src/kinds.jl @@ -42,10 +42,6 @@ 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) i = get(_kind_str_to_int, s) do error("unknown Kind name $(repr(s))") @@ -53,19 +49,19 @@ function Base.convert(::Type{Kind}, s::AbstractString) 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(length(str))) + write(io, str) end function Base.read(io::IO, ::Type{Kind}) @@ -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 From 58b31ef1c2875f8c11b64c3ca359a4065ca4ccbe Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Tue, 3 Sep 2024 23:16:11 +0200 Subject: [PATCH 2/2] 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 | 8 ++++---- test/tokenize.jl | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/kinds.jl b/src/kinds.jl index 98e91e01..a062f7af 100644 --- a/src/kinds.jl +++ b/src/kinds.jl @@ -42,7 +42,7 @@ function Kind(x::Integer) return Base.bitcast(Kind, convert(UInt16, x)) 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 @@ -67,7 +67,7 @@ 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) @@ -158,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 """ @@ -167,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...),) diff --git a/test/tokenize.jl b/test/tokenize.jl index 8913a20c..c3616646 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