Narrowing substitution ignores an inner declaration that shadows the narrowed name, so a false `ensures` can verify

Abierto
#351 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@robertpenner ya está trabajando en esto.

Desde el 22/11/2026.

  • #358 de @robertpenner — abierto

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
25/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
typescript
Área
compilers

Línea de trabajo

The substitution bug lives in replaceVarInTStmts, replaceFieldsInTStmts and replacePathInTStmts in tools/src/transform.ts, which walk the tree with mapTExpr and mapTStmt without tracking bindings. Read renameFreeVar in the same file to see how shadowing is already handled, then reproduce with the shadowLetDirect and shadowDu examples through lsc gen --backend=dafny. Done means the emitted Dafny returns the inner binding in both cases. Note that open PR #358 already targets this issue.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Problem

When lsc narrows a variable, it replaces every later occurrence of the variable's name in the block with the match binder. The replacement does not stop at an inner declaration that shadows the name. Inside if (p !== undefined) { const p = 1; return p; }, the return p refers to the inner constant, but lsc emits the binder for the outer p. A false ensures verifies.

No assignment is involved, so this is separate from #342.

Reproduced on upstream main at 2ccd0a5 (0.6.11) with Dafny 4.11.0.

Reproduction

//@ ensures p === 99 ==> \result === 99
export function shadowLetDirect(p: number | undefined): number {
  if (p !== undefined) {
    const p = 1;
    return p;
  }
  return 0;
}

shadowLetDirect(99) returns 1 in JavaScript. lsc gen --backend=dafny emits:

function shadowLetDirect(p: Option<int>): int
{
  match p {
    case Some(i_p_val) =>
      var p := 1;
      i_p_val
    case None =>
      0
  }
}

and dafny verify reports 1 verified, 0 errors for the lemma that shadowLetDirect(99) == 99.

Discriminated-union narrowing has the same problem:

export type Shape = { kind: "a"; v: number } | { kind: "b"; w: number };

//@ ensures s.kind === "a" && s.v === 99 ==> \result === 99
export function shadowDu(s: Shape): number {
  if (s.kind === "a") {
    if (true) {
      const s: Shape = { kind: "a", v: 1 };
      return s.v;
    }
  }
  return 0;
}

emits var s := a(1); return i_s_v; and verifies (2 verified, 0 errors), while JavaScript returns 1 for { kind: "a", v: 99 }.

Expected

Substitution should stop wherever the name is rebound: statements after a let or const of the same name in the same block, for…of loops that bind the name, quantifiers whose bound variable has the name, lambda parameters, and nested match binders.

Likely cause

replaceVarInTStmts, replaceFieldsInTStmts and replacePathInTStmts in tools/src/transform.ts all walk the tree with mapTExpr and mapTStmt, which do not track bindings. renameFreeVar in the same file already handles shadowing and could serve as a model.

Related

  • #343: a different problem with repeated loop variable names (the _x_idx2 counter).
  • The lambda case is currently masked because mapTExpr does not enter lambda bodies at all. That is #353; a fix for it needs to respect lambda parameters that shadow the narrowed name.
Lenguaje dominante
TypeScript
Estrellas
113
Forks
13
Merge medio
15 h 35 min
PR fusionados (30 d)
56

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de midspiral/LemmaScript

Todos los issues de midspiral/LemmaScript

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.