LemmaScript icon indicating copy to clipboard operation
LemmaScript copied to clipboard

Dafny emitter writes tab and carriage return into string literals unescaped

Open levkov-vim opened this issue 3 weeks ago • 0 comments

Version: lsc 0.6.8 (latest on npm); still present on main at 6b18f23, tools/src/dafny-emit.ts, emitExpr case "str".

A string literal containing a tab or a carriage return is emitted into the .dfy as the raw character, and Dafny can't parse the result.

//@ backend dafny
export function isBlank(c: string): boolean {
  //@ verify
  return c === "\t" || c === "\n" || c === "\r"
}

lsc regen --backend=dafny emits this, where the tab and the CR are literal bytes:

(((c == "	") || (c == "\n")) || (c == "
"))

dafny verify then fails:

redirect.dfy(8,40): Error: invalid UnaryExpression
1 parse errors detected

Outside the UTF-16 profile, the emitter escapes only \\, " and \n:

: `"${e.value.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n')}"`;

Dafny string literals also accept \t, \r, \0 and \U{…}. A fix is to escape those too:

.replace(/\t/g, '\\t').replace(/\r/g, '\\r').replace(/\0/g, '\\0')

Any other control character would become \U{hex}. Char literals likely need the same treatment.

Workaround in the meantime: compare character codes instead of one-character strings, e.g. s.charCodeAt(k) === 9.

levkov-vim avatar Nov 12 '26 06:11 levkov-vim