JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.64k stars 5.48k forks source link

Line continuation character inside string macros does not continue lines #55885

Open thchr opened 4 weeks ago

thchr commented 4 weeks ago

The @lazy_str macro behaves differently from ordinary strings when using the line continuation character \:

lstr = lazy"hello \
       world"       # == "hello \\\n       world"
str  = "hello \
       world"       # == "hello world"

Apart from interpreting the line-continuation character as a "raw" backslash (and consequently also inserting a line-break), lazy"..." also doesn't ignore the white-space on the following line. This seems unintended and undesirable.

barucden commented 3 weeks ago

It's not specific to @lazy_str:

julia> macro foo_str(s)
           show(s)
       end
@foo_str (macro with 1 method)

julia> foo"hello \
           world"
"hello \\\n    world"
thchr commented 3 weeks ago

Good catch; updated title to reflect this.

vtjnash commented 3 weeks ago

There is no line continuation character for string macros, so that title isn't quite right either, as something that doesn't exist cannot be blamed for not working. The main thing this may want is to replace the content of lazy string with the content mangled according to calling str = unescape_string(escape_raw_string(str)). This is slightly breaking, so we cannot necessarily do it for existing macros, but new ones (e.g. styled"") implements this transform to follow the user's typical expectations.

thchr commented 3 weeks ago

Makes sense; feel free to update title to something more precise.

thchr commented 3 weeks ago

It seems unlikely that relatively recent string macros like lazy"" would incur real breakage for such a change though. On other hand, it probably wouldn't be nice to change raw"" to do this.