In a pure function body, a `const` declared inside an `if` branch stays in scope after the `if`, so a false `ensures` can verify
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 25/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- typescript
- Domain
- compilers
Research direction
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.
Written by the indexing model from the issue text.
Description
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).
- Dominant language
- TypeScript
- Stars
- 113
- Forks
- 13
- Avg merge
- 15h 35m
- Merged PRs (30d)
- 56
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from midspiral/LemmaScript
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
midspiral/LemmaScript#355 · 1 comment ·
Maintainers usually reply within 1 day
-
A `const` or `let` inside a bare `{ }` block stays in scope after the block, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 4/5 3-5 days Newbie friendliness 15/100
midspiral/LemmaScript#361 ·
Maintainers usually reply within 1 day
-
A closure declared before a same-name `const` or `let` in its block reads the outer variable, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 4/5 3-5 days Newbie friendliness 18/100
midspiral/LemmaScript#360 ·
Maintainers usually reply within 1 day
-
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 verifyPossibly taken @namin claimed this today. Open
Difficulty 3/5 Half a day Newbie friendliness 30/100
midspiral/LemmaScript#356 · 1 reaction ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 62/100
midspiral/LemmaScript#354 ·
Maintainers usually reply within 1 day
All issues in midspiral/LemmaScript
Similar issues
-
bug done
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
neavo/LinguaGacha#1124 ·
Maintainers usually reply within 1 day
-
[fullsend] E2E: backstage-microsoft-auth — catalogQuery missing error handler for network errorsPossibly taken @fullsend-ai-coder claimed this today. Opene2e-failure ready-to-code
Difficulty 2/5 Under an hour Newbie friendliness 78/100
redhat-developer/rhdh-plugin-export-overlays#4821 · 1 comment ·
Maintainers usually reply within 1 day
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
brylie/mission-island#225 ·
Maintainers usually reply within 1 day
-
status/need-triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
google-gemini/gemini-cli#30412 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day