`null` and `undefined` both become `None`, so a false ensures proves for `T | null | undefined`

Open
#304 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript

Research direction

Start by reading the TypeScript-to-Dafny translation for union types and strict equality, using the lsc check --backend=dafny examples in the issue to reproduce both failures. The issue names no source files or tests, so locate the relevant translation code and its tests before changing anything. Done means strict checks distinguish null from undefined for T | null | undefined, and the shown incorrect proof and payload comparison are handled soundly.

Written by the indexing model from the issue text.

Description

Version: lsc 0.6.10 (npm), Dafny 4.11.

A type string | null | undefined is modelled as Option<string>, and both x === null and x === undefined become x == None. The model can't tell them apart. A contract that tells them apart proves things that are false in JavaScript.

//@ backend dafny
export function label(x: string | null | undefined): string {
  //@ verify
  //@ ensures x === null ==> \result === "unset"
  return x === undefined ? "unset" : "set"
}

lsc check --backend=dafny i2.ts emits the following and verifies it (1 verified, 0 errors):

function label_(x: Option<string>): string
{
  match x {
    case Some(i_x_val) => "set"
    case None => "unset"
  }
}

lemma label_ensures(x: Option<string>)
  ensures ((match x { case Some(i_) => false case None => true }) ==> (label_(x) == "unset"))

In JavaScript, label(null) is "set", so the proven ensures is false.

A body that tests for both values doesn't resolve. After the first test narrows x to Some, the second is emitted against the payload:

//@ backend dafny
export function kind(x: string | null | undefined): number {
  //@ verify
  return x === null ? 1 : x === undefined ? 2 : 3
}
  match x {
    case Some(i_x_val) =>
      if (i_x_val == None) then 2 else 3
    case None => 1
  }
i2b.dfy(9,18): Error: arguments must have comparable types (got string and Option<?>)

In JavaScript, null === undefined is false. Only loose == treats them as the same.

Suggested direction: when a type holds both null and undefined, either reject it with a message, or model the two as different values (for example a three-constructor datatype or Option<Option<T>>). A strict === null and a strict === undefined would then be different tests. Folding the two is sound only when the type holds just one of them, or when every test is loose (== null).

Workaround: in verified code, use T | undefined or T | null, not both. Convert at the boundary, e.g. x ?? undefined.

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.