A presence check in a `while` condition does not narrow the rest of the condition or the loop body

Aperta
#339 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@robertpenner ci sta già lavorando.

Dal 21/11/2026.

  • #346 di @robertpenner — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
22/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
typescript
Ambito
compilers

Direzione di ricerca

This is compiler work in the narrowing pass: start from the "Narrow Rules" section of TOOLS.md, then find where the existing if-condition presence-check narrowing is implemented and why the while condition never reaches it. Run lsc gen on the reproduction in the issue and diff the emitted Dafny to see that i stays an Option in the condition and body. Acceptance is Dafny output that verifies plus a regression test covering both a body that reads the narrowed value and one that reassigns the variable; note that PR #346 already targets this, so coordinate before starting.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Problem

TypeScript narrows an optional inside a while loop guarded by a presence check: in while (i !== undefined && i < a.length) { … a[i] … }, i is a number in the rest of the condition and in the body. lsc narrows the same check in an if condition, but not in a while condition. TOOLS.md ("Narrow Rules") has no rule for while conditions. lsc gen exits 0, and the emitted loop compares and indexes with the Option. Dafny rejects the output, so no false proof results.

Reproduced on upstream main at add6ccd (0.6.10).

Reproduction

export function whileIdx(a: number[], i?: number): number {
  let s = 0;
  while (i !== undefined && i >= 0 && i < a.length) {
    s = a[i];
    i = undefined;
  }
  return s;
}

emits:

method whileIdx(a: seq<int>, i: Option<int>) returns (res: int)
{
  var i := i;
  var s := 0;
  while (((match i { case Some(i_) => true case None => false }) && (i >= 0)) && (i < |a|))
  {
    s := a[i];
    i := None;
  }
  return s;
}

dafny verify reports four errors: arguments to >= must be of a numeric type, bitvector type, ORDINAL, char, or a set-like type (instead got Option<int> and int) and arguments to >= must have a common supertype (got Option<int> and int) for i >= 0, arguments to rank comparison must be datatypes (got Option<int> and int) for i < |a|, and incorrect type for selection into seq<int> (got Option<int>) for a[i].

Expected

A presence check at the start of a while condition narrows the rest of the condition and the loop body. The body can reassign the variable, as this one does, so the narrowing has to be re-established on every iteration rather than once before the loop. A match around the loop would not do that. One shape that does is to test and read the payload in the condition and body, for example while (i.Some? && i.value >= 0 && i.value < |a|) { s := a[i.value]; i := None; }, with each read in the body unwrapping i only where TypeScript still treats i as defined.

If while narrowing stays unsupported, lsc should report it at the source position rather than emit Dafny that does not type-check.

Acceptance criteria

  • The reproduction generates Dafny that verifies.
  • A regression test covers a while presence check whose body reads the narrowed value and a body that reassigns the variable.
Lingua principale
TypeScript
Stelle
113
Fork
13
Merge medio
15h 35m
PR unite (30g)
56

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.