Commit 4c63fda3

4c63fda3bee2Merged

Ryan Seys <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): c_string_literal preserves literal backslashes

The c_string_literal helper receives runtime Ruby string content (prism's already-cooked AST string), where any backslash is a LITERAL backslash byte, not the start of a Ruby escape sequence. The previous version still treated \ as an escape introducer and peeked at the next byte to "decode" \n, \t, etc. — collapsing the 2-byte input "\n" (backslash + n) to a single C newline byte instead of emitting \\n (backslash + n) for the C compiler.

Symptoms:

  • puts "\\n".length printed 1 instead of 2
  • "a\\nb".lines produced invalid C with a literal newline inside a string literal, failing the cc step

Fix: any backslash → emit \\ to C source unconditionally. The existing handling of newline (10.chr → \n), CR, tab, and quote is correct because those branches match the runtime byte values directly, not Ruby's source-level escape sequences.

Bootstrap fixed-point still reached (gen2.c == gen3.c). The bsl/bsl_n helpers used throughout codegen.rb construct their escape sequences via 92.chr / 10.chr to dodge this very bug, so the generated codegen output is unchanged for those call sites.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
4c63fda3bee2
Parent
8ff9aa915a30

spinel_codegen.rbmodifiedView file

@@ -19426,45 +19426,20 @@
1942619426 end
1942719427
1942819428 def c_string_literal(s)
19429+ # Input `s` is the runtime Ruby string content (already-cooked: any
19430+ # backslash in `s` is a literal backslash, NOT an escape introducer).
19431+ # We C-escape the small set that needs it: backslash, double-quote,
19432+ # newline, carriage return, tab. Everything else copies through.
19433+ # The previous version treated `s` as if it still carried Ruby
19434+ # escapes, so a 2-char input "\\n" (backslash + n) wrongly collapsed
19435+ # to a C newline; that bug is what made `"hello\\nworld\\n".lines`
19436+ # emit invalid C with a literal newline inside a string literal.
1942919437 result = "\""
1943019438 i = 0
1943119439 while i < s.length
1943219440 ch = s[i]
1943319441 if ch == bsl
19434- # Check for Ruby escape sequences
19435- if i + 1 < s.length
19436- nch = s[i + 1]
19437- if nch == "n"
19438- result = result + bsl_n
19439- i = i + 2
19440- else
19441- if nch == "t"
19442- result = result + bsl + "t"
19443- i = i + 2
19444- else
19445- if nch == "r"
19446- result = result + bsl + "r"
19447- i = i + 2
19448- else
19449- if nch == bsl
19450- result = result + bsl + bsl
19451- i = i + 2
19452- else
19453- if nch == "\""
19454- result = result + bsl + "\""
19455- i = i + 2
19456- else
19457- result = result + bsl + bsl
19458- i = i + 1
19459- end
19460- end
19461- end
19462- end
19463- end
19464- else
19465- result = result + "\\\\"
19466- i = i + 1
19467- end
19442+ result = result + bsl + bsl
1946819443 else
1946919444 if ch == "\""
1947019445 result = result + bsl + "\""
@@ -19483,8 +19458,8 @@
1948319458 end
1948419459 end
1948519460 end
19486- i = i + 1
1948719461 end
19462+ i = i + 1
1948819463 end
1948919464 # Prepend 0xff marker byte so GC can identify static literals.
1949019465 # Return form: (&("\xff" "content")[1]) — same pointer value as the