Skip to content

Commit 0e7c9ef

Browse files
committed
Allow parsing of custom string types in core parser hook
The simplest method of doing this is to convert to String, so that's what we do here, using invokelatest.
1 parent 7e26d8b commit 0e7c9ef

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/hooks.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ function core_parser_hook(code, filename::String, lineno::Int, offset::Int, opti
143143
# The C entry points will pass us this form.
144144
(ptr,len) = code
145145
code = String(unsafe_wrap(Array, ptr, len))
146+
elseif !(code isa String || code isa SubString || code isa Vector{UInt8})
147+
# For non-Base string types, convert to UTF-8 encoding, using an
148+
# invokelatest to avoid world age issues.
149+
code = Base.invokelatest(String, code)
146150
end
147151
if !isnothing(_debug_log[])
148152
print(_debug_log[], """

test/hooks.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@
7676
@test_throws JuliaSyntax.ParseError Meta.parse("[x)")
7777
end
7878

79+
# Check custom string types defined in a world age later than
80+
# enable_in_core!() can be passed to Meta.parse()
81+
mystr = @eval begin
82+
struct MyString <: AbstractString
83+
x::String
84+
end
85+
Base.String(s::MyString) = s.x
86+
Base.ncodeunits(s::MyString) = ncodeunits(s.x)
87+
88+
MyString("hi")
89+
end
90+
@test Meta.parse(mystr) == :hi
91+
7992
JuliaSyntax.enable_in_core!(false)
8093
end
8194

0 commit comments

Comments
 (0)