JuliaLang / JuliaLang/JuliaSyntax.jl

Macros juxtaposed with strings incorrectly(?) get raw string literal

Open
#509 4 comments 2 reactions 0 assignees View on GitHub
bug
Dominant language
Julia
Stars
293
Forks
50
PR merge metrics
No merged PRs in 30d

Description

If you directly juxtapose a macro with a string, the macro is supplied a raw literal version of the string **as if the macro were a string macro,** even though it's not.

Here is an example:
```julia
julia> macro m(x)
return esc(x)
end
@m (macro with 1 method)

julia> @m "hey $(2+2)"
"hey 4"

julia> @m"hey $(2+2)"
"hey \$(2+2)"

julia> m"hey $(2+2)"
ERROR: LoadError: UndefVarError: `@m_str` not defined
in expression starting at REPL[4]:1
```
I **think** this is a bug. On julia 1.6, which I happen to have installed and which doesn't yet have JuliaSyntax, we get this error instead:
```julia
julia> VERSION
v"1.6.7"

julia> @m"hey $(2+2)"
ERROR: syntax: invalid macro usage "@(@m_str # REPL[4], line 1 "hey $(2+2)")"
Stacktrace:
[1] top-level scope
@ none:1
```

-------

I believe the mistake is that the macro is being parsed as if it's a string macro, since the input matches what a string macro would see:
```julia
julia> macro m_str(x)
return esc(x)
end
@m_str (macro with 1 method)

julia> m"hey $(2+2)"
"hey \$(2+2)"
```

---------

I see the current behavior on both julia 1.10 and 1.12:
```julia
julia> versioninfo()
Julia Version 1.12.0-DEV.1173
Commit 169e9e8de1* (2024-09-09 15:10 UTC)
Platform Info:
OS: macOS (arm64-apple-darwin23.5.0)
CPU: 12 × Apple M2 Max
WORD_SIZE: 64
LLVM: libLLVM-18.1.7 (ORCJIT, apple-m2)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)
Environment:
JULIA_SSL_CA_ROOTS_PATH =
```
```julia
julia> versioninfo()
Julia Version 1.10.2
Commit bd47eca2c8a (2024-03-01 10:14 UTC)
Build Info:
Official https://julialang.org/ release
Platform Info:
OS: macOS (arm64-apple-darwin22.4.0)
CPU: 12 × Apple M2 Max
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)
Environment:
JULIA_SSL_CA_ROOTS_PATH =
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.