fix: escape special characters in Char literals#48
Merged
Conversation
A Char literal compiled to a raw character inside a double-quoted Lua string. For control characters this produces an unparseable chunk: '\n' splits the string literal across lines, and stock Lua refuses to load the file. Escape Char literals the same way String literals are escaped (decodeStringEscaping).
c2d8f9c to
42cefb6
Compare
There was a problem hiding this comment.
Pull request overview
This PR fixes Lua code generation for PureScript Char literals so that escape-sensitive characters (e.g. '\n', '\\', '\'') are emitted as valid Lua string literals rather than raw characters that can break Lua parsing. It also adds a new golden test to lock in the behavior via an eval check.
Changes:
- Update the Lua backend to escape
IR.LiteralCharsimilarly to how string literals are handled. - Add a new
Golden.CharLiterals.TestPureScript golden module covering escape-sensitive char literals. - Add corresponding golden artifacts (
.lua,.ir,corefn.json, and eval expected output).
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| lib/Language/PureScript/Backend/Lua.hs | Changes IR.LiteralChar lowering to avoid emitting raw special characters into Lua string literals. |
| test/ps/golden/Golden/CharLiterals/Test.purs | New golden test module exercising escape-sensitive char literals and comparisons. |
| test/ps/output/Golden.CharLiterals.Test/golden.lua | Golden Lua output for the new test. |
| test/ps/output/Golden.CharLiterals.Test/golden.ir | Golden IR output for the new test. |
| test/ps/output/Golden.CharLiterals.Test/corefn.json | Golden CoreFn JSON for the new test. |
| test/ps/output/Golden.CharLiterals.Test/eval/golden.txt | Expected eval output for the new golden test. |
| test/ps/output/Golden.CharLiterals.Test/eval/.gitignore | Ignores eval runtime output (actual.txt). |
Comments suppressed due to low confidence (1)
lib/Language/PureScript/Backend/Lua.hs:157
decodeStringEscapingemits PureScript-style\xescapes padded to 6 hex digits (seeLanguage.PureScript.PSString.decodeStringEscaping), but Lua string literals require\xfollowed by exactly 2 hex digits. For char literals outside the explicit cases (\n,\t,\r, quotes, backslash), this will generate Lua source that parses to the wrong runtime value (e.g.\b,\f,\v,\0, or other non-printable/whitespace chars would become\x000008-style sequences). Consider escaping only the Lua-problematic characters here (preserving the prior behavior for everything else), or implement a Lua-specific string escaping routine that produces valid Lua escape sequences throughout.
IR.LiteralChar _ann c →
pure (Right (Lua.String (decodeStringEscaping (mkString (Text.singleton c)))))
IR.LiteralBool _ann b →
pure . Right $ Lua.Boolean b
IR.LiteralArray _ann exprs →
Right . Lua.table <$> forM (zip [1 ..] exprs) \(i, e) →
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix: escape special characters in Char literals
IR.LiteralStringis escaped viadecodeStringEscapingon the way in, butIR.LiteralCharwas converted to a Lua string verbatim. A'\n'literal therefore landed as a raw newline inside a double-quoted Lua string, splitting it across two lines, and the chunk failed to load withunfinished string near '"'. Quote and backslash literals were affected the same way. Since this happens at parse time, a single such literal anywhere in a program makes the whole output file unloadable.Found while reviving the test suite of
purescript-lua-strings, whosetoCharArraytests contain a'\n'.Char literals now go through the same
decodeStringEscapingas string literals. The newGolden.CharLiterals.Testgolden locks this with an eval check overshowof the escape-sensitive literals ('\n','\t','\r','\'','\\').