Skip to content

Commit 9483de8

Browse files
authored
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.
1 parent ad5e2b9 commit 9483de8

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

src/kinds.jl

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,36 +42,32 @@ function Kind(x::Integer)
4242
return Base.bitcast(Kind, convert(UInt16, x))
4343
end
4444

45-
function Base.convert(::Type{String}, k::Kind)
46-
_kind_int_to_str[reinterpret(UInt16, k)]
47-
end
48-
49-
function Base.convert(::Type{Kind}, s::AbstractString)
45+
function Kind(s::AbstractString)
5046
i = get(_kind_str_to_int, s) do
5147
error("unknown Kind name $(repr(s))")
5248
end
5349
Kind(i)
5450
end
5551

56-
Base.string(x::Kind) = convert(String, x)
57-
Base.print(io::IO, x::Kind) = print(io, convert(String, x))
52+
Base.string(x::Kind) = _kind_int_to_str[reinterpret(UInt16, x)]
53+
Base.print(io::IO, x::Kind) = print(io, string(x))
5854

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

6157
function Base.show(io::IO, k::Kind)
62-
print(io, "K\"$(convert(String, k))\"")
58+
print(io, "K\"", k, "\"")
6359
end
6460

6561
# Save the string representation rather than the bit pattern so that kinds
6662
# can be serialized and deserialized across different JuliaSyntax versions.
6763
function Base.write(io::IO, k::Kind)
68-
str = convert(String, k)
64+
str = string(k)
6965
write(io, UInt8(sizeof(str))) + write(io, str)
7066
end
7167
function Base.read(io::IO, ::Type{Kind})
7268
len = read(io, UInt8)
7369
str = String(read(io, len))
74-
convert(Kind, str)
70+
Kind(str)
7571
end
7672

7773
function Base.parentmodule(k::Kind)
@@ -162,7 +158,7 @@ For example
162158
* K"block" is the kind of a block of code (eg, statements within a begin-end).
163159
"""
164160
macro K_str(s)
165-
convert(Kind, s)
161+
Kind(s)
166162
end
167163

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

176172
quote
177173
($(kinds...),)
@@ -1146,7 +1142,7 @@ function untokenize(k::Kind; unique=true)
11461142
if unique && k in _nonunique_kind_names
11471143
return nothing
11481144
else
1149-
return convert(String, k)
1145+
return string(k)
11501146
end
11511147
end
11521148

test/tokenize.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ end
334334
"type",
335335
"var"]
336336

337-
@test kind(tok(kw)) == convert(Kind, kw)
337+
@test kind(tok(kw)) == Kind(kw)
338338
end
339339
end
340340

0 commit comments

Comments
 (0)