An early return on an optional chain or a field path does not narrow the type in resolve, so nested optional fields stay wrapped

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

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
62/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript
Domain
compilers

Research direction

Start with collectEarlyReturnNarrowings and negatedVarPresence in tools/src/resolve.ts, then compare them with ruleEarlyReturnOptChainCompare in tools/src/narrow.ts and noneDetector in tools/src/condition-facts.ts. Run the two reproduction functions through the toolchain and check the emitted Dafny. Done when the nested inner reads come out as int in both functions and the early-return narrowing for d.b is recorded in resolve, with the PR #345 condition applied.

Written by the indexing model from the issue text.

Description

Problem

After an early return on an optional chain or a field path, such as if (b?.tag !== 1) return 0; or if (d.b === undefined) return 0;, narrow unwraps the optional for the rest of the block, but resolve keeps its Option type. A later presence check on a nested optional field then does not narrow that field, and its value is returned still wrapped in Option. Dafny rejects the generated code.

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

Reproduction

export interface Box {
  tag: number;
  inner?: number;
}

export interface Deep {
  b?: Box;
}

export function optChainGuardThenNestedOpt(b: Box | undefined): number {
  if (b?.tag !== 1) return 0;
  if (b.inner !== undefined) {
    return b.inner;
  }
  return 0;
}

export function fieldGuardThenBindNestedOpt(d: Deep): number {
  if (d.b === undefined) return 0;
  const x = d.b;
  if (x.inner !== undefined) {
    return x.inner;
  }
  return 0;
}

emits

function optChainGuardThenNestedOpt(b: Option<Box>): int
{
  match b {
    case Some(i_b_val) =>
      if (i_b_val.tag != 1) then
        0
      else
        if (i_b_val.inner != None) then
          i_b_val.inner
        else
          0
    case None =>
      0
  }
}

function fieldGuardThenBindNestedOpt(d: Deep): int
{
  match d.b {
    case Some(i_d_b_val) =>
      var x := i_d_b_val;
      if (x.inner != None) then
        x.inner
      else
        0
    case None =>
      0
  }
}

i_b_val.inner and x.inner are returned as Option<int>, and Dafny reports Error: the two branches of an if-then-else expression must have the same type (got int and Option<int>) and Error: Function body type mismatch (expected int, got Option<int>).

Expected

After if (b?.tag !== 1) return 0;, resolve should treat b as Box for the rest of the block, as narrow already does. After if (d.b === undefined) return 0;, it should record d.b as narrowed to Box, as it already does inside if (d.b !== undefined) { ... }.

Likely cause

In tools/src/resolve.ts, collectEarlyReturnNarrowings uses negatedVarPresence, which only accepts a bare variable, so it misses both forms. resolveBlock threads narrowed paths across early returns for discriminant checks but not for field-path presence checks. Narrow handles both forms (ruleEarlyReturnOptChainCompare in tools/src/narrow.ts and noneDetector in tools/src/condition-facts.ts), so the two passes disagree about the types.

Related

  • #268: narrowing within an && chain.
  • #339: presence checks in a while condition.
  • #340: assigning a defined value to an optional.
  • PR #345 restricts the opt?.chain !== rhs early-return rule in narrow to a rhs that cannot be undefined. Resolve should apply the same condition.
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.