Dafny: `toUpperCase`/`toLowerCase` are modeled as ASCII-only, so false contracts about non-ASCII text verify

Aperta
#312 1 commento 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@quangng2000 ci sta già lavorando.

Dal 16/11/2026.

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

lsc 0.6.10, Dafny 4.11.0, under both unicode-scalar (the default) and javascript-utf16.

Repro
//@ backend dafny

export function upperKeepsLength(s: string): boolean {
  //@ verify
  //@ ensures \result === true
  return s.toUpperCase().length === s.length;
}

export function lowerOfNonAsciiIsItself(s: string): boolean {
  //@ verify
  //@ requires s === "Ä"
  //@ ensures \result === true
  return s.toLowerCase() === s;
}

lsc check --backend=dafny reports 5 verified, 0 errors for this file, both as it is and with
//@ option string-semantics javascript-utf16 + //@ option dafny-library local.

JavaScript disagrees with both contracts:

upperKeepsLength("ß")           // false: "ß".toUpperCase() is "SS"
lowerOfNonAsciiIsItself("Ä") // false: "Ä".toLowerCase() is "ä"
"İ".toLowerCase().length        // 2 (a combining dot is added)
Cause

The emitted helpers map only A-Z / a-z and promise to keep the length:

function StringToUpper(s: string): string
  ensures |StringToUpper(s)| == |s|
{
  ...
    var upper := if 'a' <= c <= 'z' then (c - 'a' + 'A') as char else c;
  ...
}

String.prototype.toUpperCase/toLowerCase apply the full Unicode case mapping (UnicodeData plus
SpecialCasing). That mapping changes non-ASCII letters and can change the length. So a proof that uses
these helpers is only valid for ASCII input, but nothing in the proof requires the input to be ASCII.
toLocaleUpperCase/toLocaleLowerCase likely have the same problem if they are lowered the same way.

Suggested fix

Any one of these would make it sound:

  1. Model the result as uninterpreted (an opaque function with no length or content guarantees), possibly
    with lemmas that hold in JavaScript, e.g. ASCII input gives the ASCII result.
  2. Keep the ASCII model but emit it with requires forall i :: 0 <= i < |s| ==> s[i] < 128 as char, so a
    caller has to prove the input is ASCII.
  3. Reject the calls loudly, as is done for other unsupported built-ins.
Workaround

Fold explicitly over character codes (String.fromCharCode(c + 32) for 65 <= c <= 90). This is proven
and matches JavaScript on every code unit. Alternatively, keep the Unicode case mapping in sampled code
and prove only what comes after it.

Lingua principale
TypeScript
Stelle
113
Fork
13
Merge medio
16h 8m
PR unite (30g)
42

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di midspiral/LemmaScript

Tutte le issue di midspiral/LemmaScript

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.