Skip to content

Commit 1be790c

Browse files
authored
Tweak java getlitch not to skip zero (#18491)
The "skip zero" behavior was to accommodate escaped EOL in a text block, but also skips zero-valued escaped char `\0`. This fix is less precious and uses a flag called `skip`. Scala 2 previously received the complementary fix, because it wasn't preserving the char before the escaped EOL (after backporting the dotty code): scala/scala#10024
2 parents 64c3138 + 5f29c21 commit 1be790c

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

compiler/src/dotty/tools/dotc/parsing/JavaScanners.scala

+4-2
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ object JavaScanners {
439439
}
440440
oct.asInstanceOf[Char]
441441
end octal
442+
var skip = false
442443
def greatEscape: Char =
443444
nextChar()
444445
if '0' <= ch && ch <= '7' then octal
@@ -455,11 +456,12 @@ object JavaScanners {
455456
case '\\' => '\\'
456457
case CR | LF if inTextBlock =>
457458
if !scanOnly then nextChar()
459+
skip = true
458460
0
459461
case _ =>
460462
if !scanOnly then error("invalid escape character", charOffset - 1)
461463
ch
462-
if x != 0 then nextChar()
464+
if !skip then nextChar()
463465
x
464466
end greatEscape
465467

@@ -470,7 +472,7 @@ object JavaScanners {
470472
val res = ch
471473
nextChar()
472474
res
473-
if c != 0 && !scanOnly then putChar(c)
475+
if !skip && !scanOnly then putChar(c)
474476
end getlitch
475477

476478
/** Read a triple-quote delimited text block, starting after the first three double quotes.

tests/run/t12290.check

+8
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,11 @@ XY
6464
====
6565
X Y
6666
====
67+
582059
68+
====
69+
00
70+
====
71+
2a
72+
====
73+
c3bf
74+
====

tests/run/t12290/Test.scala

+12
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,16 @@ object Test extends App {
3030
println("====")
3131
println(valueOf[TextBlocks.Octal.type])
3232
println("====")
33+
println(hexdump(valueOf[TextBlocks.Octal.type]))
34+
println("====")
35+
println(hexdump(valueOf[TextBlocks.Zero.type].toString))
36+
println("====")
37+
println(hexdump(valueOf[TextBlocks.Magic.type].toString))
38+
println("====")
39+
println(hexdump(valueOf[TextBlocks.Maxie.type].toString))
40+
println("====")
3341
}
42+
43+
def hexdump(s: String) = s.getBytes(io.Codec.UTF8.charSet) // java.nio.charset.StandardCharsets.UTF_8
44+
.map(b => f"${b & 0xff}%02x")
45+
.mkString

tests/run/t12290/TextBlocks.java

+3
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,7 @@ class TextBlocks {
8181
""";
8282

8383
final static String Octal = "X\040Y";
84+
final static char Zero = '\0';
85+
final static char Magic = '\52';
86+
final static char Maxie = '\377';
8487
}

0 commit comments

Comments
 (0)