In a pure function body, a `const` declared inside an `if` branch stays in scope after the `if`, so a false `ensures` can verify

Open
#362 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@namin is already working on this.

Since Nov 23, 2026.

  • #364 by @namin — open

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from midspiral/LemmaScript

All issues in midspiral/LemmaScript

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.