In a pure function body, a `const` declared inside an `if` branch stays in scope after the `if`, so a false `ensures` can verify
Los mantenedores suelen responder en 1 día
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
- Estancado
- Stack tecnológico
- typescript
- Área
- compilers
Línea de trabajo
Start in transformPureBody in transform.ts, where a fallthrough if splices [...s.then, ...rest], and then check flattenLambdaBody for the same splice in block-bodied lambdas. The reproduction is a pure function whose if branch declares const p, and the goal is for the ensures to fail as it does for method bodies. Done means that case reports a postcondition failure, and the other splice sites are covered. Linked PR #364 is already open against this work, so check it first.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Problem
When a pure function body has an if without else whose branch does not return, the lowering places the branch's statements ahead of the rest of the function in the then arm. A const declared inside the branch stays in scope for the statements after the if, while in JavaScript it goes out of scope at the closing brace. A false ensures can verify.
Reproduction
//@ backend dafny
//@ ensures flag ==> \result === 5
export function branchLocal(p: number, flag: boolean): number {
if (flag) {
const p = 5;
}
return p;
}
tsc --strict accepts this. In JavaScript, branchLocal(3, true) returns 3, which breaks the ensures.
Generated Dafny:
function branchLocal(p: int, flag: bool): int
{
if flag then
var p := 5;
p
else
p
}
lsc check --backend=dafny: 1 verified, 0 errors.
The method lowering keeps the branch as a Dafny block (if flag { var p := 5; ... } return p;), and Dafny correctly refutes the same ensures there. The bug is limited to pure (function) bodies and lambda bodies.
Checked on main at 2ccd0a5 (0.6.11) with Dafny 4.11.0. #358 does not change this output.
Expected
The statements after the if read the outer p, so lsc check reports a postcondition failure.
Likely cause
transformPureBody in transform.ts lowers a fallthrough if by building the then arm from [...s.then, ...rest], so a declaration in s.then scopes over rest. The same splice builds the else arm ([...s.else, ...rest]) and the narrowing-match arms ([...replaced, ...rest], [...s.noneBody, ...rest]). flattenLambdaBody does the same for block-bodied lambdas, for if branches and switch arms ([...arm.body, ...rest]). I have only run the then case above. The others follow from the same code.
A likely fix is to alpha-rename branch-local declarations that collide with a name rest reads, before splicing.
Related
- #361 has the same JavaScript scoping mismatch at the extract stage.
- #351 (shadowing in narrowing substitution).
- 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
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de midspiral/LemmaScript
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
midspiral/LemmaScript#355 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
A `const` or `let` inside a bare `{ }` block stays in scope after the block, so a false `ensures` can verifyPosiblemente ocupada @namin la tomó hoy. Abierto
Dificultad 4/5 3-5 días Aptitud para principiantes 15/100
midspiral/LemmaScript#361 ·
Los mantenedores suelen responder en 1 día
-
A closure declared before a same-name `const` or `let` in its block reads the outer variable, so a false `ensures` can verifyPosiblemente ocupada @namin la tomó hoy. Abierto
Dificultad 4/5 3-5 días Aptitud para principiantes 18/100
midspiral/LemmaScript#360 ·
Los mantenedores suelen responder en 1 día
-
An assignment used as an expression, such as `(x = 7);` or `const y = (x = 7)`, is extracted as an equality test, so a false `ensures` can verifyPosiblemente ocupada @namin la tomó hoy. Abierto
Dificultad 3/5 Medio día Aptitud para principiantes 30/100
midspiral/LemmaScript#356 · 1 reacción ·
Los mantenedores suelen responder en 1 día
-
Dificultad 3/5 1-2 días Aptitud para principiantes 62/100
midspiral/LemmaScript#354 ·
Los mantenedores suelen responder en 1 día
Todos los issues de midspiral/LemmaScript
Issues similares
-
bug done
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
neavo/LinguaGacha#1124 ·
Los mantenedores suelen responder en 1 día
-
[fullsend] E2E: backstage-microsoft-auth — catalogQuery missing error handler for network errorsPosiblemente ocupada @fullsend-ai-coder la tomó hoy. Abiertoe2e-failure ready-to-code
Dificultad 2/5 Menos de una hora Aptitud para principiantes 78/100
redhat-developer/rhdh-plugin-export-overlays#4821 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
documentation
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
brylie/mission-island#225 ·
Los mantenedores suelen responder en 1 día
-
enhancement from-app needs-triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
nightly-labs/openbot#14986 ·
Los mantenedores suelen responder en 1 día
-
status/need-triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
google-gemini/gemini-cli#30412 · 1 comentario ·
Los mantenedores suelen responder en 1 día