NL2xxx: the type checker

The 796 codes of band 2. Diagnostics says what a code promises and how a rule's fragment is matched.

NL2001

The message of an error contains:

… (an override keeps the signature; there is no overloading)

Retired: the overload/override rule the inheritance work removed

NL2002

The message of an error contains:

… (array length is read-only; use `push`)

NL2003

The message of an error contains:

… (build the parts with template literals first)

NL2004

The message of an error contains:

… (fields must match exactly, in order)

Retired: the implements field-order rule, reworded since

NL2005

The message of an error contains:

… (it has `isOk`, `isErr`, `orReturn`, `unwrapOr` and `expect`)

nl2005_result_method.ts:

// NL2005: An unknown method on a `Result` lists the five it does have.
export const main = (): i32 => {
  const r: Result<i32, string> = Ok(1);
  return r.nope();
};
Unknown method `nope` on Result<i32, string> (it has `isOk`, `isErr`, `orReturn`, `unwrapOr` and `expect`)

NL2006

The message of an error contains:

… (it has `ok`, `value` and `error`)

nl2006_result_property.ts:

// NL2006: An unknown property on a `Result` lists the three it does have.
export const main = (): i32 => {
  const r: Result<i32, string> = Ok(1);
  return r.nope;
};
Unknown property `nope` on Result<i32, string> (it has `ok`, `value` and `error`)

NL2007

The message of an error contains:

… (the element type of …

NL2008

The message of an error contains:

… (use --number-mode f64 or toF64(x))

NL2009

The message of an error contains:

… (use `if` / `else`; only an integer switch lowers to a jump table)

NL2010

The message of an error contains:

… | null` is not supported: only class, interface, array, and string types can be nullable (a scalar has no null value)

NL2011

The message of an error contains:

… are not supported

No program provokes it: Generic interfaces are not supported needs a type parameter, which Phase 0 refuses (NL1054)

NL2012

The message of an error contains:

… argument(s), got …

NL2013

The message of an error contains:

… but its initializer is …

nl2013_field_initializer_type.ts:

// NL2013: A field's literal initializer has to have the field's type.
class Point {
  x: i32 = "one";
}
Field `x` of class `Point` is i32 but its initializer is string

NL2014

The message of an error contains:

… but the discriminant is …

NL2015

The message of an error contains:

… cannot be optional (every field has a fixed slot)

nl2015_optional_field.ts:

// NL2015: Every field has a fixed slot, so `?` on one is refused rather than ignored.
class Point {
  x?: i32;
}
Field `x` of class `Point` cannot be optional (every field has a fixed slot)

NL2016

The message of an error contains:

… cannot have type void

nl2016_void_field.ts:

// NL2016: A field of type void has no slot to lay out.
class Point {
  x: void;
}
Field `x` of class `Point` cannot have type void

NL2017

The message of an error contains:

… clashes with method `

Retired: a field clashing with a method, reworded since

NL2018

The message of an error contains:

… must be named

nl2018_anonymous_class.ts:

// NL2018: A class with no name is refused before the `export default` rule can fire.
export default class {
  x: i32 = 0;
}
Classes must be named

NL2019

The message of an error contains:

… needs a type annotation

No program provokes it: shadowed by NL2095, the same words with the closing backtick before them

NL2020

The message of an error contains:

… produces no value and can only be used as a statement

nl2020_expect_as_value.ts:

// NL2020: A `Result<void, E>` method answers nothing, so using its call as a value is refused.
const work = (): Result<void, string> => Ok();

export const main = (): i32 => {
  const r = work();
  const x: i32 = r.expect("boom");
  return 0;
};
`expect()` on Result<void, string> produces no value and can only be used as a statement

NL2021

The message of an error contains:

… to an element of …

NL2022

The message of an error contains:

… values; build it with `[]` and `push` instead

NL2023

The message of an error contains:

…; convert the error first: `if (r.isErr()) { return Err(...); }`

NL2024

The message of an error contains:

…; declare the type as `

NL2025

The message of an error contains:

…; if (r.isErr()) { ... }`), propagate it with `.orReturn()`, or end on it with `.expect(message)`

NL2026

The message of an error contains:

…; there is no top-level code to build anything else

NL2027

The message of an error contains:

…: `static` members are not supported (use a top-level function or const)

NL2028

The message of an error contains:

…: a `Result` is immutable once built (return a new `ok(...)` or `err(...)` instead)

Retired: the lower-case ok(...)/err(...) spelling of the immutable-Result rule

NL2029

The message of an error contains:

…: definite-assignment assertions (`!`) are not supported

nl2029_definite_assignment.ts:

// NL2029: `x!: T` promises an assignment the checker cannot see; a field is initialized or it is not.
class Point {
  x!: i32;
}
Field `x` of class `Point`: definite-assignment assertions (`!`) are not supported

NL2030

The message of an error contains:

…: initializers must be literals (assign other values in the constructor)

nl2030_field_initializer_call.ts:

// NL2030: A field initializer is a literal; anything computed belongs in the constructor.
const one = (): i32 => 1;

class Point {
  x: i32 = one();
}
Field `x` of class `Point`: initializers must be literals (assign other values in the constructor)

NL2031

The message of an error contains:

…: unsupported modifier `

No program provokes it: shadowed by NL2118, the same words with the colon before them

NL2032

The message of an error contains:

….isErr()`, or use `.orReturn()`, `.unwrapOr(v)` or `.expect(message)`

NL2033

The message of an error contains:

…"`) are not supported; modules have no top-level code

NL2034

The message of an error contains:

…(...)` is a `Result<T, E>`, not …

nl2034_ok_without_result.ts:

// NL2034: `Ok(...)` in a position whose contextual type is not a `Result` names what it is instead.
export const main = (): i32 => {
  const n: i32 = Ok(1);
  return n;
};
`Ok(...)` is a `Result<T, E>`, not i32

NL2035

The message of an error contains:

…(...)` needs a contextual `Result<T, E>` type (annotate the function's return type, e.g. `function f(): Result<number, string>`)

NL2036

The message of an error contains:

…()` in the constructor of `…

nl2036_method_call_in_constructor.ts:

// NL2036: A method called in the constructor could read a field that is not assigned yet.
class Point {
  x: i32;
  constructor() {
    this.scale();
    this.x = 0;
  }
  scale(): void {}
}
Cannot call `this.scale()` in the constructor of `Point` before field `x` is assigned

NL2037

The message of an error contains:

…); handle it here instead: `if (r.isErr()) { ... }`

NL2038

The message of an error contains:

…` (Phase 1 supports number, i32, i64, u8, u16, u32, u64, f32, f64, boolean, string, void)

nl2038_keyof_type.ts:

// NL2038: a type operator with no rule of its own gets the generic "unsupported
// type" sentence, which is the one that lists the set Phase 1 does have.
interface Point {
  x: i32;
}

export const main = (): i32 => {
  const k: keyof Point = 1;
  return 0;
};
Unsupported type `keyof Point` (Phase 1 supports number, i32, i64, u8, u16, u32, u64, f32, f64, boolean, string, void)

NL2039

The message of an error contains:

` (supported: number, i32, i64, u8, u16, u32, u64, f32, f64, boolean, string, void, T[], Result<T, E>, Int32Array/Float64Array/BigInt64Array, and declared classes/interfaces)

NL2040

The message of an error contains:

` (the base's constructor and methods are part of `

Retired: inheritance, removed

NL2041

The message of an error contains:

` accepts string, number, or boolean, got …

NL2042

The message of an error contains:

` allocates a dynamically sized array on every iteration of this loop and nothing keeps it …

Retired: the long form of the allocation performance warning; the live one is NL9001

NL2043

The message of an error contains:

` because it is a module constant

NL2044

The message of an error contains:

` before the error is handled; …

NL2045

The message of an error contains:

` but not exported (add `export`)

NL2046

The message of an error contains:

…` cannot be optional

nl2046_optional_method.ts:

// NL2046: A method is either declared or it is not; `m?()` has no vtable slot to leave empty.
class Point {
  x: i32 = 0;
  m?(): void {}
}
Method `m` of class `Point` cannot be optional

NL2047

The message of an error contains:

` cannot be used before `super(...)` in the constructor of `

Retired: inheritance, removed

NL2048

The message of an error contains:

…` cannot declare methods (interfaces describe layout only)

nl2048_interface_method.ts:

// NL2048: An interface describes a layout, so a method signature in one has nothing to lay out.
interface Shape {
  area(): i32;
}
Interface `Shape` cannot declare methods (interfaces describe layout only)

NL2049

The message of an error contains:

` cannot extend interface `

Retired: inheritance, removed

NL2050

The message of an error contains:

` cannot extend itself

Retired: inheritance, removed

NL2051

The message of an error contains:

` clashes with field `

Retired: inheritance, removed

NL2052

The message of an error contains:

` does not fit in …

NL2053

The message of an error contains:

` does not fit in i32

NL2054

The message of an error contains:

…` exceeds 2^53 and cannot be written exactly (the parser already rounded it); compute the …

nl2054_literal_u64.ts:

// NL2054: An unsigned literal past 2^53 was already rounded by the parser, and the message names the width.
export const main = (): i32 => {
  const x: u64 = 18014398509481985;
  return 0;
};
Literal `18014398509481985` exceeds 2^53 and cannot be written exactly (the parser already rounded it); compute the u64 value instead

NL2055

The message of an error contains:

…` exceeds 2^53 and cannot be written exactly (the parser already rounded it); compute the i64 value instead

nl2055_literal_i64.ts:

// NL2055: The i64 half of the 2^53 rule, which has a sentence of its own because the width is fixed.
export const main = (): i32 => {
  const x: i64 = 18014398509481985;
  return 0;
};
Literal `18014398509481985` exceeds 2^53 and cannot be written exactly (the parser already rounded it); compute the i64 value instead

NL2056

The message of an error contains:

…` expects 0 argument(s) (the class has no constructor), got …

nl2056_new_without_constructor.ts:

// NL2056: a class with no constructor takes no arguments at `new`, and the
// message says so rather than reporting an arity mismatch against nothing.
class Point {
  x: i32 = 0;
}

export const main = (): i32 => {
  const p = new Point(1);
  return p.x;
};
`new Point` expects 0 argument(s) (the class has no constructor), got 1

NL2057

The message of an error contains:

` expects a number (i32, i64, u8, u16, u32, u64, f32, or f64), got …

NL2058

The message of an error contains:

…` expects a number index, got …

nl2058_charcodeat_index.ts:

// NL2058: A string index is a number; the message names the method it belongs to.
export const main = (): i32 => {
  return "abc".charCodeAt("x");
};
`charCodeAt` expects a number index, got string

NL2059

The message of an error contains:

` expects a string, got …

NL2060

The message of an error contains:

` expects exactly …

NL2061

The message of an error contains:

` expects exactly 1 argument, got …

NL2062

The message of an error contains:

…` expects f64, got …

nl2062_math_sqrt_arg.ts:

// NL2062: The f64-only `Math` builtins name the type they want rather than the type they got alone.
export const main = (): i32 => {
  const x: f64 = Math.sqrt("a");
  return 0;
};
`Math.sqrt` expects f64, got string

NL2063

The message of an error contains:

…` has more than one constructor (no overloads)

nl2063_two_constructors.ts:

// NL2063: There is no overloading, so a second constructor is refused rather than merged.
class Point {
  x: i32;
  constructor() {
    this.x = 0;
  }
  constructor(x: i32) {
    this.x = x;
  }
}
Class `Point` has more than one constructor (no overloads)

NL2064

The message of an error contains:

…` has no `value`: its success arm carries nothing

nl2064_result_void_value.ts:

// NL2064: `Result<void, E>` carries nothing on success, so `.value` has nothing to read.
const work = (): Result<void, string> => Ok();

export const main = (): i32 => {
  const r = work();
  if (r.isOk()) {
    const v = r.value;
  }
  return 0;
};
`Result<void, string>` has no `value`: its success arm carries nothing

NL2065

The message of an error contains:

` has no field `

NL2066

The message of an error contains:

` has no initializer and no constructor assigns it

NL2067

The message of an error contains:

` here: it only holds an error where the call failed; …

NL2068

The message of an error contains:

…` in a constant of integer type (annotate it `f64`)

nl2068_non_integer_constant.ts:

// NL2068: A module constant of integer type folds integers; the message names the annotation that would work.
const RATIO: i32 = 1.5;
Non-integer literal `1.5` in a constant of integer type (annotate it `f64`)

NL2069

The message of an error contains:

…` in a constant requires boolean operands

nl2069_constant_boolean_operands.ts:

// NL2069: `&&` in a constant folds booleans, and the message says so rather than reporting a type mismatch.
const FLAG: boolean = 1 && 2;
Operator `&&` in a constant requires boolean operands

NL2070

The message of an error contains:

` in a constant requires two operands of the same type, got …

NL2071

The message of an error contains:

` in i32 number mode (use --number-mode f64)

NL2072

The message of an error contains:

…` is a constant imported from `…

nl2072_constant_as_type.ts:

// NL2072: An imported constant used where a type is expected names the module it came from.
import { LIMIT } from "./wordings_lib";

const use = (x: LIMIT): i32 => 1;

export const main = (): i32 => 0;
`LIMIT` is a constant imported from `./wordings_lib`, not a type

NL2073

The message of an error contains:

…` is a function imported from `…

nl2073_function_as_type.ts:

// NL2073: An imported function used where a type is expected names the module it came from.
import { one } from "./wordings_lib";

const use = (x: one): i32 => 1;

export const main = (): i32 => 0;
`one` is a function imported from `./wordings_lib`, not a type

NL2074

The message of an error contains:

…` is already declared as a class or interface

nl2074_function_after_class.ts:

// NL2074: A function whose name a class already took is refused by the function's own rule.
class Shape {
  side: i32 = 0;
}

const Shape = (): void => {};
`Shape` is already declared as a class or interface

NL2075

The message of an error contains:

` is already declared as a function

No program provokes it: a class name is registered before any function signature, so NL2074 is the one that fires

NL2076

The message of an error contains:

` is already declared in this module

NL2077

The message of an error contains:

` is defined in terms of itself

NL2078

The message of an error contains:

` is missing field `

NL2079

The message of an error contains:

…` is not a declared interface

nl2079_implements_class.ts:

// NL2079: `implements` takes an interface; naming a class is refused with the name it was given.
class Base {
  x: i32 = 0;
}

class Point implements Base {
  x: i32 = 0;
}
`Base` is not a declared interface

NL2080

The message of an error contains:

` is not a module constant, and a module constant's value must be known at compile time

NL2081

The message of an error contains:

` is not available on …

NL2082

The message of an error contains:

` is not definitely assigned in the constructor

NL2083

The message of an error contains:

…` is not supported

nl2083_declare_interface.ts:

// NL2083: `declare interface` describes a type the compiler has no layout for;
// a plain `declare class` is a C struct layout (NL2740 and on).
declare interface Point {
  x: i32;
}
`declare interface` is not supported

NL2084

The message of an error contains:

` is not supported; a module has no top-level code, so only `const` is available

NL2085

The message of an error contains:

` is not supported: inherited fields are read and written as `this.

Retired: inheritance, removed

NL2086

The message of an error contains:

` is read before it is assigned in the constructor of `

NL2087

The message of an error contains:

` is rebuilt from its own value on every iteration of this loop, so every pass copies all …

Retired: the long form of the string performance warning; the live one is NL9002

NL2088

The message of an error contains:

…` is set twice in the object literal

nl2088_field_set_twice.ts:

// NL2088: An object literal sets every field exactly once, so a repeat is an error rather than a last-wins.
interface Point {
  x: i32;
}

export const main = (): i32 => {
  const p: Point = { x: 1, x: 2 };
  return p.x;
};
Field `x` is set twice in the object literal

NL2089

The message of an error contains:

…` must be initialized

nl2089_uninitialized_local.ts:

// NL2089: A local has no undefined state to start in, so it is initialized where it is declared.
export const main = (): i32 => {
  let x: i32;
  return 0;
};
Variable `x` must be initialized

NL2090

The message of an error contains:

…` must have a body

nl2090_method_without_body.ts:

// NL2090: A method signature with no body is an overload declaration, which the language does not have.
class Point {
  x: i32 = 0;
  m(): void;
}
Method `m` of class `Point` must have a body

NL2091

The message of an error contains:

…` must have a body (no overload signatures)

nl2091_constructor_without_body.ts:

// NL2091: The constructor's half of the no-overloads rule, which has its own sentence.
class Point {
  x: i32 = 0;
  constructor();
}
Constructor of class `Point` must have a body (no overload signatures)

NL2092

The message of an error contains:

…` must have plain identifier names

nl2092_string_member_name.ts:

// NL2092: A member name is an identifier, because the name is the offset.
class Point {
  "x": i32 = 0;
}
Members of `Point` must have plain identifier names

NL2093

The message of an error contains:

` must return a value of type …

NL2094

The message of an error contains:

` must start with `super(...)`: the constructor of `

Retired: inheritance, removed

NL2095

The message of an error contains:

` needs a type annotation

NL2096

The message of an error contains:

` needs an explicit return type annotation

NL2097

The message of an error contains:

…` needs an initialiser

nl2097_constant_without_initialiser.ts:

// NL2097: A module constant is its value, so there is nothing to declare without one.
const LIMIT: i32;
Module constant `LIMIT` needs an initialiser

NL2098

The message of an error contains:

…` requires a numeric variable, got …

nl2098_increment_string.ts:

// NL2098: `++` needs a numeric variable, and the message names the type it found.
export const main = (): i32 => {
  let s: string = "a";
  s++;
  return 0;
};
Operator `++` requires a numeric variable, got string

NL2099

The message of an error contains:

` requires boolean operands, got …

NL2100

The message of an error contains:

` requires two …

NL2101

The message of an error contains:

` requires two operands of the same integer type, got …

NL2102

The message of an error contains:

` requires two operands of the same numeric type, got …

NL2103

The message of an error contains:

…` requires two operands of the same type, got …

nl2103_equality_mixed.ts:

// NL2103: `===` compares two values of one type; the message names both.
export const main = (): i32 => {
  const b: boolean = "a" === 1;
  return 0;
};
Operator `===` requires two operands of the same type, got string and i32

nl2103_host_handle_other_kind_equality.ts:

// A handle of one kind does not compare with a handle of another.
type Canvas = HostHandle<"canvas">
type Socket = HostHandle<"socket">
export const f = (c: Canvas, s: Socket): boolean => {
  return c === s
};
Operator `===` requires two operands of the same type, got HostHandle<"canvas"> and HostHandle<"socket">

NL2104

The message of an error contains:

…` returns before field `…

nl2104_constructor_returns_early.ts:

// NL2104: A constructor that returns early leaves a field unassigned, and the message names the field.
class Point {
  x: i32;
  constructor() {
    return;
  }
}
Constructor of `Point` returns before field `x` is assigned

NL2105

The message of an error contains:

` returns void and can only be used as a statement

NL2106

The message of an error contains:

` takes no type argument (it is `new Array<…

NL2107

The message of an error contains:

` takes no type argument (it is an alias of `

NL2108

The message of an error contains:

` values; compare each with `null` and then compare the narrowed values

NL2109

The message of an error contains:

` when checking a single module

No program provokes it: the single-module check() path is not the driver's; the loader reports NL3006 instead

NL2110

The message of an error contains:

`, which already extends `

Retired: inheritance, removed

NL2111

The message of an error contains:

`, which does not extend a class

Retired: inheritance, removed

NL2112

The message of an error contains:

`; a derived class cannot redeclare or shadow an inherited field

Retired: inheritance, removed

NL2113

The message of an error contains:

…`; check for null first: `if (a !== null) { ... }`

nl2113_index_nullable.ts:

// NL2113: Indexing a nullable array needs the null check first, and the message writes it out.
export const main = (): i32 => {
  const xs: i32[] | null = null;
  return xs[0];
};
Cannot index `i32[] | null`; check for null first: `if (a !== null) { ... }`

NL2114

The message of an error contains:

…`; check the nullable side against `null` first, then compare the narrowed values

nl2114_compare_nullable.ts:

// NL2114: A nullable operand compares with `null` and nothing else; the message says what to do instead.
export const main = (): i32 => {
  const s: string | null = null;
  const b: boolean = s === "a";
  return 0;
};
Cannot compare string | null with string using `===`; check the nullable side against `null` first, then compare the narrowed values

NL2115

The message of an error contains:

`; use an object literal: `{ ... }`

NL2116

The message of an error contains:

…`: `static` members are not supported (use a top-level function)

nl2116_static_method.ts:

// NL2116: There are no static members; the message names the top-level function that replaces one.
// The code covers two sentences — this one and `Constructor of class `C`: ...` — so a case here
// proves nothing about the constructor: `tests/cases/reject_cls_ctor_static` is what pins that.
class Point {
  x: i32 = 0;
  static origin(): i32 {
    return 0;
  }
}
Method `origin` of class `Point`: `static` members are not supported (use a top-level function)

NL2117

The message of an error contains:

…`: expected …

nl2117_host_handle_other_kind.ts:

// A handle of one kind is not a handle of another, as an argument.
type Canvas = HostHandle<"canvas">
type Socket = HostHandle<"socket">
const draw = (c: Canvas): i32 => {
  return 1
};
export const f = (s: Socket): i32 => {
  return draw(s)
};
Argument 1 of `draw`: expected HostHandle<"canvas">, got HostHandle<"socket">

NL2118

The message of an error contains:

`: unsupported modifier `

nl2118_override_method.ts:

// NL2118: A modifier the checker has no rule for is named in the message rather than ignored.
class Point {
  x: i32 = 0;
  override scale(): void {}
}
syntax error: a type annotation is required

NL2119

The message of an error contains:

`) are not supported; import functions by name

NL2120

The message of an error contains:

`): the type name is part of the ABI

NL2121

The message of an error contains:

`Array` needs exactly one type argument, e.g. `Array<number>`

nl2121_array_type_arguments.ts:

// NL2121: `Array<T>` takes one type argument, and the message shows the spelling that works.
export const main = (): i32 => {
  const xs: Array<i32, i32> = [];
  return 0;
};
`Array` needs exactly one type argument, e.g. `Array<number>`

NL2122

The message of an error contains:

`break` outside of a loop or `switch`

NL2123

The message of an error contains:

`case` label must be an integer literal or a module constant (LLVM's `switch` table holds constants)

Retired: the case label rule from before an enum member was a label; the live one is NL2284

NL2124

The message of an error contains:

`continue` outside of a loop

NL2125

The message of an error contains:

`declare function` is not supported yet

Retired: declare function is a supported construct now, so "not supported yet" names nothing; its wordings case went with it and the rules that replaced it are pinned by tests/cases/reject_ffi_*

NL2126

The message of an error contains:

`Err(...)` expects …

nl2126_err_payload.ts:

// NL2126: `Err(...)` carries the error arm's type, and the message names both types.
export const main = (): i32 => {
  const r: Result<i32, string> = Err(1);
  return 0;
};
`Err(...)` expects string for Result<i32, string>, got i32

NL2127

The message of an error contains:

`expect(...)` expects string, got …

nl2127_expect_argument.ts:

// NL2127: `expect(...)` takes the message it panics with, which is a string.
export const main = (): i32 => {
  const r: Result<i32, string> = Ok(1);
  return r.expect(1);
};
`expect(...)` expects string, got i32

NL2128

The message of an error contains:

`export { ... }` / `export * from` are not supported; put `export` on the function declaration itself

nl2128_export_list.ts:

// NL2128: An export list is refused with the spelling that replaces it.
const one = (): i32 => 1;

export { one };
`export { ... }` / `export * from` are not supported; put `export` on the function declaration itself

NL2129

The message of an error contains:

`export default` / `export =` are not supported; use a named `export function`

nl2129_export_default_value.ts:

// NL2129: `export default` and `export =` are refused together, naming the named export instead.
export default 1;
`export default` / `export =` are not supported; use a named `export function`

NL2130

The message of an error contains:

`export default` is not supported; use a named `export function`

NL2131

The message of an error contains:

`export default` is not supported; use a named `export`

nl2131_export_default_class.ts:

// NL2131: A default-exported class gets the shorter sentence, because the class is already named.
export default class Point {
  x: i32 = 0;
}
`export default` is not supported; use a named `export`

NL2132

The message of an error contains:

`extends` must name exactly one class declared in this module

Retired: inheritance, removed

NL2133

The message of an error contains:

`for await` is not supported…

nl2133_for_await.ts:

// NL2133: a stream of promises would hold each as a value, so `for await` is refused at the modifier rather than at the iterable.
export const main = (): i32 => {
  const xs: i32[] = [1];
  for await (const x of xs) {
    return x;
  }
  return 0;
};
`for await` is not supported here: a stream of promises would hold each as a value — `await` each call inside a plain `for` loop

NL2134

The message of an error contains:

`for...of` declares exactly one variable

nl2134_for_of_two_variables.ts:

// NL2134: `for...of` binds one name, so a second declaration is refused before the element type is read.
export const main = (): i32 => {
  const xs: i32[] = [1];
  for (const a = 1, b = 2 of xs) {
    return a;
  }
  return 0;
};
`for...of` declares exactly one variable

NL2135

The message of an error contains:

`for...of` needs a `const` or `let` declaration, e.g. `for (const x of xs)`

nl2135_for_of_without_declaration.ts:

// NL2135: `for (x of xs)` over an existing variable is refused with the declaration form spelled out.
export const main = (): i32 => {
  const xs: i32[] = [1];
  let x: i32 = 0;
  for (x of xs) {
    return x;
  }
  return 0;
};
`for...of` needs a `const` or `let` declaration, e.g. `for (const x of xs)`

NL2136

The message of an error contains:

`for...of` requires an array, got …

NL2137

The message of an error contains:

`implements` must name a declared interface

nl2137_implements_expression.ts:

// NL2137: `implements` takes a name, not an expression, and says so before resolving anything.
export const main = (): i32 => 0;

class Point implements a.B {
  x: i32 = 0;
}
`implements` must name a declared interface

NL2138

The message of an error contains:

`join` expects 0 or 1 arguments, got …

nl2138_join_arity.ts:

// NL2138: `join` takes at most a separator; the message names how many arguments arrived.
export const main = (): i32 => {
  const xs: string[] = ["a"];
  const s: string = xs.join(",", ";");
  return 0;
};
`join` expects 0 or 1 arguments, got 2

NL2139

The message of an error contains:

`main` cannot take parameters (command-line arguments are not supported yet)

NL2140

The message of an error contains:

`main` must return void or an i32 number (the process exit code), not …

nl2140_main_return_type.ts:

// NL2140: `main` returns the process exit code, so the message names what a return type may be.
export const main = (): string => {
  return "a";
};
`main` must return void or an i32 number (the process exit code), not string

NL2141

The message of an error contains:

`Math.abs` expects a number, got …

nl2141_math_abs_arg.ts:

// NL2141: `Math.abs` is the one `Math` builtin that takes any numeric width, and its message says `a number`.
export const main = (): i32 => {
  const x: i32 = Math.abs("a");
  return 0;
};
`Math.abs` expects a number, got string

NL2142

The message of an error contains:

`new Array` needs exactly one type argument, e.g. `new Array<number>(n)`

nl2142_new_array_type_arguments.ts:

// NL2142: `new Array(n)` has no element type to lay out, and the message shows the spelling that does.
export const main = (): i32 => {
  const xs = new Array(3);
  return 0;
};
`new Array` needs exactly one type argument, e.g. `new Array<number>(n)`

NL2143

The message of an error contains:

`new Array<T>(n)` expects exactly 1 argument (the length), got …

nl2143_new_array_arity.ts:

// NL2143: `new Array<T>(n)` takes the length and nothing else.
export const main = (): i32 => {
  const xs = new Array<i32>(1, 2);
  return 0;
};
`new Array<T>(n)` expects exactly 1 argument (the length), got 2

NL2144

The message of an error contains:

`new` requires a class name

nl2144_new_non_identifier.ts:

// NL2144: `new` takes a class name, so a qualified expression is refused before it is resolved.
export const main = (): i32 => {
  const p = new a.B();
  return 0;
};
`new` requires a class name

NL2145

The message of an error contains:

`null` needs a contextual `T | null` type (annotate the variable, e.g. `let p: P | null = null`)

nl2145_bare_null.ts:

// NL2145: `null` takes its type from context, and the message shows the annotation that gives it one.
export const main = (): i32 => {
  const p = null;
  return 0;
};
`null` needs a contextual `T | null` type (annotate the variable, e.g. `let p: P | null = null`)

NL2146

The message of an error contains:

`Number` expects a string, number, or boolean, got …

NL2147

The message of an error contains:

`Ok(...)` expects …

NL2148

The message of an error contains:

`process.argv` is read-only

NL2149

The message of an error contains:

`process.argv` requires a `main` entry point (this program has no `export function main`)

NL2150

The message of an error contains:

`Result` needs exactly two type arguments, e.g. `Result<number, string>`

NL2151

The message of an error contains:

`Result<T, E> | null` is not supported: a `Result` already models absence through its error arm

NL2152

The message of an error contains:

`Result<T, void>` is not supported: an error must carry a value (use `Result<T, string>`)

nl2152_result_void_error.ts:

// NL2152: An error arm carries a value; the message names the `Result<T, string>` that people want.
export const main = (): i32 => {
  const r: Result<i32, void> = Ok(1);
  return 0;
};
`Result<T, void>` is not supported: an error must carry a value (use `Result<T, string>`)

NL2153

The message of an error contains:

`substring` expects 1 or 2 arguments, got …

NL2154

The message of an error contains:

`super(...)` in the constructor of `

Retired: super(...), removed with inheritance

NL2155

The message of an error contains:

`super(...)` is only valid as the first statement of the constructor of a class that `extends` another class

Retired: super(...), removed with inheritance

NL2156

The message of an error contains:

`super(...)` must be the first statement of the constructor of `

Retired: super(...), removed with inheritance

NL2157

The message of an error contains:

`super` can only be used as `super.method(...)`, or as `super(...)` at the start of a constructor

Retired: super, removed with inheritance

NL2158

The message of an error contains:

`super` expects 0 argument(s) (`

Retired: super, removed with inheritance

NL2159

The message of an error contains:

`super` is only valid inside a method or constructor of a class that `extends` another class

Retired: super, removed with inheritance

NL2160

The message of an error contains:

`switch` has more than one `default` clause

nl2160_two_defaults.ts:

// NL2160: A second `default` clause is refused rather than being unreachable code.
export const main = (): i32 => {
  const n: i32 = 1;
  switch (n) {
    default:
      return 1;
    default:
      return 2;
  }
};
`switch` has more than one `default` clause

NL2161

The message of an error contains:

`this` cannot be used as a value in the constructor of `…

nl2161_this_as_value_in_constructor.ts:

// NL2161: `this` escaping the constructor before every field is assigned hands out a half-built struct.
class Point {
  x: i32;
  constructor() {
    use(this);
    this.x = 0;
  }
}

const use = (p: Point): void => {};
`this` cannot be used as a value in the constructor of `Point` before field `x` is assigned

NL2162

The message of an error contains:

`this` is only valid inside a method or constructor

NL2163

The message of an error contains:

`unwrapOr(...)` expects …

NL2164

The message of an error contains:

`unwrapOr(...)` needs a success value to fall back to, and `…

nl2164_unwrap_or_void.ts:

// NL2164: `unwrapOr` needs a success value to fall back to, and the message names the branch that works instead.
const work = (): Result<void, string> => Ok();

export const main = (): i32 => {
  const r = work();
  const v = r.unwrapOr(0);
  return 0;
};
`unwrapOr(...)` needs a success value to fall back to, and `Result<void, string>` has none; use `if (r.isErr()) { ... }`

NL2165

The message of an error contains:

A `case` clause cannot declare a variable directly; wrap the clause body in a block (`case 1: { ... }`)

NL2166

The message of an error contains:

A `case` clause with statements must end in `break`, `return`, `continue` or `process.exit` (…

Retired: a case clause had to end in break, return, continue or process.exit; a clause falls into the next as in TypeScript now (tests/cases/cf_switch_fallthrough)

NL2167

The message of an error contains:

A module constant's initialiser must be a literal, another constant, or arithmetic over them

NL2168

The message of an error contains:

Abstract classes are not supported

NL2169

The message of an error contains:

Array elements cannot be void

nl2169_void_array_element.ts:

// NL2169: An array of void has no element size.
const nothing = (): void => {};

export const main = (): i32 => {
  const xs = [nothing()];
  return 0;
};
Array elements cannot be void

NL2170

The message of an error contains:

Array index must be a number, got …

NL2171

The message of an error contains:

Array length must be a number, got …

nl2171_array_length_type.ts:

// NL2171: `new Array<T>(n)` takes a numeric length, and the message names the type it got.
export const main = (): i32 => {
  const xs = new Array<i32>("a");
  return 0;
};
Array length must be a number, got string

NL2172

The message of an error contains:

Array literal elements must all have the same type, got …

NL2173

The message of an error contains:

…async functions are not supported

Retired: async functions are not supported, the first compiler's refusal, shadowed by NL1015 until async was allowed

NL2174

The message of an error contains:

…async methods are not supported

Retired: async methods are not supported, the first compiler's refusal; a method is still refused, by NL1063

NL2175

The message of an error contains:

…attempt to compute with overflow in a constant: the result does not fit in …

NL2176

The message of an error contains:

…attempt to divide by zero in a constant

NL2177

The message of an error contains:

attempt to divide with overflow in a constant

nl2177_constant_divide_overflow.ts:

// NL2177: `MIN / -1` is refused while folding a constant, the same failure the emitted divisor check catches.
const X: i32 = -2147483648 / -1;
attempt to divide with overflow in a constant

NL2178

The message of an error contains:

Cannot assign …

nl2178_field_assign_type.ts:

// NL2178: assigning the wrong type to a field names both types, where the
// nearer rules (readonly, `length`, a receiver that is not a struct) do not
// apply and the general sentence is the one that fires.
class Point {
  x: i32 = 0;
}

export const main = (): i32 => {
  const p = new Point();
  p.x = "one";
  return p.x;
};
Cannot assign string to i32 field `x`

nl2178_host_handle_assign_number.ts:

// A handle variable holds a handle, and a number is not one.
type Canvas = HostHandle<"canvas">
type Socket = HostHandle<"socket">
export const f = (): Canvas => {
  let c: Canvas = 0
  c = 3
  return c
};
Cannot assign i32 to HostHandle<"canvas"> variable `c`

NL2179

The message of an error contains:

Cannot assign to `

NL2180

The message of an error contains:

Cannot assign to property `…

nl2180_assign_string_property.ts:

// NL2180: A property assignment on a value that is not a struct names the type it was.
export const main = (): i32 => {
  const s: string = "a";
  s.size = 1;
  return 0;
};
Cannot assign to property `size` of string

NL2181

The message of an error contains:

Cannot assign to readonly field `

NL2182

The message of an error contains:

Cannot call `…

nl2182_call_on_nullable.ts:

// NL2182: A method call on a nullable receiver is refused with the narrowing that would allow it.
class Point {
  x: i32 = 0;
  scale(): void {}
}

export const main = (): i32 => {
  const p: Point | null = null;
  p.scale();
  return 0;
};
Cannot call `scale` on `Point | null`; check for null first: `if (p !== null) { ... }` narrows `Point | null` to `Point`

NL2183

The message of an error contains:

Cannot declare a variable of type void

nl2183_void_local.ts:

// NL2183: A local of type void has no slot; the call is a statement rather than a value.
const nothing = (): void => {};

export const main = (): i32 => {
  const x: void = nothing();
  return 0;
};
Cannot declare a variable of type void

NL2184

The message of an error contains:

Cannot index a value of type …

NL2185

The message of an error contains:

Cannot initialize …

NL2186

The message of an error contains:

Cannot push …

NL2187

The message of an error contains:

Cannot read property `

NL2188

The message of an error contains:

Condition must be boolean, got …

NL2189

The message of an error contains:

Constructors cannot declare a return type

nl2189_constructor_return_type.ts:

// NL2189: A constructor returns the instance, so it declares no return type.
class Point {
  x: i32;
  constructor(): void {
    this.x = 0;
  }
}
Constructors cannot declare a return type

NL2190

The message of an error contains:

Default imports are not supported; use `import { …

NL2191

The message of an error contains:

Destructured constants are not supported

nl2191_destructured_constant.ts:

// NL2191: A module constant is a compile-time value folded into each use, so a pattern has no receiver to read.
const { x } = { x: 1 };
Destructured constants are not supported

NL2192

The message of an error contains:

Destructured parameters are not supported

nl2192_destructured_parameter.ts:

// NL2192: A parameter is one value with one layout, so a destructuring pattern has no slot.
interface Point {
  x: i32;
}

const read = ({ x }: Point): i32 => x;
Destructured parameters are not supported

NL2193

The message of an error contains:

A pattern may destructure an `await` only of …

nl2193_destructuring_local.ts:

// NL2193: a pattern over an `await` other than `awaitAll(...)`'s, whose names
// are the calls' answers, is refused.
const pair = async (): Promise<i32[]> => [1, 2];

export const main = async (): Promise<i32> => {
  const [a, b] = await pair();
  return a + b;
};
A pattern may destructure an `await` only of `awaitAll(...)`, whose names are the calls' answers: await into a name, then destructure the name

NL2194

The message of an error contains:

Duplicate `case` label `

NL2195

The message of an error contains:

Duplicate declaration of `…

nl2195_duplicate_local.ts:

// NL2195: Two locals of one name in one scope have two slots and one reader.
export const main = (): i32 => {
  let x: i32 = 1;
  let x: i32 = 2;
  return x;
};
Duplicate declaration of `x`

NL2196

The message of an error contains:

Duplicate function `…

nl2196_duplicate_function.ts:

// NL2196: There is no overloading, so a repeated function name is refused at the second declaration.
const twice = (): i32 => 1;
const twice = (): i32 => 2;
Duplicate function `twice`

NL2197

The message of an error contains:

Duplicate member `…

nl2197_duplicate_member.ts:

// NL2197: A repeated member name would be two offsets under one name.
class Point {
  x: i32 = 0;
  x: i32 = 1;
}
Duplicate member `x` in class `Point`

NL2198

The message of an error contains:

Duplicate parameter `…

nl2198_duplicate_parameter.ts:

// NL2198: Two parameters of one name leave the body no way to name either.
const add = (a: i32, a: i32): i32 => a;
Duplicate parameter `a`

NL2199

The message of an error contains:

Empty array literal needs a type annotation, e.g. `const xs: number[] = []`

NL2200

The message of an error contains:

Empty import list

nl2200_empty_import_list.ts:

// NL2200: an import that names nothing has nothing to bind, and is refused
// rather than treated as the side-effect import it is not.
import {} from "./wordings_lib";
Empty import list

NL2201

The message of an error contains:

Expected a return value of type …

nl2201_bare_return.ts:

// NL2201: A non-void function returns a value on every path, and the message names the type it wants.
const one = (): i32 => {
  return;
};
Expected a return value of type i32

NL2202

The message of an error contains:

Function names starting with `nish_` are reserved for the runtime

nl2202_reserved_function_name.ts:

// NL2202: `nish_` is the runtime's ABI prefix, so a user function may not take one.
const nish_alloc = (): i32 => 1;
Function names starting with `nish_` are reserved for the runtime

NL2203

The message of an error contains:

Functions must be named

nl2203_anonymous_default_function.ts:

// NL2203: A function is named because the name is the symbol; the default export cannot skip it.
export default function (): void {}
Functions must be named

NL2204

The message of an error contains:

Functions must have a body

nl2204_function_without_body.ts:

// NL2204: A body-less function declaration is an overload signature, which the language does not have.
function ambient(): void;
Functions must have a body

NL2205

The message of an error contains:

Generators are not supported

No program provokes it: generators are Phase 0's (NL1044)

NL2206

The message of an error contains:

Generic classes are not supported

Retired: Generic classes are not supported, the refusal generic classes replaced; new on a class that is not generic is NL2318 now

NL2207

The message of an error contains:

Generic functions are not supported

No program provokes it: generic functions are Phase 0's (NL1054)

NL2208

The message of an error contains:

Generic methods are not supported

Retired: Generic methods are not supported; generic methods compile now

NL2209

The message of an error contains:

Getters and setters are not supported in class `…

nl2209_getter_setter.ts:

// NL2209: A getter runs code where a field read is written, which the language keeps to methods.
class Box {
  value: i32 = 0;
  get size(): i32 {
    return this.value;
  }
}
Getters and setters are not supported in class `Box` (use a method)

NL2210

The message of an error contains:

Holes in array literals are not supported

nl2210_array_hole.ts:

// NL2210: A hole has no value to store, and there is no undefined to store instead.
export const main = (): i32 => {
  const xs: i32[] = [1, , 2];
  return 0;
};
Holes in array literals are not supported

NL2211

The message of an error contains:

Import must name the functions it imports

No program provokes it: no import syntax produces a clause with neither a name nor named bindings

NL2212

The message of an error contains:

Import specifier must be a string literal

nl2212_template_specifier.ts:

// NL2212: An import specifier is read at compile time, so it is a string literal and not an expression.
import { one } from `./wordings_lib`;
Import specifier must be a string literal

NL2213

The message of an error contains:

Index signatures are not supported in class `…

nl2213_class_index_signature.ts:

// NL2213: An index signature is a dynamic key, and a class layout is fixed.
class Table {
  [key: string]: i32;
}
Index signatures are not supported in class `Table` (object layout is fixed)

NL2214

The message of an error contains:

Index signatures are not supported in interface `…

nl2214_interface_index_signature.ts:

// NL2214: The interface half of the same rule, which names the interface rather than the class.
interface Table {
  [key: string]: i32;
}
Index signatures are not supported in interface `Table` (object layout is fixed)

NL2215

The message of an error contains:

Interface inheritance (`extends`) is not supported; list every field

nl2215_interface_extends.ts:

// NL2215: An interface is a layout, so it lists every field rather than inheriting any.
interface Base {
  x: i32;
}

interface Point extends Base {
  y: i32;
}
Interface inheritance (`extends`) is not supported; list every field

NL2216

The message of an error contains:

…internal: array method on non-array

No program provokes it: an internal invariant: the array method table is only reached for an array receiver

NL2217

The message of an error contains:

Labelled `break` is not supported

Retired: the checker's refusal of a labelled break; one compiles now (tests/cases/cf_label_loops)

NL2218

The message of an error contains:

Labelled `continue` is not supported

Retired: the checker's refusal of a labelled continue; one compiles now (tests/cases/cf_label_loops)

NL2219

The message of an error contains:

Module constant `

NL2220

The message of an error contains:

Names starting with `nish_` are reserved for the runtime

nl2220_reserved_class_name.ts:

// NL2220: The `nish_` prefix is reserved for classes and interfaces too, with a sentence of its own.
class nish_Point {
  x: i32 = 0;
}
Names starting with `nish_` are reserved for the runtime

NL2221

The message of an error contains:

Negative literal `-…

NL2222

The message of an error contains:

Non-integer literal `

NL2223

The message of an error contains:

Object literal keys must be plain identifiers

nl2223_string_object_key.ts:

// NL2223: An object literal's keys are identifiers, because the key is the field slot.
interface Point {
  x: i32;
}

export const main = (): i32 => {
  const p: Point = { "x": 1 };
  return p.x;
};
Object literal keys must be plain identifiers

NL2224

The message of an error contains:

Object literal needs a contextual class or interface type (annotate the variable: `const p: P = { ... }`)

NL2225

The message of an error contains:

Only direct calls to named functions are supported

nl2225_parenthesised_callee.ts:

// NL2225: a call goes to a named function or through a function reference.
// A callee computed to anything else, here the `i32` that `one()` returns,
// is not a function.
const one = (): i32 => 1;

export const main = (): i32 => {
  return one()();
};
Only direct calls to named functions are supported

NL2226

The message of an error contains:

Only functions can be exported for now, plus classes and interfaces (found `export` on …

nl2226_export_import_equals.ts:

// NL2226: the catch-all for an exported statement that is neither a function,
// class, interface nor constant. It names the statement's own kind, so the
// message says which one was written. `export import A = require(...)` is what
// reaches it now that the exported enum has a rule of its own (NL2288).
export import A = require("./wordings_lib");
Only functions can be exported for now, plus classes and interfaces (found `export` on ImportEqualsDeclaration)

NL2227

The message of an error contains:

Only relative import specifiers are supported (`./x` or `../x`), got `

Retired: Only relative import specifiers are supported, from before a package name was one of the four legal forms; the live rule is NL2320

NL2228

The message of an error contains:

Only simple variables can be assigned

NL2229

The message of an error contains:

Only the entry module may declare `export function main`

NL2230

The message of an error contains:

Only top-level function declarations are supported in Phase 1 (found …

NL2231

The message of an error contains:

Operator `+` requires two operands of the same numeric type or two strings, got …

nl2231_host_handle_arithmetic.ts:

// A handle names something on the page; it is not a number to count with.
type Canvas = HostHandle<"canvas">
type Socket = HostHandle<"socket">
export const f = (c: Canvas): Canvas => {
  return c + 1
};
Operator `+` requires two operands of the same numeric type or two strings, got HostHandle<"canvas"> and i32

NL2232

The message of an error contains:

Operator `~` requires an integer operand, got …

NL2233

The message of an error contains:

Optional/default parameters are not supported…

nl2233_optional_parameter.ts:

// NL2233: An arrow written as a value gives no parameter a default: only a declaration's own list does.
export const main = (): void => {
  const xs = [1, 2]
  console.log(xs.map((x: i32, i: i32 = 0): i32 => x + i).length)
};
Optional/default parameters are not supported here: only a function, a method with a body or a constructor gives a parameter a default, not an arrow passed as an argument, a function type, an interface or object method, an accessor or a `declare function`

NL2234

The message of an error contains:

Parameter properties (`constructor(public x: number)`) are not supported; declare the field and assign it

nl2234_parameter_property.ts:

// NL2234: A parameter property declares a field in the parameter list; the message says to write both out.
class Point {
  constructor(public x: i32) {}
}
Parameter properties (`constructor(public x: number)`) are not supported; declare the field and assign it

NL2235

The message of an error contains:

Rest parameters are not supported…

nl2235_rest_parameter.ts:

// NL2235: An arrow written as a value ends in no rest: only a declaration's own list does.
export const main = (): void => {
  const xs = [1, 2]
  console.log(xs.map((x: i32, ...r: i32[]): i32 => x + r.length).length)
};
Rest parameters are not supported here: only a function, a method with a body or a constructor ends in one, not an arrow passed as an argument, a function type, an interface or object method, an accessor or a `declare function`

NL2236

The message of an error contains:

Return type mismatch: function returns …

nl2236_return_type_mismatch.ts:

// NL2236: A returned expression has to have the declared type, and the message names both.
const one = (): i32 => {
  return "a";
};
Return type mismatch: function returns i32 but expression is string

NL2237

The message of an error contains:

Spread in array literals is not supported

nl2237_array_spread.ts:

// NL2237: A spread copies an unknown number of elements, which the literal's fixed length cannot express.
export const main = (): i32 => {
  const ys: i32[] = [1];
  const xs: i32[] = [...ys];
  return 0;
};
Spread in array literals is not supported

NL2238

The message of an error contains:

Template literal hole must be string, number, or boolean, got …

nl2238_template_hole_type.ts:

// NL2238: A template hole is formatted, so its type is one of the three the formatter has.
export const main = (): i32 => {
  const xs: i32[] = [1];
  const s: string = `${xs}`;
  return 0;
};
Template literal hole must be string, number, or boolean, got i32[]

NL2239

The message of an error contains:

Ternary branches cannot be void

nl2239_void_ternary.ts:

// NL2239: A ternary is a value, so neither arm may be void.
const nothing = (): void => {};

export const main = (): i32 => {
  const flag: boolean = true;
  const x = flag ? nothing() : nothing();
  return 0;
};
Ternary branches cannot be void

NL2240

The message of an error contains:

Ternary branches must have the same type, got …

NL2241

The message of an error contains:

The `for...of` variable cannot have an initializer

nl2241_for_of_initializer.ts:

// NL2241: The `for...of` variable takes each element, so an initializer would be overwritten.
export const main = (): i32 => {
  const xs: i32[] = [1];
  for (const x = 1 of xs) {
    return x;
  }
  return 0;
};
The `for...of` variable cannot have an initializer

NL2242

The message of an error contains:

The `for...of` variable takes the element type; remove the annotation

nl2242_for_of_annotation.ts:

// NL2242: The `for...of` variable takes the element type, so an annotation is redundant rather than checked.
export const main = (): i32 => {
  const xs: i32[] = [1];
  for (const x: i32 of xs) {
    return x;
  }
  return 0;
};
The `for...of` variable takes the element type; remove the annotation

NL2243

The message of an error contains:

Type-only imports are not supported

nl2243_type_only_import.ts:

// NL2243: There is no erasure step, so a type-only import has nothing to mean.
import type { Point } from "./wordings_lib";
Type-only imports are not supported

NL2244

The message of an error contains:

Unknown builtin `

NL2245

The message of an error contains:

Unknown class `…

nl2245_unknown_class.ts:

// NL2245: `new X` where no class `X` is declared names the class it looked for.
export const main = (): i32 => {
  const p = new Missing();
  return 0;
};
Unknown class `Missing`

NL2246

The message of an error contains:

Unknown field `

NL2247

The message of an error contains:

Unknown function `…

nl2247_unknown_function.ts:

// NL2247: a call to a name that is no function, builtin or local. It used to
// be provoked only as the cascade after a refused `const alias = double`, which
// #275 silenced, so the rule has a program of its own.
export const main = (): i32 => {
  return nope(1);
};
Unknown function `nope`

NL2248

The message of an error contains:

Unknown identifier `

NL2249

The message of an error contains:

Unknown method `

NL2250

The message of an error contains:

Unknown property `

NL2251

The message of an error contains:

Unreachable code after …

NL2252

The message of an error contains:

Unsupported `new …

No program provokes it: the new dispatch has a * fallback, so the table never misses and NL2245 answers instead

NL2253

The message of an error contains:

Unsupported assignment operator `…

nl2253_field_compound_operator.ts:

// NL2253: A compound assignment to a field takes the five arithmetic operators, and names one that is not.
class Point {
  x: i32 = 0;
}

export const main = (): i32 => {
  const p = new Point();
  p.x **= 2;
  return p.x;
};
Unsupported assignment operator `**=`

NL2254

The message of an error contains:

Unsupported binary operator `…

nl2254_unsupported_binary.ts:

// NL2254: A binary operator with no checker names itself, which is how `**` is refused.
export const main = (): i32 => {
  const x: i32 = 2 ** 3;
  return x;
};
Unsupported binary operator `**`

NL2255

The message of an error contains:

Unsupported class member in `…

nl2255_class_static_block.ts:

// NL2255: A class member kind with no rule of its own names its syntax kind rather than being ignored.
class Point {
  x: i32 = 0;
  static {}
}
Unsupported class member in `Point`: ClassStaticBlockDeclaration

NL2256

The message of an error contains:

Unsupported expression in Phase 1: …

nl2256_host_handle_as_number.ts:

// No cast takes a handle out to a number.
type Canvas = HostHandle<"canvas">
type Socket = HostHandle<"socket">
export const f = (c: Canvas): number => {
  return c as number
};
Unsupported expression in Phase 1: AsExpression

nl2256_host_handle_from_number.ts:

// No cast makes a handle out of a number: the page gives handles out.
type Canvas = HostHandle<"canvas">
type Socket = HostHandle<"socket">
export const f = (): Canvas => {
  return 0 as Canvas
};
Unsupported expression in Phase 1: AsExpression

nl2256_satisfies_expression.ts:

// NL2256: `satisfies` is still in the not-implemented-yet bucket, and reports as a Phase 1 gap.
export const main = (): i32 => {
  const x = 1 satisfies i32;
  return 0;
};
Unsupported expression in Phase 1: SatisfiesExpression

NL2257

The message of an error contains:

Unsupported interface member in `…

nl2257_interface_construct_signature.ts:

// NL2257: The interface half of the unsupported-member rule, which names the interface.
interface Point {
  x: i32;
  new (): Point;
}
Unsupported interface member in `Point`: ConstructSignature

NL2258

The message of an error contains:

Unsupported object literal member: …

nl2258_object_literal_method.ts:

// NL2258: An object literal sets fields; a method in one has no slot to live in.
interface Point {
  x: i32;
}

export const main = (): i32 => {
  const p: Point = {
    x(): i32 {
      return 1;
    },
  };
  return p.x;
};
Unsupported object literal member: MethodDeclaration (only `key: value`)

NL2259

The message of an error contains:

Unsupported operator `…

nl2259_constant_bitwise.ts:

// NL2259: Constant folding is add, subtract, multiply and divide; anything else names itself.
const MASK: i32 = 1 & 3;
Unsupported operator `&` in a constant

NL2260

The message of an error contains:

Unsupported statement in Phase 1: …

nl2260_empty_statement.ts:

// NL2260: A statement kind with no checker names its syntax kind rather than being skipped.
export const main = (): i32 => {
  ;
  return 0;
};
Unsupported statement in Phase 1: EmptyStatement

NL2261

The message of an error contains:

Unsupported unary operator `…

nl2261_constant_complement.ts:

// NL2261: Constant folding has `-` and `!` and nothing else, and names the operator it was handed.
const MASK: i32 = ~1;
Unsupported unary operator `~` on i32 in a constant

NL2262

The message of an error contains:

Unsupported unary operator `-` on …

nl2262_negate_string.ts:

// NL2262: Unary `-` is numeric; the message names the type it was given.
export const main = (): i32 => {
  const s: string = "a";
  const x: i32 = -s;
  return 0;
};
Unsupported unary operator `-` on string

NL2263

The message of an error contains:

Unsupported unary operator `!` on …

nl2263_not_number.ts:

// NL2263: There is no truthiness, so `!` takes a boolean and names what it got.
export const main = (): i32 => {
  const b: boolean = !1;
  return 0;
};
Unsupported unary operator `!` on i32

NL2264

The message of an error contains:

… to write through it)

NL2265

The message of an error contains:

`readonly` is only permitted on an array type, got …

NL2266

The message of an error contains:

`ReadonlyArray` needs exactly one type argument, e.g. `ReadonlyArray<number>`

NL2267

The message of an error contains:

Cannot assign to an element of …

NL2268

The message of an error contains:

` expects an argument of type …

NL2269

The message of an error contains:

…` expects a value of type …

nl2269_host_handle_object_literal.ts:

// An object literal's field forges no handle either.
type Canvas = HostHandle<"canvas">
type Socket = HostHandle<"socket">
interface Slot {
  c: Canvas
}
export const f = (): Canvas => {
  const s: Slot = { c: 7 }
  return s.c
};
Field `c` of `Slot` expects a value of type HostHandle<"canvas">, got i32

NL2270

The message of an error contains:

…: a `Result` is immutable once built (return a new `Ok(...)` or `Err(...)` instead)

NL2271

The message of an error contains:

Unary `+` is forbidden; it converts, and …

NL2272

The message of an error contains:

` must be declared `const`, not `let`

NL2273

The message of an error contains:

` takes its signature from the arrow; drop the annotation on `

NL2274

The message of an error contains:

A function declaration binds one name

nl2274_two_names_one_declaration.ts:

// NL2274: An arrow bound to a `const` declares one function, so a second name in the same declaration is refused.
const one = (): i32 => 1,
  two = (): i32 => 2;
A function declaration binds one name

NL2275

The message of an error contains:

` is a built-in type name and cannot be used for a type alias

NL2276

The message of an error contains:

Type aliases cannot be exported: an alias names a type inside one module (declare it in every module that needs it)

Retired: Type aliases cannot be exported, the refusal exported types replaced; an exported alias is imported now (tests/link/alias_export_class), and importing one that is not exported is NL2045 (tests/link/alias_import_not_exported)

NL2277

The message of an error contains:

… (the interface's fields must be the class's first fields, in order)

NL2278

The message of an error contains:

`extends` is not supported: Nish has no inheritance. Declare the base's fields as the first fields of `

NL2279

The message of an error contains:

`super` is not supported: Nish has no inheritance, so a class has no base class to reach

NL2280

The message of an error contains:

` has no member `

NL2281

The message of an error contains:

` is a built-in type name and cannot be used for an enum

NL2282

The message of an error contains:

` must be an integer literal

NL2283

The message of an error contains:

` must declare at least one member

NL2284

The message of an error contains:

`case` label must be an integer literal, a module constant, or an enum member (LLVM's `switch` table holds constants)

NL2285

The message of an error contains:

`const enum` is not supported: an enum member is already folded to its integer, so `const` would ask for nothing

NL2286

The message of an error contains:

`declare enum` is not supported

nl2286_declare_enum.ts:

// NL2286: `declare enum` describes an enum some other translation unit holds,
// and there is no other translation unit to hold one — an enum member is folded
// to its integer where it is written, so there would be nothing to link to.
declare enum Kind {
  If = 1,
}
`declare enum` is not supported

NL2287

The message of an error contains:

An enum member name must be an identifier

Retired before it ever fired: the number was reserved, and no message in either compiler produces the fragment

NL2288

The message of an error contains:

Enums cannot be exported: an enum names a type inside one module (declare it in every module that needs it)

Retired: Enums cannot be exported, the refusal exported types replaced; an exported enum is imported now (tests/link/enum_export_uses), and importing one that is not exported is NL2045 (tests/link/enum_import_not_exported)

NL2289

The message of an error contains:

`slice` expects 1 or 2 arguments, got …

NL2290

The message of an error contains:

` again afterwards rather than holding the element across it

NL2291

The message of an error contains:

`, and the push may move that storage first; copy the fields you need into locals before pushing

NL2292

The message of an error contains:

… = ...`) is not supported: a type argument is inferred …

nl2292_default_type_argument.ts:

// NL2292: a type argument is inferred from the call's arguments and is never
// written, so a default has no position to fill.
const dflt = <T = i32>(x: T): T => x;

export const main = (): i32 => dflt(1);
a default type argument (`T = ...`) is not supported: a type argument is inferred from the arguments

NL2293

The message of an error contains:

… are not supported yet

Retired: the checker's Generic classes are not supported yet, deleted when a generic class became an ordinary struct

NL2294

The message of an error contains:

… extends ...`) is not supported yet: an unconstrained …

Retired: a constrained type parameter is not supported yet, the refusal type-parameter constraints replaced; a constraint is accepted now, and what it may name is NL2326/NL2327 (tests/cases/reject_generic_constraint)

NL2295

The message of an error contains:

… generic instantiations, which is this compiler's limit …

No program provokes it: the per-module cap (4,096 instantiations); provokable only by a program this corpus will not carry

NL2296

The message of an error contains:

…: `$` separates a generic's name from its …

NL2297

The message of an error contains:

` has been instantiated …

NL2298

The message of an error contains:

` is a generic function, and a generic function cannot …

Retired: a generic function cannot yet be instantiated from another module, the refusal cross-module generics replaced; a template is imported and instantiated now (tests/link/generic_import)

NL2299

The message of an error contains:

`: a type parameter is inferred from the arguments, and …

NL2300

The message of an error contains:

`main` cannot be generic: the entry point is called by the C runtime, which has no type arguments to give it

nl2300_generic_main.ts:

// NL2300: the entry point is called by the C runtime, which has no type
// arguments to give it, so there is no tuple to monomorphise `main` at.
export const main = <T>(x: T): i32 => {
  return 0;
};
`main` cannot be generic: the entry point is called by the C runtime, which has no type arguments to give it

NL2301

The message of an error contains:

Cannot infer `…

nl2301_infer_shape.ts:

// NL2301: inference is unification against the annotation's *shape*, so an
// argument that does not have the shape binds nothing and the parameter is left
// with no type. Distinct from NL2299, which is the parameter that appears in no
// annotation at all, and from an ordinary argument-type error: here the failure
// is that `T` could not be chosen, not that a chosen `T` did not fit.
const firstOf = <T>(xs: T[]): T => xs[0];

export const main = (): i32 => firstOf(5);
Cannot infer `T` for `firstOf`: argument 1 is i32, which does not match the declared `T[]`

NL2302

The message of an error contains:

Duplicate type parameter `…

nl2302_duplicate_type_parameter.ts:

// NL2302: two type parameters of one name. The second could never be bound to
// anything the first was not, so the list says one thing twice.
const pair = <T, T>(a: T, b: T): T => a;

export const main = (): i32 => pair(1, 2);
Duplicate type parameter `T`

NL2303

The message of an error contains:

Type arguments are not written at a call site in …

NL2304

The message of an error contains:

…, and a declared C function returns a scalar only

Retired: the scalar-only declare function return rule, before a CPtr was let through; the live one is NL2321, itself retired for NL2538

NL2305

The message of an error contains:

…, and a declared C function takes scalars only

Retired: the scalar-only declare function parameter rule, before a CPtr was let through; the live one is NL2555

NL2306

The message of an error contains:

` cannot be exported: it is a C function this program calls, not one it defines

NL2307

The message of an error contains:

`declare function` cannot be generic: a C symbol is one function, not a template to instantiate

NL2308

The message of an error contains:

`declare function` declares a C function this program calls, so it must have no body

NL2309

The message of an error contains:

` under a type constructor instead of passing it on, so the chain has no end; pass `

NL2310

The message of an error contains:

` (the builtin modules are …

NL2311

The message of an error contains:

` has no export `

NL2312

The message of an error contains:

` is a builtin function and can only be called

NL2313

The message of an error contains:

…` is a builtin imported from `…

nl2313_builtin_import_as_type.ts:

// NL2313: a `nish:` import binds a builtin, which is a function or a value and
// never a type, so naming it in a type position is refused against the import
// that bound it — the declaration to fix — the way NL2073 refuses an imported
// function used the same way.
import { write } from "nish:io";

const use = (x: write): i32 => 1;

export const main = (): i32 => 0;
`write` is a builtin imported from `nish:io`, not a type

NL2314

The message of an error contains:

` is a builtin value and cannot be called

NL2315

The message of an error contains:

` is already imported from `

NL2316

The message of an error contains:

` is a generic …

Retired: the same refusal one level up for a generic class or interface, also replaced by cross-module generics (tests/link/generic_import, tests/link/package_generic_import)

NL2317

The message of an error contains:

` is generic: it must be written with its type arguments, e.g. …

NL2318

The message of an error contains:

…` is not generic, so `new …

nl2318_generic_new.ts:

// NL2318: `new` on a class that is not generic. A generic class takes its type
// arguments here, so the refusal is about this class rather than about
// generics, and Phase 0 cannot see it because it is a use rather than a declaration.
class Point {
  x: i32 = 0;
}

export const main = (): i32 => {
  const p = new Point<i32>();
  return 0;
};
`Point` is not generic, so `new Point` takes no type arguments

NL2319

The message of an error contains:

…` under a type constructor instead of passing it on, so the chain has no end; name `…

nl2319_generic_expanding_field.ts:

// NL2319: the struct half of the termination rule. A field whose
// type puts the class's own type argument under a constructor asks for a
// strictly larger instantiation, so the chain has no end, and the sentence
// names the two shapes that do terminate rather than a depth — the fix is to
// change the annotation, not to raise a limit.
//
// `tests/cases/reject_generic_expanding_field` already pins these words. What
// this corpus adds is the *code*: `tests/diagnostic_coverage.js` reads the
// `--json` object and fails if the message comes out NL0000, or under a
// number other than the one the registry froze, which is what a reword would
// do to it.
class Nest<T> {
  inner: Nest<T[]> | null;
  constructor() {
    this.inner = null;
  }
}

export const main = (): i32 => {
  const n = new Nest<i32>();
  return n.inner === null ? 0 : 1;
};
Monomorphising `Nest` would not terminate: `Nest<i32>` names `Nest<i32[]>`, which puts `i32` under a type constructor instead of passing it on, so the chain has no end; name `T` itself, or a type that does not mention it

NL2320

The message of an error contains:

` must be relative (`./x`, `../x`), a package name (`hash`, `@scope/hash`), or one of …

NL2321

The message of an error contains:

…, and a declared C function returns a scalar or `CPtr` only

Retired: the scalar-or-CPtr declare function return rule, before a copied string was let through; the live one is NL2538

NL2322

The message of an error contains:

…, and a declared C function takes scalars and `CPtr` only

Retired: the declare function parameter rule that named scalars, CPtr and string only, before a u8[] buffer was let through; the live one is NL2555

NL2323

The message of an error contains:

…: a foreign pointer may only appear in a `declare function` signature or on a local bound to one, because it is an address a C function owns and this compiler can neither lay it out nor say how long it lives

NL2324

The message of an error contains:

` cannot be nullable: a foreign pointer is narrowed with `!== null` before it is passed back, because only the C function it came from can hand out a null one

Retired: the declare function rule that refused a CPtr | null parameter, before the literal null was let through; a held nullable pointer is NL2539

NL2325

The message of an error contains:

…` takes no type arguments: only a generic …

nl2325_type_args_on_import.ts:

// NL2325: a type-argument list on an imported name that is not a template.
//
// A generic class is importable, and this is the rule that draws the
// line around it. The refusal is made when the import binds rather than where
// the annotation is written, because pass 1 resolves a module's signatures as
// soon as it is parsed — before any import is bound — so at the point this
// annotation is read the compiler does not yet know what `Plain` is.
import { Plain } from "./wordings_generic_class";

export const valueOf = (p: Plain<i32>): i32 => p.value;

export const main = (): i32 => 0;
`Plain` in `./wordings_generic_class` takes no type arguments: only a generic class or interface is written with them

NL2326

The message of an error contains:

…` is not supported: a constraint cannot mention a type parameter, because it is resolved once for the template rather than once per instantiation; name a class or interface, with any type arguments written out

nl2326_constraint_mentions_parameter.ts:

// NL2326: a constraint that mentions a type parameter. A constraint
// is resolved once for the template, with nothing bound, so a bound that
// depends on another parameter would mean something different at every
// instantiation; that is where an F-bounded constraint starts, and it is out of
// this path.
interface Container<T> {
  value: T;
}

export const unwrap = <T, U extends Container<T>>(c: U, fallback: T): T => fallback;

export const main = (): i32 => 0;
`U extends Container<T>` is not supported: a constraint cannot mention a type parameter, because it is resolved once for the template rather than once per instantiation; name a class or interface, with any type arguments written out

NL2327

The message of an error contains:

…` is not supported: a constraint must be a declared class or interface, because the members a type parameter has are its constraint's

nl2327_constraint_not_struct.ts:

// NL2327: a constraint that is not a class or interface. The members
// a constrained parameter has are its constraint's, and `string` lends none a
// template could read through a `T`.
export const echo = <T extends string>(s: T): T => s;

export const main = (): i32 => 0;
`T extends string` is not supported: a constraint must be a declared class or interface, because the members a type parameter has are its constraint's

NL2328

The message of an error contains:

…` does not implement it; pass …

nl2328_unsatisfied_constraint.ts:

// NL2328: a type argument that does not satisfy its parameter's constraint
// here a class constraint. Nothing but `Counter` itself
// satisfies `T extends Counter`, because a class can only `implements` an
// interface, so the fix names the class.
class Counter {
  count: i32 = 0;
}

class Gauge {
  count: i32 = 0;
}

const countOf = <T extends Counter>(c: T): i32 => c.count;

export const main = (): i32 => countOf(new Gauge());
`T` of `countOf` requires `T extends Counter`, and `Gauge` does not implement it; pass `Counter` itself, because a class constraint is satisfied by that class alone

NL2329

The message of an error contains:

…`: an unconstrained type parameter has no members; …

nl2329_unconstrained_member.ts:

// NL2329: a member of an unconstrained type parameter, in
// the write form. `T` is `Cell` at the one instantiation, and the language
// still says `T` has no members until a constraint names them.
class Cell {
  value: i32 = 0;
}

const reset = <T>(c: T): void => {
  c.value = 0;
};

export const main = (): i32 => {
  reset(new Cell());
  return 0;
};
Cannot assign to `value` of `T`: an unconstrained type parameter has no members; say which ones it has with `<T extends Cell>`

NL2330

The message of an error contains:

…`: a constrained type parameter has only the members its constraint declares

nl2330_member_not_in_constraint.ts:

// NL2330: a method the concrete type has and the constraint does not.
// `Shape` is an interface, and an interface has fields only, so no method
// can be called through a `T extends Shape`.
interface Shape {
  area: i32;
}

class Circle implements Shape {
  area: i32 = 12;

  grow(): i32 {
    this.area = this.area * 2;
    return this.area;
  }
}

const grown = <T extends Shape>(shape: T): i32 => shape.grow();

export const main = (): i32 => grown(new Circle());
Unknown method `grow` on interface `Shape`, the constraint of `T`: a constrained type parameter has only the members its constraint declares

NL2331

The message of an error contains:

…`: a class's type parameters are in scope in its methods, and a diagnostic that names `…

nl2331_method_shadows_class_parameter.ts:

// NL2331: a method type parameter with the name of one of its class's.
// Both are in scope in the method's body, so every `T` there — in an
// annotation, a constraint or a diagnostic — would have two meanings.
class Box<T> {
  value: T;

  constructor(value: T) {
    this.value = value;
  }

  also<T>(other: T): T {
    return other;
  }
}

export const main = (): i32 => new Box<i32>(1).value;
Type parameter `T` of `Box.also` shadows `Box`'s own `T`: a class's type parameters are in scope in its methods, and a diagnostic that names `T` has to mean one of them; give the method's another name

NL2332

The message of an error contains:

`integer` is reserved for ranged integers (`integer<Lo, Hi>`) and cannot be declared as …

nl2332_integer_declared.ts:

// NL2332: a class, interface or function named `integer`, the name ranged
// integers (`integer<Lo, Hi>`) are reserved to take.
class integer {
  value: i32 = 0;
}

export const main = (): i32 => 0;
`integer` is reserved for ranged integers (`integer<Lo, Hi>`) and cannot be declared as a class

NL2333

The message of an error contains:

`integer<Lo, Hi>` is reserved for ranged integers, which this compiler does not have yet

Retired: integer&lt;Lo, Hi> refused as reserved until ranged integers landed; integer is the type now, and a malformed one is NL2378-NL2383

NL2334

The message of an error contains:

…` is a function parameter and can only be called or passed on as a function argument: it names the callee each call is compiled for, not a value, so it cannot be stored, returned, compared or put in an array

nl2334_fnarg_parameter_as_value.ts:

// NL2334: a function parameter used as a value. It names a callee chosen at
// compile time, so it can be called or passed on, and a comparison is as
// much a value use as a store.
const same = (f: (x: i32) => i32, g: (x: i32) => i32): boolean => f === g;

const inc = (x: i32): i32 => x + 1;

export const main = (): i32 => (same(inc, inc) ? 1 : 0);
`f` is a function parameter and can only be called or passed on as a function argument: it names the callee each call is compiled for, not a value, so it cannot be stored, returned, compared or put in an array

NL2335

The message of an error contains:

…`, which belongs to the function it is written in: an arrow given to `expose` or `exposeWith` reads only its own parameters and the module's top-level names, so nothing it computes from the plaintext leaves but its result; pass what it needs as `exposeWith`'s argument

nl2335_secret_expose_capture.ts:

// NL2335: an arrow given to `expose` reads a local of the function it is
// written in; what `expose` runs captures nothing.
import { expose, secret, wipe } from "nish:secret";
const bytes = (): u8[] => [1, 2];
export const main = (): i32 => {
  const k = secret(bytes());
  const leak: u8[] = [0, 0];
  const n = expose(k, (b: u8[]): i32 => {
    leak.set(b, 0);
    return 0;
  });
  wipe(k);
  return n;
};
The arrow reads `leak`, which belongs to the function it is written in: an arrow given to `expose` or `exposeWith` reads only its own parameters and the module's top-level names, so nothing it computes from the plaintext leaves but its result; pass what it needs as `exposeWith`'s argument

NL2336

The message of an error contains:

an arrow in value position is not yet a function reference: declare it as a top-level function and name it

nl2336_fnarg_arrow_returned.ts:

// NL2336: an arrow anywhere but as a function argument, here returned. It
// captures nothing, so the advice is the one stage 1 of function references
// takes: name it as a top-level function.
const make = (): ((x: i32) => i32) => {
  return (x: i32): i32 => x + 1;
};

export const main = (): i32 => make()(1);
an arrow in value position is not yet a function reference: declare it as a top-level function and name it

NL2337

The message of an error contains:

` is a function type, which may only annotate a parameter of a top-level function: a function is never a value in …

Retired: ... is a function type, which may only annotate a parameter of a top-level function; function reference types made a function type outside a parameter a function reference type, so an array, field, local or return of one compiles (tests/cases/fnref_array)

NL2338

The message of an error contains:

…` cannot have a function type here: a function type may only annotate a parameter of a top-level function, which is monomorphised for each function it is given, and this is a parameter of …

nl2338_fnarg_constructor_parameter.ts:

// NL2338: a function-typed parameter of a constructor. A constructor is not a
// top-level function and cannot be a template over its callee.
class Counter {
  n: i32;

  constructor(step: (x: i32) => i32) {
    this.n = 0;
  }
}

export const main = (): i32 => 0;
`step` cannot have a function type here: a function type may only annotate a parameter of a top-level function, which is monomorphised for each function it is given, and this is a parameter of a method or a constructor

NL2339

The message of an error contains:

…` must be the name of a top-level function or an arrow written at the call, because `…

nl2339_fnarg_constant_argument.ts:

// NL2339: a module constant passed where a function is wanted is named as a
// constant, not as an unknown function; a constant is never a function.
const apply = (f: (x: i32) => i32, x: i32): i32 => f(x);

const notAFunction: i32 = 5;

export const main = (): i32 => apply(notAFunction, 3);
Argument 1 of `apply` must be the name of a top-level function or an arrow written at the call, because `f` is a function parameter, resolved at compile time; `notAFunction` is a constant, and a constant is never a function

nl2339_fnarg_member_argument.ts:

// NL2339: a function argument that is neither a top-level function nor an
// arrow written at the call; a member access computes a value.
const apply = (f: (x: i32) => i32, x: i32): i32 => f(x);

export const main = (): i32 => apply(Math.abs, -2);
Argument 1 of `apply` must be the name of a top-level function or an arrow written at the call, because `f` is a function parameter, resolved at compile time; a function parameter is bound to a callee, not a value computed at run time

NL2340

The message of an error contains:

…`; a function argument must have exactly its parameter's type

nl2340_fnarg_parameter_count.ts:

// NL2340: a named function whose parameter list does not match the function
// type: it takes two where the parameter says one.
const apply = (f: (x: i32) => i32, x: i32): i32 => f(x);

const add = (a: i32, b: i32): i32 => a + b;

export const main = (): i32 => apply(add, 1);
Argument 1 of `apply`: `add` is `(a: i32, b: i32) => i32`, and `f` is `(x: i32) => i32`; a function argument must have exactly its parameter's type

nl2340_fnarg_reference_arity.ts:

// NL2340: a function reference passed for a function parameter of another
// parameter count.
type G = (x: i32, y: i32) => i32;

const add = (x: i32, y: i32): i32 => x + y;

const apply = (f: (x: i32) => i32, x: i32): i32 => f(x);

export const main = (): i32 => {
  const g: G = add;
  return apply(g, 2);
};
Argument 1 of `apply`: `g` is `(i32, i32) => i32`, and `f` is `(x: i32) => i32`; a function argument must have exactly its parameter's type

NL2341

The message of an error contains:

…` is generic, so it names no one function to pass as argument …

nl2341_fnarg_generic_by_name.ts:

// NL2341: a generic function passed by name, which names a template and not
// one function.
const each = <T>(xs: T[], f: (x: T) => T): T[] => xs;

const keep = <T>(x: T): T => x;

export const main = (): i32 => each([1, 2], keep).length;
`keep` is generic, so it names no one function to pass as argument 2 of `each`; pass an arrow that calls it, such as `(x) => keep(x)`

NL2342

The message of an error contains:

…` mentions a type parameter no other argument binds; annotate the parameter

nl2342_fnarg_arrow_parameter_uninferred.ts:

// NL2342: an unannotated arrow parameter whose type is a type parameter that
// only the arrow itself could bind.
const count = <T>(pred: (x: T) => boolean): i32 => 0;

export const main = (): i32 => count((x) => true);
Cannot infer the type of `x` in the arrow passed to `count`: `T` mentions a type parameter no other argument binds; annotate the parameter

NL2343

The message of an error contains:

…`, which only its body could bind, and a block body's type is not inferred

nl2343_fnarg_block_result_uninferred.ts:

// NL2343: a block-bodied arrow whose result type is left to it, with no
// annotation to take it from.
const make = <U>(f: (x: i32) => U): i32 => 0;

export const main = (): i32 =>
  make((x) => {
    return x * 2;
  });
The arrow passed to `make` needs a return type annotation: its result is `U`, which only its body could bind, and a block body's type is not inferred

NL2344

The message of an error contains:

The arrow passed as argument …

nl2344_fnarg_arrow_parameter_count.ts:

// NL2344: an arrow with more parameters than its function type.
const fold = (xs: i32[], f: (acc: i32, x: i32) => i32): i32 => 0;

export const main = (): i32 => fold([1], (acc, x, i) => acc + x + i);
The arrow passed as argument 2 of `fold` takes 3 parameter(s), and `f` is `(acc: i32, x: i32) => i32`

NL2345

The message of an error contains:

…` runs it on several threads at once: a parallel body may read what its caller owns and write nothing but its result…

nl2345_par_shared_write_map_arrow.ts:

// NL2345: the same rule for an arrow body of `parallelMap`, which writes the
// caller's `seen` on every element.
import { parallelMap } from "nish/threads";

class Seen {
  n: i32 = 0;
}

export const main = (): i32 => {
  const seen = new Seen();
  const src: i32[] = [1, 2, 3];
  const doubled = parallelMap(src, (x: i32): i32 => {
    seen.n = x;
    return x * 2;
  });
  return doubled[0] + seen.n;
};
`(x) => ...` writes memory its caller can see at tests/wordings/nl2345_par_shared_write_map_arrow.ts:13:5, and `parallelMap` runs it on several threads at once: a parallel body may read what its caller owns and write nothing but its result; make it answer what it computes and do the write after the call, or run it on the thread that called it, `const doubled: i32[] = []; for (const x of src) { seen.n = x; doubled.push(x * 2) }`

nl2345_par_shared_write_map_into.ts:

// NL2345: a data-parallel body writes nothing its caller can see. `tally` adds
// to the `Tally` each element points at, from every thread at once.
import { parallelMapInto } from "nish/threads";

class Tally {
  n: i32 = 0;
}

const tally = (t: Tally): i32 => {
  t.n = t.n + 1;
  return t.n * 2;
};

export const main = (): i32 => {
  const src: Tally[] = [new Tally(), new Tally()];
  const dst: i32[] = [0, 0];
  parallelMapInto(src, dst, tally);
  return dst[0];
};
`tally` writes memory its caller can see at tests/wordings/nl2345_par_shared_write_map_into.ts:10:3, and `parallelMapInto` runs it on several threads at once: a parallel body may read what its caller owns and write nothing but its result; make it answer what it computes and do the write after the call, or run it on the thread that called it, `for (let i = 0; i < src.length; i++) { dst[i] = tally(src[i]) }`

nl2345_par_shared_write_reduce.ts:

// NL2345: the same rule for a reducer, which writes the caller's `log` as it folds.
import { parallelReduce } from "nish/threads";

class Log {
  last: i32 = 0;
}

export const main = (): i32 => {
  const src: i32[] = [1, 2, 3];
  const log = new Log();
  const sum = parallelReduce(src, (a: i32, b: i32): i32 => {
    log.last = b;
    return a + b;
  }, 0);
  return sum + log.last;
};
`(a, b) => ...` writes memory its caller can see at tests/wordings/nl2345_par_shared_write_reduce.ts:12:5, and `parallelReduce` runs it on several threads at once: a parallel body may read what its caller owns and write nothing but its result; make it answer what it computes and do the write after the call, or run it on the thread that called it, `let sum: i32 = 0; for (const b of src) { const a = sum; log.last = b; sum = a + b }`

NL2346

The message of an error contains:

` runs it on threads whose arenas are freed when they exit: a parallel body may not allocate (a string, an array, an object or a `Result`)

Retired: the first parallel loops' strict rule that a parallel body may not allocate at all; a body's allocations are recycled per element since, and what is refused instead is NL2351 (the arena) and NL2352 (an allocation that escapes), pinned by tests/cases/reject_par_arena and reject_par_alloc_escapes

NL2347

The message of an error contains:

`, which could be the array `parallelMapInto` is writing: another thread would be writing it while this one reads, so `dst` may not be reachable from an element of `src`

NL2348

The message of an error contains:

…` hands back only a number, a `boolean`…

nl2348_par_from_class.ts:

// NL2348: `parallelFrom` copies a number, a string or an array of numbers out
// of a worker's arena, and an object is none of them.
import { parallelFrom } from "nish/threads";

class Cell {
  v: i32 = 0;
}

const cellAt = (i: i32): Cell => {
  const c = new Cell();
  c.v = i;
  return c;
};

export const main = (): i32 => {
  const cells = parallelFrom(3, cellAt);
  return cells[2].v;
};
`cellAt` answers `Cell`, and `parallelFrom` hands back only a number, a `boolean`, an enum, a `string` or an array of numbers, booleans or enums, which the join copies out of the worker's arena: anything else would point into memory the worker frees or reuses; answer the number or string the caller needs, one result per field if it needs several, and build the object from them after the call, or run it on the thread that called it, `const cells: Cell[] = []; for (let i2 = 0; i2 < 3; i2++) { cells.push(cellAt(i2)) }`

nl2348_par_map_class.ts:

// NL2348: the same rule for an arrow body of `parallelMap` answering an object.
import { parallelMap } from "nish/threads";

class Cell {
  v: i32 = 0;
}

export const main = (): i32 => {
  const src: i32[] = [1, 2, 3];
  const cells = parallelMap(src, (x: i32): Cell => {
    const c = new Cell();
    c.v = x;
    return c;
  });
  return cells[0].v;
};
`(x) => ...` answers `Cell`, and `parallelMap` hands back only a number, a `boolean`, an enum, a `string` or an array of numbers, booleans or enums, which the join copies out of the worker's arena: anything else would point into memory the worker frees or reuses; answer the number or string the caller needs, one result per field if it needs several, and build the object from them after the call, or run it on the thread that called it, `const cells: Cell[] = []; for (const x of src) { const c = new Cell(); c.v = x; cells.push(c) }`

nl2348_par_reduce_string.ts:

// NL2348: a reduce hands back only a number, a `boolean` or an enum. Strings
// would be folded on several threads, each with an arena of its own.
import { parallelReduce } from "nish/threads";

export const main = (): i32 => {
  const words: string[] = ["a", "b", "c"];
  const joined = parallelReduce(words, (a: string, b: string): string => a + b, "");
  return joined.length;
};
`(a, b) => ...` answers `string`, and `parallelReduce` hands back only a number, a `boolean` or an enum: a worker's arena is freed when its thread exits, so anything else would point into freed memory; answer a number and fold those, or run it on the thread that called it, `let joined: string = ""; for (const b of words) { const a = joined; joined = a + b }`

nl2348_task_class.ts:

// NL2348: a task's answer crosses back by copy too, so an object is refused.
import { scope } from "nish/threads";

class Cell {
  v: i32 = 0;
}

const make = (v: i32): Cell => {
  const c = new Cell();
  c.v = v;
  return c;
};

export const main = (): i32 => {
  const cells: Cell[] = [new Cell()];
  {
    using s = scope();
    s.spawn(make, 7, cells, 0);
  }
  return cells[0].v;
};
`make` answers `Cell`, and `spawn` hands back only a number, a `boolean`, an enum, a `string` or an array of numbers, booleans or enums, which the join copies out of the worker's arena: anything else would point into memory the worker frees or reuses; answer the number or string the caller needs, one result per field if it needs several, and build the object from them after the call, or run it on the thread that called it, `cells[0] = make(7)`

NL2349

The message of an error contains:

`, which is not associative: `parallelReduce` folds each block from the identity and then combines the blocks, which is a left fold only for an associative operator

NL2350

The message of an error contains:

`parallelReduce` folds every block from its identity, and the identity of `

NL2351

The message of an error contains:

` runs it on several threads that each have an arena of their own: a parallel body may not call `Arena.mark` or `Arena.used`, nor open a `using a = arena()` block, nor call `region`, `regionWith`, `child`, `childWith`, `enter`, `enterWith` or `free` (`nish:region`)

NL2352

The message of an error contains:

…` releases what a body allocates after every element: a parallel body may allocate only temporaries it drops before it returns, and this analysis stops following a value once it is stored…

nl2352_field_store.ts:

// NL2352: a string a parallel body builds and stores into a field of an
// element is refused, with the serial loop that does the store on the calling thread.
import { parallelMap } from "nish/threads";

class Row {
  n: i32;
  label: string;

  constructor(n: i32) {
    this.n = n;
    this.label = "";
  }
}

export const main = (): i32 => {
  const rows: Row[] = [new Row(1), new Row(2)];
  const ys = parallelMap(rows, (r: Row): i32 => {
    r.label = `row ${r.n}`;
    return r.n;
  });
  console.log(ys.length);
  console.log(rows[1].label);
  return 0;
};
`(r) => ...` allocates at tests/wordings/nl2352_field_store.ts:18:15 and stores the allocation into memory, and `parallelMap` releases what a body allocates after every element: a parallel body may allocate only temporaries it drops before it returns, and this analysis stops following a value once it is stored; make it answer what it allocates and do the store after the call, or run it on the thread that called it, `` const ys: i32[] = []; for (const r of rows) { r.label = `row ${r.n}`; ys.push(r.n) } ``

nl2352_push_captured.ts:

// NL2352: a parallel body that stores what it allocates into an array its
// caller owns is refused, and the remedy is the serial loop in the program's names.
import { parallelMap } from "nish/threads";

export const main = (): i32 => {
  const xs: i32[] = [1, 2, 3];
  const seen: string[] = [];
  const ys = parallelMap(xs, (x: i32): i32 => {
    seen.push(`${x}`);
    return x * 2;
  });
  console.log(ys.length);
  console.log(seen.length);
  return 0;
};
`(x) => ...` allocates at tests/wordings/nl2352_push_captured.ts:9:15 and stores the allocation into memory, and `parallelMap` releases what a body allocates after every element: a parallel body may allocate only temporaries it drops before it returns, and this analysis stops following a value once it is stored; make it answer what it allocates and do the store after the call, or run it on the thread that called it, `` const ys: i32[] = []; for (const x of xs) { seen.push(`${x}`); ys.push(x * 2) } ``

NL2353

The message of an error contains:

…`: a key is hashed and compared by value, or by identity for a class instance, so it is a string, a number of any width, a boolean, an enum or a class, and not an interface, an array, a nullable type or a `Result`

nl2353_map_key_interface.ts:

// NL2353: an interface as the key of the global `Map`: it is stored inline and
// has no identity to hash or compare.
interface Point {
  x: i32;
}

export const main = (): i32 => {
  new Map<Point, i32>();
  return 0;
};
`Point` cannot be the key type of `Map<Point, i32>`: a key is hashed and compared by value, or by identity for a class instance, so it is a string, a number of any width, a boolean, an enum or a class, and not an interface, an array, a nullable type or a `Result`

NL2354

The message of an error contains:

…`: a value is stored in the table as it is, so it may not be `void`, nor an interface, whose record would be copied in rather than shared (use a class)

nl2354_map_value_interface.ts:

// NL2354: an interface as the value of the global `Map`: its record would be
// copied into the table rather than shared.
interface Point {
  x: i32;
}

export const main = (): i32 => {
  new Map<string, Point>();
  return 0;
};
`Point` cannot be the value type of `Map<string, Point>`: a value is stored in the table as it is, so it may not be `void`, nor an interface, whose record would be copied in rather than shared (use a class)

NL2355

The message of an error contains:

…` takes no arguments in this version: a `Map` or `Set` starts empty, so create it with `…

nl2355_map_new_arguments.ts:

// NL2355: `new Map(...)` with arguments; a map starts empty in this version.
export const main = (): i32 => {
  new Map<string, i32>(4);
  return 0;
};
`new Map<string, i32>` takes no arguments in this version: a `Map` or `Set` starts empty, so create it with `new Map<string, i32>()` and `set` or `add` each entry in a loop

NL2356

The message of an error contains:

…`: a `Map` or `Set` in this version has no `entries`, because a `[key, value]` pair is a two-type tuple; `forEach((value, key) => ...)` visits each key with its value

nl2356_map_entries.ts:

// NL2356: `entries` on the global `Map`; a `[key, value]` pair is a two-type tuple.
export const main = (): i32 => {
  const m = new Map<string, i32>();
  m.entries();
  return 0;
};
`entries` is not available on `Map<string, i32>`: a `Map` or `Set` in this version has no `entries`, because a `[key, value]` pair is a two-type tuple; `forEach((value, key) => ...)` visits each key with its value

NL2357

The message of an error contains:

` is not supported yet: its result is `V | undefined`, and this version cannot narrow one

Retired: get refused as "not supported yet" until its V | undefined landed; each place that refuses a maybe has its own code now, NL2361-NL2369

NL2358

The message of an error contains:

… can only be the iterable of a `for...of`, which walks the entries in insertion order: an iterator is not a value in this version

nl2358_map_keys.ts:

// NL2358: `keys()` on the global `Map` outside a `for...of`.
export const main = (): i32 => {
  const m = new Map<string, i32>();
  m.keys();
  return 0;
};
`keys()` of `Map<string, i32>` can only be the iterable of a `for...of`, which walks the entries in insertion order: an iterator is not a value in this version

NL2359

The message of an error contains:

…`: a `Map` has size, get, set, has, delete, clear and forEach, and a `Set` has size, add, has, delete, clear and forEach

nl2359_map_internal_member.ts:

// NL2359: a member of the table itself, which a program cannot see.
export const main = (): i32 => {
  const s = new Set<i32>();
  return s.live;
};
Unknown member `live` on `Set<i32>`: a `Map` has size, get, set, has, delete, clear and forEach, and a `Set` has size, add, has, delete, clear and forEach

NL2360

The message of an error contains:

…` is read-only: it counts the entries, and `set`, `add`, `delete` and `clear` are what change it

nl2360_map_size_assign.ts:

// NL2360: `size` of the global `Set` is read-only.
export const main = (): i32 => {
  const s = new Set<i32>();
  s.size = 1;
  return 0;
};
`size` of `Set<i32>` is read-only: it counts the entries, and `set`, `add`, `delete` and `clear` are what change it

NL2361

The message of an error contains:

… and cannot be held in a `let`: only a `const` is narrowed, because a `let` can be assigned; bind it with `const` and test it with `!== undefined`, or give it a default with `??`

nl2361_map_get_let.ts:

// NL2361: a `V | undefined` held in a `let`.
export const main = (): i32 => {
  const m = new Map<string, i32>();
  let i = m.get("a");
  return 0;
};
`m.get("a")` is `i32 | undefined` and cannot be held in a `let`: only a `const` is narrowed, because a `let` can be assigned; bind it with `const` and test it with `!== undefined`, or give it a default with `??`

NL2362

The message of an error contains:

… and cannot be passed as an argument: a value that may be missing does not cross a call; give it a default with `??`, or bind it to a `const` and pass it where `!== undefined` has narrowed it

nl2362_map_get_argument.ts:

// NL2362: a `V | undefined` passed as an argument.
const use = (x: i32): i32 => x;
export const main = (): i32 => {
  const m = new Map<string, i32>();
  return use(m.get("a"));
};
`m.get("a")` is `i32 | undefined` and cannot be passed as an argument: a value that may be missing does not cross a call; give it a default with `??`, or bind it to a `const` and pass it where `!== undefined` has narrowed it

NL2363

The message of an error contains:

… and cannot be returned: a value that may be missing does not cross a call; return a default with `??`, or bind it to a `const` and return it where `!== undefined` has narrowed it

nl2363_map_get_return.ts:

// NL2363: a `V | undefined` returned.
export const main = (): i32 => {
  const m = new Map<string, i32>();
  m.set("a", 1);
  return m.get("a");
};
`m.get("a")` is `i32 | undefined` and cannot be returned: a value that may be missing does not cross a call; return a default with `??`, or bind it to a `const` and return it where `!== undefined` has narrowed it

NL2364

The message of an error contains:

… and cannot be stored in a field: a value that may be missing is never in memory; store a default with `??`, or bind it to a `const` and store it where `!== undefined` has narrowed it

nl2364_map_get_field.ts:

// NL2364: a `V | undefined` stored in a field.
class Box {
  v: i32 = 0;
}
export const main = (): i32 => {
  const m = new Map<string, i32>();
  const b = new Box();
  b.v = m.get("a");
  return b.v;
};
`m.get("a")` is `i32 | undefined` and cannot be stored in a field: a value that may be missing is never in memory; store a default with `??`, or bind it to a `const` and store it where `!== undefined` has narrowed it

NL2365

The message of an error contains:

… and cannot be stored in an array element: a value that may be missing is never in memory; store a default with `??`, or bind it to a `const` and store it where `!== undefined` has narrowed it

nl2365_map_get_element.ts:

// NL2365: a `V | undefined` stored in an array element.
export const main = (): i32 => {
  const m = new Map<string, i32>();
  const a: i32[] = [0];
  a[0] = m.get("a");
  return a[0];
};
`m.get("a")` is `i32 | undefined` and cannot be stored in an array element: a value that may be missing is never in memory; store a default with `??`, or bind it to a `const` and store it where `!== undefined` has narrowed it

NL2366

The message of an error contains:

… and cannot be a template literal hole: there is no `undefined` to print; give it a default with `??`, or bind it to a `const` and print it where `!== undefined` has narrowed it

nl2366_map_get_template.ts:

// NL2366: a `V | undefined` as a template hole.
export const main = (): i32 => {
  const m = new Map<string, i32>();
  console.log(`${m.get("a")}`);
  return 0;
};
`m.get("a")` is `i32 | undefined` and cannot be a template literal hole: there is no `undefined` to print; give it a default with `??`, or bind it to a `const` and print it where `!== undefined` has narrowed it

NL2367

The message of an error contains:

… and cannot be the operand of an operator other than `??`, `=== undefined` and `!== undefined`: give it a default with `??` first, or bind it to a `const` and use it where `!== undefined` has narrowed it

nl2367_map_get_operand.ts:

// NL2367: a `V | undefined` as an arithmetic operand.
export const main = (): i32 => {
  const m = new Map<string, i32>();
  m.set("a", m.get("a") + 1);
  return 0;
};
`m.get("a")` is `i32 | undefined` and cannot be the operand of an operator other than `??`, `=== undefined` and `!== undefined`: give it a default with `??` first, or bind it to a `const` and use it where `!== undefined` has narrowed it

NL2368

The message of an error contains:

… and cannot initialise a `const` annotated with its value type: annotate the `const` with the whole type, or with none, and test it with `!== undefined`, or give it a default with `??`

nl2368_map_get_annotated.ts:

// NL2368: a `V | undefined` initialising a `const` annotated `V`.
export const main = (): i32 => {
  const m = new Map<string, i32>();
  const j: i32 = m.get("a");
  return j;
};
`m.get("a")` is `i32 | undefined` and cannot initialise a `const` annotated with its value type: annotate the `const` with the whole type, or with none, and test it with `!== undefined`, or give it a default with `??`

NL2369

The message of an error contains:

…, which can only initialise a `const`, be the left operand of `??`, or be compared with `undefined` by `===` or `!==`

nl2369_map_get_elsewhere.ts:

// NL2369: a `V | undefined` anywhere else.
export const main = (): i32 => {
  const m = new Map<string, i32>();
  m.get("a");
  return 0;
};
`m.get("a")` is `i32 | undefined`, which can only initialise a `const`, be the left operand of `??`, or be compared with `undefined` by `===` or `!==`

NL2370

The message of an error contains:

The right operand of `??` stands in for a missing value, so it must be `…

nl2370_nullish_default.ts:

// NL2370: a default of `??` that is not the value type.
export const main = (): i32 => {
  const m = new Map<string, i32>();
  return m.get("a") ?? "none";
};
The right operand of `??` stands in for a missing value, so it must be `i32`, the type of the value on its left; got `string`

NL2371

The message of an error contains:

… and cannot be the default of another `??`: the default stands in for a missing value, so it is a value; give this one a default of its own first, `a ?? (b ?? d)`

nl2371_nullish_default_maybe.ts:

// NL2371: a `V | undefined` as the default of another `??`.
export const main = (): i32 => {
  const m = new Map<string, i32>();
  return m.get("a") ?? m.get("b") ?? 0;
};
`m.get("b")` is `i32 | undefined` and cannot be the default of another `??`: the default stands in for a missing value, so it is a value; give this one a default of its own first, `a ?? (b ?? d)`

NL2372

The message of an error contains:

… cannot be stored: an iterator is not a value in this version, so a `Map` or `Set` is walked where it is, as the iterable of a `for...of`

nl2372_map_iter_stored.ts:

// NL2372: `keys()` of the global `Map` stored in a variable.
export const main = (): i32 => {
  const m = new Map<string, i32>();
  const it = m.keys();
  return 0;
};
`keys()` of `Map<string, i32>` cannot be stored: an iterator is not a value in this version, so a `Map` or `Set` is walked where it is, as the iterable of a `for...of`

NL2373

The message of an error contains:

… cannot be passed as an argument: an iterator is not a value in this version, so pass the `Map` or `Set` itself and walk it in the callee with a `for...of`

nl2373_map_iter_argument.ts:

// NL2373: `values()` of the global `Map` passed as an argument.
const count = (xs: i32[]): i32 => xs.length;

export const main = (): i32 => {
  const m = new Map<string, i32>();
  return count(m.values());
};
`values()` of `Map<string, i32>` cannot be passed as an argument: an iterator is not a value in this version, so pass the `Map` or `Set` itself and walk it in the callee with a `for...of`

NL2374

The message of an error contains:

… cannot be returned: an iterator is not a value in this version, so return the `Map` or `Set` itself and walk it with a `for...of` where it is used

nl2374_map_iter_returned.ts:

// NL2374: `keys()` of the global `Set` returned.
const keysOf = (s: Set<i32>): i32[] => {
  return s.keys();
};

export const main = (): i32 => keysOf(new Set<i32>()).length;
`keys()` of `Set<i32>` cannot be returned: an iterator is not a value in this version, so return the `Map` or `Set` itself and walk it with a `for...of` where it is used

NL2375

The message of an error contains:

…`, whose `[key, value]` pairs are two-type tuples, which this version does not have: walk `keys()` or `values()` instead, as in `for (const k of …

nl2375_map_iter_entries.ts:

// NL2375: `for...of` over the global `Map` itself, which would need `entries()`.
export const main = (): i32 => {
  const m = new Map<string, i32>();
  for (const e of m) {
  }
  return 0;
};
`for...of` over `Map<string, i32>` needs `entries()`, whose `[key, value]` pairs are two-type tuples, which this version does not have: walk `keys()` or `values()` instead, as in `for (const k of m.keys())`

NL2376

The message of an error contains:

` has no exported function `

NL2377

The message of an error contains:

` is inspected, and the value it replaces is dropped: test it first with `

NL2378

The message of an error contains:

`integer` needs exactly two bounds, e.g. `integer<0, 255>`

nl2378_integer_bounds.ts:

// NL2378: `integer<Lo, Hi>` written with other than two bounds.
const widen = (x: integer<i32>): i32 => x

export const main = (): i32 => widen(0)
`integer` needs exactly two bounds, e.g. `integer<0, 255>`

NL2379

The message of an error contains:

A bound of `integer<Lo, Hi>` must be an integer literal, got `…

nl2379_integer_bound_not_literal.ts:

// NL2379: a bound of `integer<Lo, Hi>` that is a name rather than a literal.
const LIMIT: i32 = 9

const f = (x: integer<0, LIMIT>): i32 => x

export const main = (): i32 => f(0)
A bound of `integer<Lo, Hi>` must be an integer literal, got `LIMIT` (a type or a constant cannot state a bound)

NL2380

The message of an error contains:

…` where a bound of `integer<Lo, Hi>` is expected

nl2380_integer_bound_fraction.ts:

// NL2380: a bound of `integer<Lo, Hi>` with a fraction.
const f = (x: integer<0.5, 9>): i32 => x

export const main = (): i32 => f(1)
Non-integer literal `0.5` where a bound of `integer<Lo, Hi>` is expected

NL2381

The message of an error contains:

…>` is an empty range: the lower bound is greater than the upper bound

nl2381_integer_empty_range.ts:

// NL2381: `integer<Lo, Hi>` with Lo above Hi.
const f = (x: integer<1, -1>): i32 => x

export const main = (): i32 => f(0)
`integer<1, -1>` is an empty range: the lower bound is greater than the upper bound

NL2382

The message of an error contains:

…` of `integer<Lo, Hi>` is outside i32 (a ranged integer is always an i32)

nl2382_integer_bound_outside_i32.ts:

// NL2382: a bound of `integer<Lo, Hi>` that does not fit in `i32`.
const f = (x: integer<0, 0x80000000>): i32 => x

export const main = (): i32 => f(0)
Bound `0x80000000` of `integer<Lo, Hi>` is outside i32 (a ranged integer is always an i32)

NL2383

The message of an error contains:

…` is only allowed as a bound of `integer<Lo, Hi>`

nl2383_literal_type.ts:

// NL2383: a numeric literal type anywhere but as a bound of `integer<Lo, Hi>`.
const values: (3)[] = [3]

export const main = (): i32 => values.length
A literal type `3` is only allowed as a bound of `integer<Lo, Hi>`

NL2384

The message of an error contains:

…` is outside the range of …

nl2384_literal_outside_range.ts:

// NL2384: a literal entering a range it lies outside.
const first = (xs: integer<1, 12>[]): i32 => xs[0]

export const main = (): i32 => first([0])
Literal `0` is outside the range of integer<1, 12>

NL2385

The message of an error contains:

…, and a range is a promise the C side never made: declare it `i32`

nl2385_declare_function_range.ts:

// NL2385: a ranged integer in a `declare function` signature.
declare function putchar(c: integer<0, 255>): i32

export const main = (): i32 => putchar(65)
`declare function putchar` takes `c` as integer<0, 255>, and a range is a promise the C side never made: declare it `i32`

NL2386

The message of an error contains:

…>(n)` would zero-fill with 0, which is outside the range; build it with `[]` and `push` instead

nl2386_array_zero_fill_range.ts:

// NL2386: `new Array<integer<Lo, Hi>>(n)` for a range that does not hold 0.
export const main = (): i32 => {
  const months = new Array<integer<1, 12>>(3)
  return months.length
}
`new Array<integer<1, 12>>(n)` would zero-fill with 0, which is outside the range; build it with `[]` and `push` instead

NL2387

The message of an error contains:

`using` takes only `scope()` or a `Mutex`'s `lock()` from `nish/threads`, or the builtin `arena()`, in this version: they are the values whose disposal the language defines — a scope joins its tasks, a guard releases its lock, an arena releases what its block allocated — so a `using` of anything else would promise a disposal nothing performs

nl2387_thread_using_not_scope.ts:

// NL2387: `using` takes only a `scope()` from `nish/threads`: a scope is the one value
// whose disposal the language defines, and a `Box` has none.
class Box {
  n: i32 = 0;
}

export const main = (): i32 => {
  using b = new Box();
  return b.n;
};
`using` takes only `scope()` or a `Mutex`'s `lock()` from `nish/threads`, or the builtin `arena()`, in this version: they are the values whose disposal the language defines — a scope joins its tasks, a guard releases its lock, an arena releases what its block allocated — so a `using` of anything else would promise a disposal nothing performs

NL2388

The message of an error contains:

`scope()` must be the initialiser of a `using` declaration: a scope joins its tasks when the block that declares it ends, so a scope bound any other way would be one nobody joins…

nl2388_thread_scope_not_using.ts:

// NL2388: A scope must be introduced by `using`: bound by `const`, nothing would join
// its tasks when the block ends.
import { scope } from "nish/threads";

const one = (n: i32): i32 => n + 1;

export const main = (): i32 => {
  const out: i32[] = [0];
  const s = scope();
  s.spawn(one, 1, out, 0);
  return out[0];
};
`scope()` must be the initialiser of a `using` declaration: a scope joins its tasks when the block that declares it ends, so a scope bound any other way would be one nobody joins; declare it as `using s = scope()`

NL2389

The message of an error contains:

…` can only be the receiver of a `spawn` statement: passed, stored, returned or copied, it could be given a task after its block has joined it

nl2389_thread_scope_escapes.ts:

// NL2389: A scope is only ever the receiver of a `spawn` statement: handed to another
// function it could be stored, and given a task after its block joined it.
import { scope, ThreadScope } from "nish/threads";

const one = (n: i32): i32 => n + 1;

const later = (s: ThreadScope): i32 => {
  const out: i32[] = [0];
  s.spawn(one, 1, out, 0);
  return out[0];
};

export const main = (): i32 => {
  const out: i32[] = [0];
  {
    using s = scope();
    out[0] = later(s);
  }
  return out[0];
};
A `ThreadScope` can only be the receiver of a `spawn` statement: passed, stored, returned or copied, it could be given a task after its block has joined it

NL2390

The message of an error contains:

A `using` declaration must be a statement of a block, `{ ... }`: its scope joins when that block ends, and a single-statement body is not a block

nl2390_thread_using_placement.ts:

// NL2390: A `using` declaration is a statement of a block: its scope joins when that
// block ends, and the body of this `if` is one statement, not a block.
import { scope } from "nish/threads";

export const main = (): i32 => {
  const go = true;
  if (go) using s = scope();
  return 0;
};
A `using` declaration must be a statement of a block, `{ ... }`: its scope joins when that block ends, and a single-statement body is not a block

NL2391

The message of an error contains:

The task given to `spawn` must be a top-level function named at the call, not an arrow: a task is a unit of work a thread runs on its own, and its name is what a debugger or a profiler shows for that thread (declare the arrow as a `const` of the module and pass its name)

Retired: The task given to spawn must be a top-level function named at the call, not an arrow, the refusal arrow tasks replaced; an arrow task reads its function's const locals and is lifted into a function named after that function (tests/link/thread_task_arrow)

NL2392

The message of an error contains:

…`, which a task cannot be handed: a `Result` is passed as its parts rather than as one value, so hand the task the value it holds

nl2392_thread_task_result_arg.ts:

// NL2392: A task cannot be handed a `Result`, which is passed as its parts rather than
// as one value: hand it the value the `Result` holds.
import { scope } from "nish/threads";

const orZero = (r: Result<i32, i32>): i32 => r.unwrapOr(0);

export const main = (): i32 => {
  const out: i32[] = [0];
  const r: Result<i32, i32> = Ok(3);
  {
    using s = scope();
    s.spawn(orZero, r, out, 0);
  }
  return out[0];
};
The argument of `spawn` is `Result<i32, i32>`, which a task cannot be handed: a `Result` is passed as its parts rather than as one value, so hand the task the value it holds

NL2393

The message of an error contains:

…` cannot declare `[Symbol.dispose]`: in this version `using` takes only `scope()` or a `Mutex`'s `lock()` from `nish/threads`, or the builtin `arena()`, whose disposal the compiler emits itself, so a disposal method of any other class would never be called

nl2393_thread_dispose_elsewhere.ts:

// NL2393: `[Symbol.dispose]` belongs to `nish/threads`'s scope alone: `using` takes
// nothing else, so any other class's would never be called.
class Handle {
  n: i32 = 0;
  [Symbol.dispose](): void {}
}

export const main = (): i32 => new Handle().n;
`Handle` cannot declare `[Symbol.dispose]`: in this version `using` takes only `scope()` or a `Mutex`'s `lock()` from `nish/threads`, or the builtin `arena()`, whose disposal the compiler emits itself, so a disposal method of any other class would never be called

NL2394

The message of an error contains:

…: a scope's tasks run, and store their answers, when its block ends, and under Node each runs and stores where it is spawned, so a destination is a fresh `const` array that is never handed on, and between a scope's first `spawn` and the end of its block its thread neither reads a destination nor writes memory a task may read

nl2394_thread_region_read.ts:

// NL2394: Between a scope's first `spawn` and the end of its block, its thread may not
// read an array a task stores into: natively `sums[0]` is still 0 here, and
// under Node the task has already stored 6.
import { scope } from "nish/threads";

const sumOf = (xs: i32[]): i32 => {
  let t: i32 = 0;
  for (const x of xs) {
    t = t + x;
  }
  return t;
};

export const main = (): i32 => {
  const xs: i32[] = [1, 2, 3];
  const sums: i32[] = [0];
  {
    using s = scope();
    s.spawn(sumOf, xs, sums, 0);
    console.log(`${sums[0]}`);
  }
  return sums[0];
};
This reads `sums`, a destination of a task of `s`, before the scope joins: a scope's tasks run, and store their answers, when its block ends, and under Node each runs and stores where it is spawned, so a destination is a fresh `const` array that is never handed on, and between a scope's first `spawn` and the end of its block its thread neither reads a destination nor writes memory a task may read

NL2395

The message of an error contains:

…` copies bytes, so it needs an array whose elements are numbers (`u8[]`, `i32[]`, `f64[]`, ...), got …

nl2395_bytes_numbers_only.ts:

// NL2395: `set` and `fill` copy bytes, so the receiver's elements are numbers.
export const main = (): i32 => {
  const a: string[] = ["x"];
  a.fill("y");
  return 0;
};
`fill` copies bytes, so it needs an array whose elements are numbers (`u8[]`, `i32[]`, `f64[]`, ...), got string[]

NL2396

The message of an error contains:

`set` copies from an array of the receiver's element type, so it expects …

nl2396_bytes_set_source.ts:

// NL2396: `set` copies from an array of the receiver's own element type.
export const main = (): i32 => {
  const a: f64[] = [0.0];
  const b: u8[] = [1];
  a.set(b);
  return 0;
};
`set` copies from an array of the receiver's element type, so it expects f64[], got u8[]

NL2397

The message of an error contains:

`set` expects 1 or 2 arguments, got …

nl2397_bytes_set_arity.ts:

// NL2397: `set` takes a source and an optional offset.
export const main = (): i32 => {
  const a: u8[] = [0];
  a.set();
  return 0;
};
`set` expects 1 or 2 arguments, got 0

NL2398

The message of an error contains:

`fill` expects 1 to 3 arguments, got …

nl2398_bytes_fill_arity.ts:

// NL2398: `fill` takes a value and at most two ends.
export const main = (): i32 => {
  const a: u8[] = [0];
  a.fill(1, 0, 1, 2);
  return 0;
};
`fill` expects 1 to 3 arguments, got 4

NL2399

The message of an error contains:

…` takes u32 or u64 operands (convert with toU32 or toU64), got …

nl2399_ct_operand_type.ts:

// NL2399: the constant-time builtins take the unsigned words u32 and u64 only.
export const main = (): i32 => {
  const a: i32 = 1;
  const m: i32 = ctEq(a, a);
  return m;
};
`ctEq` takes u32 or u64 operands (convert with toU32 or toU64), got i32

NL2400

The message of an error contains:

…` needs every operand of one type, got …

nl2400_ct_mixed_operands.ts:

// NL2400: the mask and both candidates are one type.
export const main = (): i32 => {
  const m: u32 = 0;
  const a: u64 = 1;
  const r: u64 = ctSelect(m, a, a);
  return 0;
};
`ctSelect` needs every operand of one type, got u32 and u64

NL2401

The message of an error contains:

…` is refused: Nish has no `Date` object; the one `Date` member is `Date.now()`, the wall clock in milliseconds

nl2401_date_object.ts:

// NL2401: Nish has no `Date` object; `Date.now()` is its one member.
export const main = (): i32 => {
  const d = new Date();
  return 0;
};
`new Date()` is refused: Nish has no `Date` object; the one `Date` member is `Date.now()`, the wall clock in milliseconds

NL2402

The message of an error contains:

…` fills a `u8[]`, got …

nl2402_random_bytes.ts:

// NL2402: `crypto.getRandomValues` fills a u8[] and nothing wider.
export const main = (): i32 => {
  const words: i32[] = [0, 0];
  crypto.getRandomValues(words);
  return 0;
};
`crypto.getRandomValues` fills a `u8[]`, got i32[]

NL2403

The message of an error contains:

…` is refused: the one `crypto` member is `crypto.getRandomValues(bytes)`

nl2403_crypto_member.ts:

// NL2403: `crypto.getRandomValues` is the one `crypto` member.
export const main = (): i32 => {
  const id = crypto.randomUUID();
  return 0;
};
`crypto.randomUUID` is refused: the one `crypto` member is `crypto.getRandomValues(bytes)`

NL2404

The message of an error contains:

…` reaches the operating system, and a wasm32 build has none to reach

nl2404_host_on_wasm.ts (compiled with --target wasm32):

// NL2404: the host builtins reach the operating system, which a wasm32 build does not have.
export const now = (): f64 => Date.now();
`Date.now` reaches the operating system, and a wasm32 build has none to reach

NL2405

The message of an error contains:

…` sends from a `u8[]`, got …

nl2405_net_send_bytes.ts:

// NL2405: `netWrite` sends the bytes of a u8[] and nothing else.
export const main = (): i32 => {
  const words: i32[] = [0, 0];
  return netWrite(1, words, 0, 2);
};
`netWrite` sends from a `u8[]`, got i32[]

NL2406

The message of an error contains:

…` fills an `i32[]`, got …

nl2406_net_meta_words.ts:

// NL2406: `udpRecvFrom`'s `meta` is an i32[], the one `nish:net` argument
// that holds numbers rather than bytes.
export const main = (): i32 => {
  const buf: u8[] = new Array<u8>(4);
  const from: u8[] = new Array<u8>(18);
  const meta: f64[] = [0, 0];
  return udpRecvFrom(-1, buf, 0, 4, from, meta);
};
`udpRecvFrom` fills an `i32[]`, got f64[]

NL2415

The message of an error contains:

…`, whose length is fixed: declare it `…

nl2415_typed_array_push.ts:

// NL2415: a typed-array name has a fixed length, so it has no `push` or `pop`.
export const main = (): i32 => {
  const xs = new Float64Array(2);
  xs.push(1.0);
  return 0;
};
`push` is not a method of `Float64Array`, whose length is fixed: declare it `f64[]` to push and pop

NL2416

The message of an error contains:

`arena()` must be the initialiser of a `using` declaration, `using a = arena()`: the arena releases what was allocated after it when the block that declares it ends, and an `arena()` written anywhere else has no block to end

NL2417

The message of an error contains:

` is the binding of a `using` declaration of `arena()` and cannot be read: it holds the mark its block releases to when it ends, and nothing but that release may see or move the mark

NL2418

The message of an error contains:

`, which is declared outside the block: the block releases what it allocated when it ends, and the local would outlive it pointing into freed memory

NL2419

The message of an error contains:

`: the block releases what it allocated when it ends, and nothing proves the object it is stored into ends with it (allocate what must outlive the block before the block opens)

NL2420

The message of an error contains:

`: the block releases what it allocated when it ends, and nothing proves the array it is stored into ends with it (allocate what must outlive the block before the block opens)

NL2421

The message of an error contains:

… and is stored into memory — handed to a callee that keeps it, or held by an object or an array — where this analysis stops following it, so nothing proves it is gone when the block releases

NL2422

The message of an error contains:

…, and the array is older than the block: a `push` moves the array's storage into the arena, which the block releases when it ends (push only onto a `const` the block binds to a fresh array)

NL2423

The message of an error contains:

…: the block releases what it allocated before the function returns, so the caller would be handed freed memory

NL2424

The message of an error contains:

… and stores an allocation into memory, where this analysis stops following it: what it stores may be the block's and outlive the release

NL2425

The message of an error contains:

…, and this analysis cannot see what it does with memory, so nothing proves that what the block allocated is gone when it releases

No program provokes it: every callee the checker records has whole-program facts (a function, an instantiation, a lifted arrow, a declare function), so a using arena() block never calls one it cannot see; the refusal stays so that such a callee is never let through unexamined

NL2426

The message of an error contains:

…: the block releases to the mark it took when it opened, which a release or a reset in between has already moved the arena below

Retired: a using a = arena() block whose body calls Arena.release or Arena.reset, removed with them before 1.0; nothing a program can call moves the arena under the block's mark now

NL2427

The message of an error contains:

…, and that scope is declared outside the block: the task runs, and its answer is stored, when the scope joins, after the block has released what it allocated

NL2428

The message of an error contains:

…>` is not key material: a `Secret` holds an array of integers (`u8[]`, `u32[]`, ...) or a record whose every field is an integer, because `wipe` has to reach every byte of it with one store

NL2429

The message of an error contains:

` cannot hold a `Secret`: an element, a `Result` payload or a type argument outlives the function that made the secret, which then can neither return nor wipe it; hold a `Secret` in a local or a parameter

NL2430

The message of an error contains:

` cannot be a `Secret`: a field of a class or interface outlives the function that made the secret, which then can neither return nor wipe it; hold a `Secret` in a local, a parameter, or a `readonly` field of a class whose `wipe()` wipes it

NL2431

The message of an error contains:

A `Secret` cannot be interpolated into a template literal: what it holds would leave in the string. Compute with it inside `expose` and interpolate what that returns

NL2432

The message of an error contains:

` cannot be handed a `Secret`: a builtin reaches the outside world or reads the bytes, and a `Secret` is read only through `expose`

NL2433

The message of an error contains:

A `Secret` can be compared only with another `Secret` or with `null`: comparing it with a value would answer a question about the key, in time that depends on it. Use `timingSafeEqual` inside `expose`

NL2434

The message of an error contains:

A `Secret` cannot be a condition: a branch taken on key material is a timing channel, and a `Secret` is read only through `expose`

NL2435

The message of an error contains:

A `Secret` cannot be indexed or be an index: either reads key material outside `expose`, and an index that depends on a key is a cache-timing channel

NL2436

The message of an error contains:

` of a `Secret` is read only by `nish:secret` itself: compute with the value inside `expose(key, f)`, which runs `f` with it

NL2437

The message of an error contains:

A `Secret` is made by `secret(v)`, which moves `v` into it, not by `new`

NL2438

The message of an error contains:

`wipe` zeroes a `Secret`, or an array or record of integers, and `

NL2439

The message of an error contains:

` is made here and nobody owns it: a `Secret` leaves the function that made it returned or wiped, so bind it to a local (`const s = ...`) and `wipe(s)` once done, or return it

NL2440

The message of an error contains:

` that this exit leaves neither wiped nor returned: a `Secret` leaves the function that made it returned or wiped, on every path, so `wipe(…

NL2441

The message of an error contains:

` that can reach the end of its block neither wiped nor returned: a `Secret` leaves the function that made it returned or wiped, on every path, so `wipe(…

NL2442

The message of an error contains:

` is assigned again while the `Secret` it holds is neither wiped nor returned, and that secret would be lost unwiped: `wipe(…

NL2443

The message of an error contains:

` may have been wiped already, and a wiped `Secret` holds zeros: read it before `wipe`, or make a new one

NL2444

The message of an error contains:

` is a `Secret`, and a `Secret` is not copied: two names for one key would leave one of them to be wiped by nobody. Pass it to a function, return it, `wipe` it, or move a local the function made into another (`const j = k`), after which `k` is not read

NL2445

The message of an error contains:

… would be key material outside the `Secret`'s rules

NL2446

The message of an error contains:

` was moved into another `Secret` local and cannot be read again: one key has one owner, which is the one that wipes it

NL2447

The message of an error contains:

`secret` takes a value only it will hold: a fresh value (a call, `new`, a literal) or a local it moves, not a parameter, a field or an element, whose owner would keep the plain bytes

NL2448

The message of an error contains:

` runs may reach no I/O, no clock, no entropy, no C, and no `panic` or exit whose message or status is computed, so that what it returns is all that leaves

NL2449

The message of an error contains:

` hands it — returns it, stores it or passes it where it is kept — and the key would outlive the `Secret` unwiped; return a value computed from it instead

NL2450

The message of an error contains:

` writes the argument `exposeWith` hands it, a channel out of `expose` its result type does not name: return what it computes instead

NL2451

The message of an error contains:

` returns must be a declared type that holds no `Secret`: that type is the one thing that leaves

NL2452

The message of an error contains:

` returns is the one thing that leaves the secret, so it is written down rather than inferred

NL2453

The message of an error contains:

`: import `Secret`, `secret`, `expose`, `exposeWith` and `wipe` from `

NL2454

The message of an error contains:

…` takes i32 or i64 operands (convert with toI32 or toI64), got …

nl2454_wrapping_operand_type.ts:

// NL2454: the wrapping functions take the signed words i32 and i64 only.
import { wrappingAdd } from "nish:unsafe";

export const main = (): i32 => {
  const a: u32 = toU32(1);
  const b: u32 = wrappingAdd(a, a);
  return toI32(b);
};
`wrappingAdd` takes i32 or i64 operands (convert with toI32 or toI64), got u32

NL2455

The message of an error contains:

…` takes an array of numbers (i32, i64, u8, u16, u32, u64, f32 or f64), got …

nl2455_unchecked_receiver.ts:

// NL2455: an unchecked access reads or writes an array of numbers.
import { uncheckedGet } from "nish:unsafe";

export const main = (): i32 => {
  const names: string[] = ["a"];
  const name = uncheckedGet(names, 0);
  return name.length;
};
`uncheckedGet` takes an array of numbers (i32, i64, u8, u16, u32, u64, f32 or f64), got string[]

NL2456

The message of an error contains:

…` function and is called only through an import of it, so that each module that gives up a check or defines a wrap says so at its top: add `import { …

nl2456_unsafe_without_import.ts:

// NL2456: a nish:unsafe function is called only through an import of it.
export const main = (): i32 => {
  const xs: i32[] = [1];
  return uncheckedGet(xs, 0);
};
`uncheckedGet` is a `nish:unsafe` function and is called only through an import of it, so that each module that gives up a check or defines a wrap says so at its top: add `import { uncheckedGet } from "nish:unsafe"`

NL2457

The message of an error contains:

…` may panic here, and a module in the no-panic scope (`--deny-panics` or `noPanic`) may keep no panic site but out of memory: …

nl2457_deny_panics_index.ts (compiled with --deny-panics):

// --deny-panics refuses an index the checker cannot prove in range, and the
// error names the guard that would prove it.
export const at = (xs: i32[], i: i32): i32 => xs[i];
`index` may panic here, and a module in the no-panic scope (`--deny-panics` or `noPanic`) may keep no panic site but out of memory: `i` is not proven to be in range for `xs` here: guard it with a test that reaches the access — `if (i >= 0 && i < xs.length)` proves both ends, and an unsigned index needs only the upper one

nl2457_deny_panics_index_f64.ts (compiled with --deny-panics --number-mode f64):

// --deny-panics refuses a `number` index under `--number-mode f64`, and names
// no guard: a float index is never proven in range.
export const at = (xs: number[], i: number): number => xs[i];
`index` may panic here, and a module in the no-panic scope (`--deny-panics` or `noPanic`) may keep no panic site but out of memory: `i` is not proven to be in range for `xs` here: no guard removes the check on an index of type `f64`, because the compiler credits no compare of it with the length: an `i32` index, or a `u8`, `u16` or `u32` one, has a guard that does

nl2457_deny_panics_index_f64_i32.ts (compiled with --deny-panics --number-mode f64):

// --deny-panics under `--number-mode f64`: a length is an `f64`, so the guard
// for an `i32` index spells the bound `toI32(xs.length)`, which compiles and
// is credited in both modes.
export const at = (xs: number[], i: i32): number => xs[i];
`index` may panic here, and a module in the no-panic scope (`--deny-panics` or `noPanic`) may keep no panic site but out of memory: `i` is not proven to be in range for `xs` here: guard it with a test that reaches the access — `if (i >= 0 && i < toI32(xs.length))` proves both ends, and an unsigned index needs only the upper one

nl2457_deny_panics_index_f64_ranged.ts (compiled with --deny-panics --number-mode f64):

// --deny-panics under `--number-mode f64`: a ranged index is an `i32`, so its
// guard spells the bound `toI32(xs.length)` as an `i32` index's does.
export const at = (xs: i32[], k: i32): i32 => {
  if (k >= 0 && k <= 9) {
    const i: integer<0, 9> = k
    return xs[i]
  }
  return 0
}
`index` may panic here, and a module in the no-panic scope (`--deny-panics` or `noPanic`) may keep no panic site but out of memory: `i` is not proven to be in range for `xs` here: guard it with a test that reaches the access — `if (i >= 0 && i < toI32(xs.length))` proves both ends, and an unsigned index needs only the upper one

nl2457_deny_panics_index_f64_shadowed_to_i32.ts (compiled with --deny-panics --number-mode f64):

// --deny-panics under `--number-mode f64`, in a module that declares its own
// `toI32`: the credited guard would call it rather than the builtin, so the
// error names no guard.
const toI32 = (x: number): number => x

export const at = (xs: number[], i: i32): number => xs[i] + toI32(0);
`index` may panic here, and a module in the no-panic scope (`--deny-panics` or `noPanic`) may keep no panic site but out of memory: `i` is not proven to be in range for `xs` here: no guard removes the check here, because the one the compiler credits for this index calls the builtin `toI32`, which this module's own `toI32` hides: rename it to guard the access

nl2457_deny_panics_index_shadowed_to_u32.ts (compiled with --deny-panics):

// --deny-panics refuses a `u32` index the checker cannot prove in range, and
// names no guard: the module declares its own `toU32`, which the credited
// `toU32(i) < toU32(xs.length)` would call instead of the builtin.
const toU32 = (x: i32): i32 => x

export const at = (xs: i32[], i: u32): i32 => xs[i] + toU32(0);
`index` may panic here, and a module in the no-panic scope (`--deny-panics` or `noPanic`) may keep no panic site but out of memory: `i` is not proven to be in range for `xs` here: no guard removes the check here, because the one the compiler credits for this index calls the builtin `toU32`, which this module's own `toU32` hides: rename it to guard the access

nl2457_deny_panics_index_u64.ts (compiled with --deny-panics):

// --deny-panics refuses a `u64` index the checker cannot prove in range, and
// names no guard: none both type-checks against the length and is credited.
export const at = (xs: i32[], i: u64): i32 => xs[i];
`index` may panic here, and a module in the no-panic scope (`--deny-panics` or `noPanic`) may keep no panic site but out of memory: `i` is not proven to be in range for `xs` here: no guard removes the check on an index of type `u64`, because the compiler credits no compare of it with the length: an `i32` index, or a `u8`, `u16` or `u32` one, has a guard that does

NL2458

The message of an error contains:

…, outside the no-panic scope this call is in, where nothing the caller proves can remove it: call a function that cannot panic, or make the call from a module outside the scope

nl2458_deny_panics_call.ts (compiled with --deny-panics):

// --deny-panics refuses a call into the standard library that may panic
// (NL2458): the callee is outside the scope, so the call is where it is refused.
import { trim } from "nish/text";

export const tidy = (s: string): string => trim(s);
This call to `trim` may panic: it reaches the `index` site at std/text.ts:163:43, outside the no-panic scope this call is in, where nothing the caller proves can remove it: call a function that cannot panic, or make the call from a module outside the scope

NL2459

The message of an error contains:

…`, which the capability policy does not grant (…

nl2459_policy_allowlist.ts (compiled with --allow clock):

// An allowlist refuses what it does not list (NL2459): `--allow clock` and a
// `main` that also reaches `exit`.
const stop = (code: i32): void => {
  process.exit(code);
};

export const main = (): number => {
  if (Date.now() < 0) {
    stop(3);
  }
  return 0;
};
`main` reaches `exit`, which the capability policy does not grant (`--allow clock` does not list it); the chain that reaches it: nl2459_policy_allowlist.ts:9:5 calls stop, nl2459_policy_allowlist.ts:4:3 calls process.exit

nl2459_policy_deny.ts (compiled with --deny fs.read):

// `--deny fs.read` refuses a program whose `main` reaches `fs.read` through a
// helper (NL2459): spanned at `main`'s call, with the whole chain named.
const settings = (): string => readFileSync("settings.txt");

export const main = (): number => settings().length;
`main` reaches `fs.read`, which the capability policy does not grant (`--deny fs.read`); the chain that reaches it: nl2459_policy_deny.ts:5:35 calls settings, nl2459_policy_deny.ts:3:32 calls readFileSync

nl2459_policy_library.ts (compiled with --deny clock):

// A library entry has no `main`, and every export a host can call is judged:
// `stamp` reaches `clock`, which `--deny clock` refuses (NL2459).
export const stamp = (): f64 => Date.now();

export const twice = (n: i32): i32 => n * 2;
`stamp` reaches `clock`, which the capability policy does not grant (`--deny clock`); the chain that reaches it: nl2459_policy_library.ts:3:33 calls Date.now

nl2459_policy_unexported.ts (compiled with --no-strict-exports --deny fs.read):

// Under `--no-strict-exports` every function has external linkage, so in an
// open build a host can call one nobody exported: the private `load` reaches
// `fs.read`, which `--deny fs.read` refuses (NL2459).
const load = (path: string): string => readFileSync(path);

export const size = (): i32 => 3;
`load` reaches `fs.read`, which the capability policy does not grant (`--deny fs.read`); the chain that reaches it: nl2459_policy_unexported.ts:4:40 calls readFileSync

NL2460

The message of an error contains:

`Math.clz32` expects an i32 or a u32, got …

nl2460_math_clz32_arg.ts:

// NL2460: `Math.clz32` counts the leading zeros of a 32-bit integer, so a 64-bit one is refused.
export const main = (): i32 => {
  const x: i64 = 1;
  return Math.clz32(x);
};
`Math.clz32` expects an i32 or a u32, got i64 (convert with toI32 or toU32)

NL2461

The message of an error contains:

`indexOf` expects 1 or 2 arguments, got …

NL2462

The message of an error contains:

…finding, because the arena memory it describes stays allocated after nothing in the program can reach it any more…

nl2462_deny_retention.ts (compiled with --deny-retention):

// NL2462: under --deny-retention, an arena-retention warning in the program's
// own code is an error that keeps the warning's code and text.
export const lastLine = (rows: i32): i32 => {
  let last = "";
  for (let i = 0; i < rows; i++) {
    last = `row ${i}`;
  }
  return last.length;
};
`--deny-retention` refuses this NL9016 finding, because the arena memory it describes stays allocated after nothing in the program can reach it any more: `last` is given a new allocation on every pass of this loop and drops the one the pass before gave it: nothing can reach that value again and nothing frees it while the loop runs, so memory grows with every pass. Declare `last` with `const` inside the loop when only its pass reads it, or keep what the last pass needs as numbers and build the value once after the loop

NL2463

The message of an error contains:

`new Mutex` takes a value that is fresh all the way down — a `new` expression or a literal whose every argument or element is a number, a `boolean`, an enum, a string literal or itself fresh — because the data a `Mutex` owns is reachable only through its guard, and a value the program already holds would be a path to it that skips the lock…

nl2463_mutex_not_fresh.ts:

// NL2463: `new Mutex` takes a fresh value. `c` is still the program's after the
// lock owns it, so `c.n = 1` would write the data without the lock.
import { Mutex } from "nish/threads";

class Counter {
  n: i32 = 0;
}

export const main = (): i32 => {
  const c = new Counter();
  const m = new Mutex<Counter>(c);
  c.n = 1;
  using g = m.lock();
  return g.value.n;
};
`new Mutex` takes a value that is fresh all the way down — a `new` expression or a literal whose every argument or element is a number, a `boolean`, an enum, a string literal or itself fresh — because the data a `Mutex` owns is reachable only through its guard, and a value the program already holds would be a path to it that skips the lock; build the value inside the call, `new Mutex<Hist>(new Hist(new Array<i32>(16)))`, or copy what it needs into a fresh one, `new Mutex<Hist>(new Hist(h.n))`

NL2464

The message of an error contains:

…` is not a member a program may name: the data a `Mutex` owns is reachable only through the guard its `lock()` answers, `using g = m.lock()`, so every path to it takes the lock

nl2464_mutex_member.ts:

// NL2464: a `Mutex` has no member a program may name but `lock`: its guard,
// reached as a field, would be a path to the data that skips the lock.
import { Mutex } from "nish/threads";

class Counter {
  n: i32 = 0;
}

export const main = (): i32 => {
  const m = new Mutex<Counter>(new Counter());
  m.guard.value.n = 1;
  return 0;
};
`guard` is not a member a program may name: the data a `Mutex` owns is reachable only through the guard its `lock()` answers, `using g = m.lock()`, so every path to it takes the lock

NL2465

The message of an error contains:

`lock()` must be the initialiser of a `using` declaration: the lock is released when the block that declares it ends, so a guard bound any other way would be one nobody releases…

nl2465_mutex_lock_not_using.ts:

// NL2465: `lock()` is only the initialiser of a `using` declaration; a guard bound
// by `const` is one no block end releases.
import { Mutex } from "nish/threads";

class Counter {
  n: i32 = 0;
}

export const main = (): i32 => {
  const m = new Mutex<Counter>(new Counter());
  const g = m.lock();
  return 0;
};
`lock()` must be the initialiser of a `using` declaration: the lock is released when the block that declares it ends, so a guard bound any other way would be one nobody releases; declare it as `using g = m.lock()`

NL2466

The message of an error contains:

….value` only as the base of a field or element read or store: passed, bound, stored, returned or walked, what it reaches would outlive the block that holds the lock…

nl2466_mutex_guard_passed.ts:

// NL2466: a guard is used only as `g.value`, or handed to a helper whose
// parameter is a `MutexGuard<T>`. Stored in a field, it could be kept past the
// block that holds the lock.
import { Mutex, MutexGuard } from "nish/threads";

class Counter {
  n: i32 = 0;
}

class Box {
  g: MutexGuard<Counter> | null = null;
}

export const main = (): i32 => {
  const m = new Mutex<Counter>(new Counter());
  const box = new Box();
  {
    using g = m.lock();
    box.g = g;
  }
  return 0;
};
A guard is used only as `g.value`, and `g.value` only as the base of a field or element read or store: passed, bound, stored, returned or walked, what it reaches would outlive the block that holds the lock; write `g.value.n` where the value is used, or hand `g` to a helper whose parameter is a `MutexGuard<Counter>`

NL2467

The message of an error contains:

…` through a guard, whose chains are only the base of a further field or element read or store: a value that is not a number, a `boolean`, an enum or a `string`, which the read copies, would outlive the block that holds the lock…

nl2467_mutex_guard_alias.ts:

// NL2467: what a guard reaches is only the base of a field or element access. `c`
// would hold the data after the block that holds the lock has ended.
import { Mutex } from "nish/threads";

class Counter {
  n: i32 = 0;
}

export const main = (): i32 => {
  const m = new Mutex<Counter>(new Counter());
  let n: i32 = 0;
  {
    using g = m.lock();
    const c = g.value;
    n = c.n;
  }
  return n;
};
This reads `Counter` through a guard, whose chains are only the base of a further field or element read or store: a value that is not a number, a `boolean`, an enum or a `string`, which the read copies, would outlive the block that holds the lock; do the work inside the guard's block, or read the numbers and strings it holds one at a time, `const c = g.value.n`

nl2467_mutex_guard_nullable.ts:

// NL2467: a nullable field read through a guard is a pointer that would outlive the lock.
import { Mutex } from "nish/threads";

class Log {
  last: string | null = null;
  n: i32 = 0;
}

export const main = (): i32 => {
  const m = new Mutex<Log>(new Log());
  let n: i32 = 0;
  {
    using g = m.lock();
    const last = g.value.last;
    if (last !== null) {
      n = last.length;
    }
  }
  return n;
};
This reads `string | null` through a guard, whose chains are only the base of a further field or element read or store: a value that is not a number, a `boolean`, an enum or a `string`, which the read copies, would outlive the block that holds the lock; declare the field a `string` with `""` for none, `last: string = ""`

NL2468

The message of an error contains:

…` through a guard, which stores only a number, a `boolean`, an enum or a `string`, which the store copies, into a field or an element that already exists: anything else is a pointer, which may point into a task's arena, freed when the task is joined…

nl2468_mutex_guard_store.ts:

// NL2468: a store through a guard stores a scalar or a string, which it copies. An
// array of strings a task built lives in its arena, which is freed when the task is
// joined.
import { Mutex } from "nish/threads";

class Named {
  names: string[];
  constructor() {
    this.names = [];
  }
}

export const main = (): i32 => {
  const m = new Mutex<Named>(new Named());
  {
    using g = m.lock();
    g.value.names = ["x"];
  }
  return 0;
};
This stores `string[]` through a guard, which stores only a number, a `boolean`, an enum or a `string`, which the store copies, into a field or an element that already exists: anything else is a pointer, which may point into a task's arena, freed when the task is joined; build the value into the one the `Mutex` is made with, or store what it holds one at a time into slots that already exist, `g.value.names[0] = "x"`

NL2469

The message of an error contains:

…` is called through a guard, which reads and stores fields and elements and calls no method: a method could keep what it reaches, or grow an array into a task's arena, which is freed when it is joined…

nl2469_mutex_guard_method.ts:

// NL2469: no method is called through a guard. `push` can move the array's storage
// into the arena of the task that holds the lock, which is freed at the join.
import { Mutex } from "nish/threads";

class Bins {
  xs: i32[];

  constructor() {
    this.xs = [0, 0];
  }
}

export const main = (): i32 => {
  const m = new Mutex<Bins>(new Bins());
  {
    using g = m.lock();
    g.value.xs.push(1);
  }
  return 0;
};
`push` is called through a guard, which reads and stores fields and elements and calls no method: a method could keep what it reaches, or grow an array into a task's arena, which is freed when it is joined; write what it does as reads and stores through the guard, into slots that already exist, `g.value.xs[0] = 1`

NL2470

The message of an error contains:

…: inside a guard's block, directly or through any function it calls, there is no second `lock()`, no `scope()` or `spawn`, and no `parallelMapInto` or `parallelReduce`, so one lock is held at a time and nothing is waited on while it is held…

nl2470_mutex_lock_held.ts:

// NL2470: one lock is held at a time. A second `lock()` inside a guard's block is
// how two tasks taking the same two locks in opposite orders deadlock.
import { Mutex } from "nish/threads";

class Counter {
  n: i32 = 0;
}

export const main = (): i32 => {
  const a = new Mutex<Counter>(new Counter());
  const b = new Mutex<Counter>(new Counter());
  {
    using ga = a.lock();
    using gb = b.lock();
    ga.value.n = gb.value.n;
  }
  return 0;
};
A second `lock()` is reached while `ga` holds its lock: inside a guard's block, directly or through any function it calls, there is no second `lock()`, no `scope()` or `spawn`, and no `parallelMapInto` or `parallelReduce`, so one lock is held at a time and nothing is waited on while it is held; move `b.lock()` below the end of the block that opens with `using ga = a.lock()`, which releases the lock

NL2471

The message of an error contains:

…, and `spawn` runs it beside the scope's other tasks: make scratch only this task uses inside it…

nl2471_mutex_task_unguarded.ts:

// NL2471: a task writes shared memory only through a guard. `hits` is the parent's
// array, written here without a lock while the other task writes it too.
import { Mutex, scope } from "nish/threads";

class Counter {
  n: i32 = 0;
}

class Job {
  total: Mutex<Counter>;
  hits: i32[];
  constructor(total: Mutex<Counter>, hits: i32[]) {
    this.total = total;
    this.hits = hits;
  }
}

const count = (job: Job): i32 => {
  {
    using g = job.total.lock();
    g.value.n = g.value.n + 1;
  }
  job.hits[0] = job.hits[0] + 1;
  return 0;
};

export const main = (): i32 => {
  const total = new Mutex<Counter>(new Counter());
  const hits: i32[] = [0];
  const done: i32[] = [0, 0];
  {
    using s = scope();
    s.spawn(count, new Job(total, hits), done, 0);
    s.spawn(count, new Job(total, hits), done, 1);
  }
  return hits[0];
};
`count` writes memory its caller can see at tests/wordings/nl2471_mutex_task_unguarded.ts:23:3, and `spawn` runs it beside the scope's other tasks: make scratch only this task uses inside it, because an object handed in as the argument is shared memory even when the `spawn` made it, since where an argument was made is not tracked. Write memory the tasks share only through the guard of a `Mutex`; hand a value the parent needs back as the task's answer, or run it on the thread that called it, `done[0] = count(new Job(total, hits))`

NL2472

The message of an error contains:

…`, before the scope joins: natively the scope's tasks run when its block ends and under Node each runs where it is spawned, so the parent locks what they share only before the statement that holds the first `spawn` or after the join…

nl2472_mutex_region_lock.ts:

// NL2472: after a scope's first `spawn` the parent does not lock what its tasks
// share. Natively the task has not run here and under Node it has, so `seen`
// would be 0 compiled and 1 under Node.
import { Mutex, scope } from "nish/threads";

class Counter {
  n: i32 = 0;
}

const bump = (m: Mutex<Counter>): i32 => {
  using g = m.lock();
  g.value.n = g.value.n + 1;
  return 0;
};

export const main = (): i32 => {
  const m = new Mutex<Counter>(new Counter());
  const done: i32[] = [0];
  let seen: i32 = 0;
  {
    using s = scope();
    s.spawn(bump, m, done, 0);
    {
      using g = m.lock();
      seen = g.value.n;
    }
  }
  return seen;
};
This `lock()` locks a `Mutex` a task of `s` can reach through `arg`, before the scope joins: natively the scope's tasks run when its block ends and under Node each runs where it is spawned, so the parent locks what they share only before the statement that holds the first `spawn` or after the join; move `m.lock()` above the block that opens with `using s = scope()`, or below the end of that block

NL2473

The message of an error contains:

…`: a `Mutex` reaches a task as its argument or as a field of the class its argument is, never deeper, so which tasks share a lock is a question the checker can answer…

nl2473_mutex_task_reach.ts:

// NL2473: a `Mutex` reaches a task as its argument or as a field of the class its
// argument is. An array of them would make which tasks share a lock a question
// about indices rather than types.
import { Mutex, scope } from "nish/threads";

class Counter {
  n: i32 = 0;
}

const first = (ms: Mutex<Counter>[]): i32 => {
  using g = ms[0].lock();
  g.value.n = g.value.n + 1;
  return 0;
};

export const main = (): i32 => {
  const ms: Mutex<Counter>[] = [new Mutex<Counter>(new Counter())];
  const done: i32[] = [0];
  {
    using s = scope();
    s.spawn(first, ms, done, 0);
  }
  return 0;
};
The argument of `spawn` is `Mutex<Counter>[]`, which reaches a `Mutex` through `ms[i]`: a `Mutex` reaches a task as its argument or as a field of the class its argument is, never deeper, so which tasks share a lock is a question the checker can answer; hand the task the `Mutex` itself, `s.spawn(first, ms[i], done, 0)`, and declare its parameter a `Mutex` of the same data

NL2474

The message of an error contains:

…` runs it on several threads at once: a lock is taken by a scope's task or by the thread that opened the scope, never by a parallel body…

nl2474_mutex_parallel_body.ts:

// NL2474: A lock is taken by a scope's task or by the thread that opened the scope,
// never by the body of `parallelMapInto`, which runs on many threads at once.
import { Mutex, parallelMapInto } from "nish/threads";

class Counter {
  n: i32 = 0;
}

class Item {
  total: Mutex<Counter>;
  x: i32;
  constructor(total: Mutex<Counter>, x: i32) {
    this.total = total;
    this.x = x;
  }
}

const tally = (it: Item): i32 => {
  using g = it.total.lock();
  g.value.n = g.value.n + it.x;
  return it.x;
};

export const main = (): i32 => {
  const total = new Mutex<Counter>(new Counter());
  const src: Item[] = [new Item(total, 1), new Item(total, 2)];
  const dst: i32[] = [0, 0];
  parallelMapInto(src, dst, tally);
  return dst[1];
};
`tally` takes a `Mutex`'s lock, and `parallelMapInto` runs it on several threads at once: a lock is taken by a scope's task or by the thread that opened the scope, never by a parallel body; run it as a scope's tasks instead, `{ using s2 = scope(); for (let i = 0; i < src.length; i++) { s2.spawn(tally, src[i], dst, i) } }`

NL2475

The message of an error contains:

…`: a sender's arena is freed when its scope joins, and only a string is copied out of it on the way, so anything else would point into freed memory…

nl2475_channel_element.ts:

// NL2475, C1: a channel carries a number, a `boolean`, an enum or a `string`, which
// a send copies in and a receive copies out. An array of strings is a pointer
// into the arena of the task that built it, and its elements are pointers too.
import { Channel, scope } from "nish/threads";

class Pipe {
  ch: Channel<i32>;
  n: i32;

  constructor(ch: Channel<i32>, n: i32) {
    this.ch = ch;
    this.n = n;
  }
}

const produce = (p: Pipe): i32 => {
  for (let i: i32 = 1; i <= p.n; i++) {
    p.ch.send(i);
  }
  return p.n;
};

const consume = (p: Pipe): i32 => {
  let sum: i32 = 0;
  for (const x of p.ch) {
    sum = sum + x;
  }
  return sum;
};

export const main = (): i32 => {
  const out: i32[] = [0];
  const ch = new Channel<i32>();
  const words = new Channel<string[]>();
  {
    using s = scope();
    s.spawn(produce, new Pipe(ch, 1), out, 0);
  }
  for (const w of words) {
    console.log(w[0]);
  }
  return out[0];
};
A `Channel` carries a number, a `boolean`, an enum or a `string`, and this one would carry `string[]`: a sender's arena is freed when its scope joins, and only a string is copied out of it on the way, so anything else would point into freed memory; keep the values in an array the receiver can read and send each one's index, `const words = new Channel<i32>()`

NL2476

The message of an error contains:

…: a channel is a `const` declared in the block that holds its scope's `using s = scope()`, or in the block that has the scope's block as one of its statements, before it, so each run of the declaration makes a channel for one run of one scope…

nl2476_channel_not_const.ts:

// NL2476, C2: `new Channel` is the initialiser of a `const`. A `let` could be given
// another channel, and the scope would close the one it was given first.
import { Channel, scope } from "nish/threads";

class Pipe {
  ch: Channel<i32>;
  n: i32;

  constructor(ch: Channel<i32>, n: i32) {
    this.ch = ch;
    this.n = n;
  }
}

const produce = (p: Pipe): i32 => {
  for (let i: i32 = 1; i <= p.n; i++) {
    p.ch.send(i);
  }
  return p.n;
};

const consume = (p: Pipe): i32 => {
  let sum: i32 = 0;
  for (const x of p.ch) {
    sum = sum + x;
  }
  return sum;
};

export const main = (): i32 => {
  const out: i32[] = [0, 0];
  let ch = new Channel<i32>();
  {
    using s = scope();
    s.spawn(produce, new Pipe(ch, 3), out, 0);
    s.spawn(consume, new Pipe(ch, 0), out, 1);
  }
  return out[1];
};
`new Channel` must be the initialiser of a `const`: a channel is a `const` declared in the block that holds its scope's `using s = scope()`, or in the block that has the scope's block as one of its statements, before it, so each run of the declaration makes a channel for one run of one scope; write `const ch = new Channel<i32>()` on the line before `using s = scope()`

NL2477

The message of an error contains:

…: a channel is used only as the receiver of `send`, the iterable of a `for...of`, an argument, or a field its class's constructor sets from a parameter, so it is never bound, stored, returned or captured, and which tasks reach it is a question the checker can answer…

nl2477_channel_bound.ts:

// NL2477, C2: a channel is never bound to a second name. A task that kept one could
// hand it on where the checker cannot follow it.
import { Channel, scope } from "nish/threads";

class Pipe {
  ch: Channel<i32>;
  n: i32;

  constructor(ch: Channel<i32>, n: i32) {
    this.ch = ch;
    this.n = n;
  }
}

const produce = (p: Pipe): i32 => {
  for (let i: i32 = 1; i <= p.n; i++) {
    p.ch.send(i);
  }
  return p.n;
};

const consume = (p: Pipe): i32 => {
  let sum: i32 = 0;
  for (const x of p.ch) {
    sum = sum + x;
  }
  return sum;
};

const relay = (p: Pipe): i32 => {
  const c = p.ch;
  c.send(1);
  return 1;
};

export const main = (): i32 => {
  const out: i32[] = [0, 0];
  const ch = new Channel<i32>();
  {
    using s = scope();
    s.spawn(relay, new Pipe(ch, 1), out, 0);
    s.spawn(consume, new Pipe(ch, 0), out, 1);
  }
  return out[1];
};
A `Channel` cannot be bound, stored, returned or compared here: a channel is used only as the receiver of `send`, the iterable of a `for...of`, an argument, or a field its class's constructor sets from a parameter, so it is never bound, stored, returned or captured, and which tasks reach it is a question the checker can answer; hand it to the task as its argument, `s.spawn(task, ch)`, or as a field the argument's constructor sets, `s.spawn(task, new Work(ch, n))`

NL2478

The message of an error contains:

`new Channel` takes no capacity: a channel is unbounded and `send` never waits, because under Node a scope's tasks run one at a time and nothing would drain a channel that was full…

nl2478_channel_capacity.ts:

// NL2478, C3: a channel is unbounded. Under Node nothing would drain a full one.
import { Channel, scope } from "nish/threads";

class Pipe {
  ch: Channel<i32>;
  n: i32;

  constructor(ch: Channel<i32>, n: i32) {
    this.ch = ch;
    this.n = n;
  }
}

const produce = (p: Pipe): i32 => {
  for (let i: i32 = 1; i <= p.n; i++) {
    p.ch.send(i);
  }
  return p.n;
};

const consume = (p: Pipe): i32 => {
  let sum: i32 = 0;
  for (const x of p.ch) {
    sum = sum + x;
  }
  return sum;
};

export const main = (): i32 => {
  const out: i32[] = [0, 0];
  const ch = new Channel<i32>(16);
  {
    using s = scope();
    s.spawn(produce, new Pipe(ch, 3), out, 0);
    s.spawn(consume, new Pipe(ch, 0), out, 1);
  }
  return out[1];
};
`new Channel` takes no capacity: a channel is unbounded and `send` never waits, because under Node a scope's tasks run one at a time and nothing would drain a channel that was full; write `new Channel<i32>()`

NL2479

The message of an error contains:

…: a channel is reachable from the tasks of one scope, the parent sends on it only before the block that holds that scope's spawns, and nobody closes it: it closes once the scope has started to join and every task that may send on it has returned…

nl2479_channel_ctor_this.ts:

// NL2479, C4: a constructor of a class that holds a channel uses `this` only through its own fields.
import { Channel, scope } from "nish/threads";

class Pipe {
  ch: Channel<i32>;
  n: i32;

  constructor(ch: Channel<i32>, n: i32) {
    this.ch = ch;
    this.n = n;
    remember(this);
  }
}

const remember = (p: Pipe): void => {
  p.n = p.n + 1;
};

const produce = (p: Pipe): i32 => {
  p.ch.send(p.n);
  return p.n;
};

export const main = (): i32 => {
  const out: i32[] = [0];
  const ch = new Channel<i32>();
  {
    using s = scope();
    s.spawn(produce, new Pipe(ch, 3), out, 0);
  }
  return out[0];
};
The constructor of a class that holds a channel uses `this` other than to read or set its own fields, `this.f`, where a send made through it could not be counted: a channel is reachable from the tasks of one scope, the parent sends on it only before the block that holds that scope's spawns, and nobody closes it: it closes once the scope has started to join and every task that may send on it has returned; set only the object's own fields in the constructor, and move `remember(this)` to below the `new` that makes the object, naming the object where it names `this`

nl2479_channel_parent_send.ts:

// NL2479, C4: the parent sends only before the block that holds the scope's spawns.
// Inside it, natively the tasks have not run, and under Node they have.
import { Channel, scope } from "nish/threads";

class Pipe {
  ch: Channel<i32>;
  n: i32;

  constructor(ch: Channel<i32>, n: i32) {
    this.ch = ch;
    this.n = n;
  }
}

const produce = (p: Pipe): i32 => {
  for (let i: i32 = 1; i <= p.n; i++) {
    p.ch.send(i);
  }
  return p.n;
};

const consume = (p: Pipe): i32 => {
  let sum: i32 = 0;
  for (const x of p.ch) {
    sum = sum + x;
  }
  return sum;
};

export const main = (): i32 => {
  const out: i32[] = [0];
  const ch = new Channel<i32>();
  {
    using s = scope();
    s.spawn(consume, new Pipe(ch, 0), out, 0);
    ch.send(7);
  }
  return out[0];
};
This sends on `ch` once its scope's block has begun: a channel is reachable from the tasks of one scope, the parent sends on it only before the block that holds that scope's spawns, and nobody closes it: it closes once the scope has started to join and every task that may send on it has returned; move `ch.send(7)` above the block that opens with `using s = scope()`

nl2479_channel_spawn_param.ts:

// NL2479, C4: a `spawn` hands its task only a channel the spawning function declared.
import { Channel, scope } from "nish/threads";

class Pipe {
  ch: Channel<i32>;

  constructor(ch: Channel<i32>) {
    this.ch = ch;
  }
}

const produce = (p: Pipe): i32 => {
  p.ch.send(1);
  return 1;
};

const run = (ch: Channel<i32>): i32 => {
  const out: i32[] = [0];
  {
    using s = scope();
    s.spawn(produce, new Pipe(ch), out, 0);
  }
  return out[0];
};

export const main = (): i32 => {
  const ch = new Channel<i32>();
  return run(ch);
};
A `spawn` hands its task only a channel this function declared: a channel is reachable from the tasks of one scope, the parent sends on it only before the block that holds that scope's spawns, and nobody closes it: it closes once the scope has started to join and every task that may send on it has returned; declare the channel in the function that opens the scope, `const ch2 = new Channel<i32>()`, and hand the task that one

nl2479_channel_two_scopes.ts:

// NL2479, C4: a channel is reachable from the tasks of one scope only.
import { Channel, scope } from "nish/threads";

const produce = (ch: Channel<i32>): i32 => {
  ch.send(1);
  return 1;
};

export const main = (): i32 => {
  const one: i32[] = [0];
  const two: i32[] = [0];
  const ch = new Channel<i32>();
  {
    using a = scope();
    a.spawn(produce, ch, one, 0);
  }
  {
    using b = scope();
    b.spawn(produce, ch, two, 0);
  }
  return one[0] + two[0];
};
`ch` is reachable from the tasks of two scopes: a channel is reachable from the tasks of one scope, the parent sends on it only before the block that holds that scope's spawns, and nobody closes it: it closes once the scope has started to join and every task that may send on it has returned; spawn every task that reaches `ch` on one scope, `a.spawn(produce, ch, two, 0)`

NL2480

The message of an error contains:

…: a channel has one receiver, either one task spawned outside any loop or the parent in a later statement of the block that has the scope's block as one of its statements, and inside a scope's block the parent receives nothing, so the parent's one wait is the join…

nl2480_channel_two_receivers.ts:

// NL2480, C5: a channel has one receiver. Under Node the first would take everything.
import { Channel, scope } from "nish/threads";

class Pipe {
  ch: Channel<i32>;
  n: i32;

  constructor(ch: Channel<i32>, n: i32) {
    this.ch = ch;
    this.n = n;
  }
}

const produce = (p: Pipe): i32 => {
  for (let i: i32 = 1; i <= p.n; i++) {
    p.ch.send(i);
  }
  return p.n;
};

const consume = (p: Pipe): i32 => {
  let sum: i32 = 0;
  for (const x of p.ch) {
    sum = sum + x;
  }
  return sum;
};

export const main = (): i32 => {
  const out: i32[] = [0, 0, 0];
  const ch = new Channel<i32>();
  {
    using s = scope();
    s.spawn(produce, new Pipe(ch, 3), out, 0);
    s.spawn(consume, new Pipe(ch, 0), out, 1);
    s.spawn(consume, new Pipe(ch, 0), out, 2);
  }
  return out[1] + out[2];
};
`ch` has a second receiver here: a channel has one receiver, either one task spawned outside any loop or the parent in a later statement of the block that has the scope's block as one of its statements, and inside a scope's block the parent receives nothing, so the parent's one wait is the join; receive in one task spawned outside the loop, or after the scope's block, `{ using s = scope(); ... } for (const x of ch) { ... }`

NL2481

The message of an error contains:

…: under Node a task runs where it is spawned, so every task that may send on a channel is spawned, in the block's statement order, before the one task that receives on it, which is also what keeps channels from deadlocking…

nl2481_channel_receiver_first.ts:

// NL2481, C6: every sender is spawned before the receiver. Under Node the receiver
// runs at its spawn, and would find the channel empty.
import { Channel, scope } from "nish/threads";

class Pipe {
  ch: Channel<i32>;
  n: i32;

  constructor(ch: Channel<i32>, n: i32) {
    this.ch = ch;
    this.n = n;
  }
}

const produce = (p: Pipe): i32 => {
  for (let i: i32 = 1; i <= p.n; i++) {
    p.ch.send(i);
  }
  return p.n;
};

const consume = (p: Pipe): i32 => {
  let sum: i32 = 0;
  for (const x of p.ch) {
    sum = sum + x;
  }
  return sum;
};

export const main = (): i32 => {
  const out: i32[] = [0, 0];
  const ch = new Channel<i32>();
  {
    using s = scope();
    s.spawn(consume, new Pipe(ch, 0), out, 0);
    s.spawn(produce, new Pipe(ch, 3), out, 1);
  }
  return out[0];
};
`produce` may send on `ch` and is spawned after its receiver: under Node a task runs where it is spawned, so every task that may send on a channel is spawned, in the block's statement order, before the one task that receives on it, which is also what keeps channels from deadlocking; move the `spawn` of the receiving task below every `spawn` that sends, `s.spawn(produce, new Pipe(ch, 3), out, 1)` above `s.spawn(consume, new Pipe(ch, 0), out, 0)`

NL2482

The message of an error contains:

… holds its lock: a receive waits, and nothing waits while a lock is held, so there is no receive inside a guard's block, directly or through any function it calls (a `send` never waits, and may stay)

nl2482_channel_loop_held.ts:

// NL2482, C7: nothing receives while a lock is held, here a loop over a channel written
// in the guard's block: it waits, and the lock's holder must not.
import { Channel, Mutex, scope } from "nish/threads";

class Tally {
  n: i32 = 0;
}

class Job {
  ch: Channel<i32>;
  tally: Mutex<Tally>;

  constructor(ch: Channel<i32>, tally: Mutex<Tally>) {
    this.ch = ch;
    this.tally = tally;
  }
}

const count = (j: Job): i32 => {
  using g = j.tally.lock();
  for (const x of j.ch) {
    g.value.n = g.value.n + x;
  }
  return g.value.n;
};

export const main = (): i32 => {
  const out: i32[] = [0];
  const ch = new Channel<i32>();
  const tally = new Mutex<Tally>(new Tally());
  ch.send(4);
  {
    using s = scope();
    s.spawn(count, new Job(ch, tally), out, 0);
  }
  return out[0];
};
A loop over a channel is reached while `g` holds its lock: a receive waits, and nothing waits while a lock is held, so there is no receive inside a guard's block, directly or through any function it calls (a `send` never waits, and may stay)

NL2483

The message of an error contains:

…: no task both sends and receives on one channel, because a channel closes only once every task that may send on it has returned, and this one would wait in its own loop for its own return…

nl2483_channel_send_and_receive.ts:

// NL2483, C6: no task both sends and receives on one channel: it would wait for its
// own sends to close the channel, which they do only when it returns.
import { Channel, scope } from "nish/threads";

class Pipe {
  ch: Channel<i32>;
  n: i32;

  constructor(ch: Channel<i32>, n: i32) {
    this.ch = ch;
    this.n = n;
  }
}

const produce = (p: Pipe): i32 => {
  for (let i: i32 = 1; i <= p.n; i++) {
    p.ch.send(i);
  }
  return p.n;
};

const consume = (p: Pipe): i32 => {
  let sum: i32 = 0;
  for (const x of p.ch) {
    sum = sum + x;
  }
  return sum;
};

const echo = (p: Pipe): i32 => {
  p.ch.send(1);
  let sum: i32 = 0;
  for (const x of p.ch) {
    sum = sum + x;
  }
  return sum;
};

export const main = (): i32 => {
  const out: i32[] = [0];
  const ch = new Channel<i32>();
  {
    using s = scope();
    s.spawn(echo, new Pipe(ch, 0), out, 0);
  }
  return out[0];
};
`echo` may both send and receive on `ch`: no task both sends and receives on one channel, because a channel closes only once every task that may send on it has returned, and this one would wait in its own loop for its own return; split it into a task that sends and a task that receives, or answer the result through a second channel

NL2484

The message of an error contains:

A `Channel` is used inside an arrow here: an arrow is lifted into a function of its own that the channel rules do not follow, so a channel is declared, sent on and received from only in a top-level function's own body…

nl2484_channel_arrow.ts:

// NL2484, C2: a channel is used only in a top-level function's own body, never inside an
// arrow: an arrow is lifted into a function of its own, which the channel rules
// do not follow, so a channel there would escape every one of them.
import { Channel, scope } from "nish/threads";

class Pipe {
  ch: Channel<i32>;

  constructor(ch: Channel<i32>) {
    this.ch = ch;
  }
}

const apply = <T>(x: T, f: (x: T) => i32): i32 => f(x);

const relay = (p: Pipe): i32 =>
  apply(p, (q: Pipe): i32 => {
    q.ch.send(1);
    return 1;
  });

export const main = (): i32 => {
  const out: i32[] = [0];
  const ch = new Channel<i32>();
  {
    using s = scope();
    s.spawn(relay, new Pipe(ch), out, 0);
  }
  let sum: i32 = 0;
  for (const x of ch) {
    sum = sum + x;
  }
  return sum + out[0];
};
A `Channel` is used inside an arrow here: an arrow is lifted into a function of its own that the channel rules do not follow, so a channel is declared, sent on and received from only in a top-level function's own body; write the arrow as a module function, `const relayBody = (q: Pipe): i32 => { q.ch.send(1); return 1; }`, and pass `relayBody` where the arrow was

nl2484_channel_arrow_boolean.ts:

// NL2484, C2: an arrow over a `Channel<boolean>` is refused with a module
// function over the same element type and the arrow's own body.
import { Channel, scope } from "nish/threads";

const count = (): i32 => {
  const ch = new Channel<boolean>();
  {
    using s = scope();
    s.spawn((out: Channel<boolean>): void => { out.send(true); out.send(false); }, ch);
  }
  let n: i32 = 0;
  for (const x of ch) {
    if (x) {
      n = n + 1;
    }
  }
  return n;
};

export const main = (): i32 => {
  console.log(count());
  return 0;
};
A `Channel` is used inside an arrow here: an arrow is lifted into a function of its own that the channel rules do not follow, so a channel is declared, sent on and received from only in a top-level function's own body; write the arrow as a module function, `const countTask = (out: Channel<boolean>): void => { out.send(true); out.send(false); }`, and pass `countTask` where the arrow was

nl2484_channel_arrow_string.ts:

// NL2484, C2: the remedy is built from the arrow it refuses, so a task that
// sends strings is told to write a function that takes a `Channel<string>`.
import { Channel, scope } from "nish/threads";

export const main = (): i32 => {
  const ch = new Channel<string>();
  {
    using s = scope();
    s.spawn((c: Channel<string>): void => { c.send("hi"); }, ch);
  }
  for (const x of ch) {
    console.log(x);
  }
  return 0;
};
A `Channel` is used inside an arrow here: an arrow is lifted into a function of its own that the channel rules do not follow, so a channel is declared, sent on and received from only in a top-level function's own body; write the arrow as a module function, `const mainTask = (c: Channel<string>): void => { c.send("hi"); }`, and pass `mainTask` where the arrow was

NL2485

The message of an error contains:

…: a field read or written as JSON is an `i32`, `u32`, `i64`, `f64`, `boolean`, `string` or a declared class, an array of one of those, or a `string`, a class or an array with `| null`…

nl2485_json_field_type.ts:

// NL2485: a field typed JSON cannot read or write.
import { JsonWriter } from "nish/json";
import { jsonEncode } from "nish/json-typed";

class Gauge {
  level: f32 = 0;
}

export const main = (): i32 => {
  const w = new JsonWriter(new Array<u8>(16), 0);
  jsonEncode(w, new Gauge());
  return w.failed;
};
`jsonEncode` cannot handle `Gauge.level`, which is `f32`: a field read or written as JSON is an `i32`, `u32`, `i64`, `f64`, `boolean`, `string` or a declared class, an array of one of those, or a `string`, a class or an array with `| null`; a `u64` and an enum are too, alone or in an array, and so is a `Map` whose keys are `string` and whose values are any of these, with `| null` or not; so are a union of classes, alone, with `| null` or in an array, and a variant's tag

NL2486

The message of an error contains:

…`, which is not a class: typed JSON reads and writes an object through the fields of a declared class, and an interface, a generic class or any other type is not one in this version

nl2486_json_not_class.ts:

// NL2486: typed JSON's value is not a declared class.
import { JsonWriter } from "nish/json";
import { jsonEncode } from "nish/json-typed";

export const main = (): i32 => {
  const w = new JsonWriter(new Array<u8>(16), 0);
  jsonEncode(w, "text");
  return w.failed;
};
`jsonEncode`'s value is `string`, which is not a class: typed JSON reads and writes an object through the fields of a declared class, and an interface, a generic class or any other type is not one in this version

NL2487

The message of an error contains:

…` is `readonly`, and `jsonDecode` assigns every field of the class it decodes into

nl2487_json_readonly.ts:

// NL2487: a `readonly` field of a class `jsonDecode` decodes into.
import { JsonLimits } from "nish/json-tokenizer";
import { JsonDecoder, jsonDecode } from "nish/json-typed";

class Ticket {
  readonly serial: i32 = 0;
}

export const main = (): i32 => jsonDecode(new JsonDecoder([], 0, 0, new JsonLimits()), new Ticket());
`Ticket.serial` is `readonly`, and `jsonDecode` assigns every field of the class it decodes into

NL2488

The message of an error contains:

… parameter(s): an array's element and a `| null` field's object are made by the decoder with no arguments, so their class's constructor takes none…

nl2488_json_constructor.ts:

// NL2488: a class the decoder makes whose constructor takes parameters.
import { JsonLimits } from "nish/json-tokenizer";
import { JsonDecoder, jsonDecode } from "nish/json-typed";

class Line {
  text: string;
  constructor(text: string) {
    this.text = text;
  }
}

class Page {
  lines: Line[];
  constructor() {
    this.lines = [];
  }
}

export const main = (): i32 => jsonDecode(new JsonDecoder([], 0, 0, new JsonLimits()), new Page());
`jsonDecode` makes a `Line` with `new Line()`, and its constructor takes 1 parameter(s): an array's element and a `| null` field's object are made by the decoder with no arguments, so their class's constructor takes none, and so is a `Map`'s value, one for each key, and a union's variant

NL2489

The message of an error contains:

`??` cannot default an element read: past the end of the array, JavaScript answers the default and …

nl2489_nullish_element.ts:

// NL2489: `??` defaulting an element read, where Node and native disagree past the end.
export const main = (): i32 => {
  const names = new Array<string | null>(1);
  return (names[3] ?? "none").length;
};
`??` cannot default an element read: past the end of the array, JavaScript answers the default and Nish panics (test the index against `length`, or read the element into a `const` first)

NL2490

The message of an error contains:

…` is never `null` here, so `?.` has nothing to skip: write `…

nl2490_chain_never_null.ts:

// NL2490: `?.` on a receiver that cannot be `null`.
export const f = (s: string): i32 => s?.length ?? 0;
`s` is never `null` here, so `?.` has nothing to skip: write `.`

NL2491

The message of an error contains:

…` ends a `?.` chain in `get`: give the map a default first, then default what `get` answers

nl2491_chain_ends_in_get.ts:

// NL2491: a `?.` chain whose last link is `Map.get`.
class Holder {
  counts: Map<string, i32>;
  constructor() {
    this.counts = new Map<string, i32>();
  }
}

export const f = (h: Holder | null): i32 => h?.counts.get("a") ?? 0;
`h?.counts.get("a")` ends a `?.` chain in `get`: give the map a default first, then default what `get` answers

NL2492

The message of an error contains:

…with no type annotation, in a class that is not generic: it holds the class's typed JSON options and is not a field

nl2492_json_options_shape.ts:

// NL2492: typed JSON options that are not `static readonly json = { ... }`.
import { JsonWriter } from "nish/json";
import { jsonEncode } from "nish/json-typed";

class User {
  static readonly json: string = "x";
  id: i32 = 0;
}

export const main = (): i32 => {
  const w = new JsonWriter(new Array<u8>(64), 0);
  jsonEncode(w, new User());
  return w.failed;
};
`static` member `json` of class `User` is written `static readonly json = { ... }`, with no type annotation, in a class that is not generic: it holds the class's typed JSON options and is not a field

NL2493

The message of an error contains:

… is not written as a literal: typed JSON reads a class's options from its text when it writes the class's code, so `json` is an object literal, `rename` an object literal of string literals and `optional` an array literal of string literals…

nl2493_json_options_literal.ts:

// NL2493: a typed JSON option that is not written as a literal.
import { JsonWriter } from "nish/json";
import { jsonEncode } from "nish/json-typed";

const KEY: string = "user_id";

class User {
  static readonly json = { rename: { id: KEY } };
  id: i32 = 0;
}

export const main = (): i32 => {
  const w = new JsonWriter(new Array<u8>(64), 0);
  jsonEncode(w, new User());
  return w.failed;
};
`User.json`'s `rename.id` is not written as a literal: typed JSON reads a class's options from its text when it writes the class's code, so `json` is an object literal, `rename` an object literal of string literals and `optional` an array literal of string literals, and `presence` an object literal of string literals

NL2494

The message of an error contains:

…`: typed JSON's options are `rename` and `optional`…

nl2494_json_options_unknown.ts:

// NL2494: a typed JSON option that does not exist.
import { JsonWriter } from "nish/json";
import { jsonEncode } from "nish/json-typed";

class User {
  static readonly json = { renames: { id: "user_id" } };
  id: i32 = 0;
}

export const main = (): i32 => {
  const w = new JsonWriter(new Array<u8>(64), 0);
  jsonEncode(w, new User());
  return w.failed;
};
`User.json` has no option `renames`: typed JSON's options are `rename` and `optional`, and `presence` and `tag`

NL2495

The message of an error contains:

…`: an option names one of the class's own fields, spelled as it is declared

nl2495_json_options_field.ts:

// NL2495: a typed JSON option naming no field of the class.
import { JsonWriter } from "nish/json";
import { jsonEncode } from "nish/json-typed";

class User {
  static readonly json = { rename: { userID: "user_id" } };
  userId: i32 = 0;
}

export const main = (): i32 => {
  const w = new JsonWriter(new Array<u8>(64), 0);
  jsonEncode(w, new User());
  return w.failed;
};
`User.json` names `userID` in `rename`, which is not a field of `User`: an option names one of the class's own fields, spelled as it is declared

NL2496

The message of an error contains:

…` to a value that is not a string: a field's key on the wire is a string literal

nl2496_json_options_rename.ts:

// NL2496: a rename to a value that is not a string.
import { JsonWriter } from "nish/json";
import { jsonEncode } from "nish/json-typed";

class User {
  static readonly json = { rename: { id: 7 } };
  id: i32 = 0;
}

export const main = (): i32 => {
  const w = new JsonWriter(new Array<u8>(64), 0);
  jsonEncode(w, new User());
  return w.failed;
};
`User.json` renames `id` to a value that is not a string: a field's key on the wire is a string literal

NL2497

The message of an error contains:

…: each field is renamed and made optional at most once, and no two fields share a key on the wire

nl2497_json_options_duplicate.ts:

// NL2497: two fields with one key on the wire.
import { JsonWriter } from "nish/json";
import { jsonEncode } from "nish/json-typed";

class User {
  static readonly json = { rename: { id: "name" } };
  id: i32 = 0;
  name: string = "";
}

export const main = (): i32 => {
  const w = new JsonWriter(new Array<u8>(64), 0);
  jsonEncode(w, new User());
  return w.failed;
};
`User.json` gives `id` and `name` one key, `name`: each field is renamed and made optional at most once, and no two fields share a key on the wire

NL2498

The message of an error contains:

…` optional, and the field has no initializer: a missing optional key sets its field to the field's initializer, so an optional field declares one

nl2498_json_options_initializer.ts:

// NL2498: an optional field with no initializer.
import { JsonWriter } from "nish/json";
import { jsonEncode } from "nish/json-typed";

class User {
  static readonly json = { optional: ["name"] };
  id: i32 = 0;
  name: string;

  constructor() {
    this.name = "";
  }
}

export const main = (): i32 => {
  const w = new JsonWriter(new Array<u8>(64), 0);
  jsonEncode(w, new User());
  return w.failed;
};
`User.json` makes `name` optional, and the field has no initializer: a missing optional key sets its field to the field's initializer, so an optional field declares one

NL2499

The message of an error contains:

…` to a key that is not valid UTF-8: the decoder reads every key as UTF-8 and refuses a lone surrogate, so no input could give it

nl2499_json_options_utf8.ts:

// NL2499: a rename to a key that is not valid UTF-8.
import { JsonWriter } from "nish/json";
import { jsonEncode } from "nish/json-typed";

class User {
  static readonly json = { rename: { id: "a\ud800" } };
  id: i32 = 0;
}

export const main = (): i32 => {
  const w = new JsonWriter(new Array<u8>(64), 0);
  jsonEncode(w, new User());
  return w.failed;
};
`User.json` renames `id` to a key that is not valid UTF-8: the decoder reads every key as UTF-8 and refuses a lone surrogate, so no input could give it

NL2500

The message of an error contains:

…`, a `let` of the function it is written in: a data-parallel body may read only a `const` or a parameter of that function, whose value no thread can change while the body runs; bind the value to a `const` before the call…

nl2500_par_capture_let.ts:

// NL2500: an arrow passed to a data-parallel form reads a `let` of the function
// it is written in, which could change while the threads read it.
import { parallelFrom, parallelMap, parallelMapInto, parallelReduce } from "nish/threads";

export const main = (): i32 => {
  let m: i32 = 3;
  m = m + 1;
  const ys = parallelMap([1, 2], (x) => x * m);
  return ys[0];
};
The arrow reads `m`, a `let` of the function it is written in: a data-parallel body may read only a `const` or a parameter of that function, whose value no thread can change while the body runs; bind the value to a `const` before the call, `const mNow = m`, and read `mNow` in the arrow

NL2501

The message of an error contains:

…` from outside the arrow it is written in: only an arrow written directly in a function's body may read that function's `const` locals and parameters, and an arrow inside another arrow reads only its own parameters and the module's top-level names; move the inner call into a top-level function that takes the value as a parameter

nl2501_par_capture_nested.ts:

// NL2501: an arrow inside an arrow reads a name from outside both; only an
// arrow written directly in a function's body captures.
import { parallelFrom, parallelMap, parallelMapInto, parallelReduce } from "nish/threads";

export const main = (): i32 => {
  const k: i32 = 3;
  const xs: i32[] = [1, 2];
  const ys = parallelMap([1, 2], (x) => parallelMap(xs, (y) => y + k)[0]);
  return ys[0];
};
The arrow reads `k` from outside the arrow it is written in: only an arrow written directly in a function's body may read that function's `const` locals and parameters, and an arrow inside another arrow reads only its own parameters and the module's top-level names; move the inner call into a top-level function that takes the value as a parameter

NL2502

The message of an error contains:

The arrow reads `this`: a data-parallel body may read the `const` locals and the parameters of the function it is written in, and `this` is neither; bind it, or what the body needs of it, to a `const` before the call (`const self = this`, `const k = this.k`)

nl2502_par_capture_this.ts:

// NL2502: an arrow passed to a data-parallel form reads `this`.
import { parallelFrom, parallelMap, parallelMapInto, parallelReduce } from "nish/threads";

class Scaler {
  k: i32 = 2;
  run(xs: i32[]): i32[] {
    return parallelMap(xs, (x) => x * this.k);
  }
}

export const main = (): i32 => new Scaler().run([1, 2])[0];
The arrow reads `this`: a data-parallel body may read the `const` locals and the parameters of the function it is written in, and `this` is neither; bind it, or what the body needs of it, to a `const` before the call (`const self = this`, `const k = this.k`)

NL2503

The message of an error contains:

…`, which belongs to the function it is written in: a data-parallel body reads a captured value and never writes it, because every thread reads the same one…

nl2503_par_capture_write.ts:

// NL2503: an arrow passed to a data-parallel form assigns to a name of the
// function it is written in.
import { parallelFrom, parallelMap, parallelMapInto, parallelReduce } from "nish/threads";

export const main = (): i32 => {
  const k: i32 = 3;
  const ys = parallelMap([1, 2], (x): i32 => {
    k = x;
    return x;
  });
  return ys[0];
};
The arrow assigns to `k`, which belongs to the function it is written in: a data-parallel body reads a captured value and never writes it, because every thread reads the same one; answer the value from the body instead, and read the answers from the array the call gives back, `const kValues = parallelMap([1, 2], (x): i32 => x)`

nl2503_par_capture_write_reduce.ts:

// NL2503: the reducer passed to `parallelReduce` assigns to a name of the
// function it is written in.
import { parallelReduce } from "nish/threads";

export const main = (): i32 => {
  let calls: i32 = 0;
  const total = parallelReduce([1, 2, 3], (a: i32, b: i32): i32 => {
    calls += 1;
    return a + b;
  }, 0);
  return total + calls;
};
The arrow assigns to `calls`, which belongs to the function it is written in: a data-parallel body reads a captured value and never writes it, because every thread reads the same one; answer the value from the reducer instead, and let `parallelReduce` fold it, `const callsTotal = parallelReduce([1, 2, 3], (a, b): i32 => a + b, 0)`

NL2504

The message of an error contains:

…`, and a data-parallel body cannot capture it: …

nl2504_par_capture_type.ts:

// NL2504: an arrow passed to a data-parallel form reads a local whose type a
// body cannot capture: here a `Result`.
import { parallelFrom, parallelMap, parallelMapInto, parallelReduce } from "nish/threads";

export const main = (): i32 => {
  const r: Result<i32, string> = Ok(3);
  const ys = parallelMap([1, 2], (x) => (r.isOk() ? x : 0));
  return ys[0];
};
The arrow reads `r`, which is `Result<i32, string>`, and a data-parallel body cannot capture it: a `Result` is read through the test that narrowed it; bind the value it holds to a `const` instead

NL2505

The message of an error contains:

…`, which could be the array `parallelMapInto` is writing: another thread would be writing it while this one reads; a captured array is known not to be `dst` only when one of the two is a `const` bound to an array of its own, such as a literal or a `new Array`, and the other is another such `const` or a parameter

nl2505_par_capture_dst.ts:

// NL2505: a body reads `dst` itself through a capture, while other threads
// write it.
import { parallelFrom, parallelMap, parallelMapInto, parallelReduce } from "nish/threads";

export const main = (): i32 => {
  const dst: i32[] = [0, 0];
  parallelMapInto([1, 2], dst, (x) => dst[0] + x);
  return dst[0];
};
`(x) => ...` reads `dst`, and can reach a `i32[]` through `dst`, which could be the array `parallelMapInto` is writing: another thread would be writing it while this one reads; a captured array is known not to be `dst` only when one of the two is a `const` bound to an array of its own, such as a literal or a `new Array`, and the other is another such `const` or a parameter

NL2506

The message of an error contains:

…`, a `let` of the function it is written in: a task's arrow may read only a `const` or a parameter of that function, which is handed to the task when it is spawned as its argument is and cannot change before it runs; bind the value to a `const` before the `spawn`…

nl2506_task_capture_let.ts:

// NL2506: an arrow given to `spawn` reads a `let` of the function it is
// written in, which could change before the task runs.
import { scope } from "nish/threads";

export const main = (): i32 => {
  let k: i32 = 3;
  const out: i32[] = [0];
  {
    using s = scope();
    s.spawn((n: i32): i32 => n + k, 1, out, 0);
  }
  k = 4;
  return out[0];
};
The arrow reads `k`, a `let` of the function it is written in: a task's arrow may read only a `const` or a parameter of that function, which is handed to the task when it is spawned as its argument is and cannot change before it runs; bind the value to a `const` before the `spawn`, `const kNow = k`, and read `kNow` in the arrow

NL2507

The message of an error contains:

The arrow reads `this`: a task's arrow may read the `const` locals and the parameters of the function it is written in, and `this` is neither; bind it, or what the task needs of it, to a `const` before the `spawn` (`const self = this`, `const k = this.k`)

nl2507_task_capture_this.ts:

// NL2507: an arrow given to `spawn` reads `this`.
import { scope } from "nish/threads";

class W {
  k: i32 = 3;

  run(): i32 {
    const out: i32[] = [0];
    {
      using s = scope();
      s.spawn((n: i32): i32 => n + this.k, 1, out, 0);
    }
    return out[0];
  }
}

export const main = (): i32 => new W().run();
The arrow reads `this`: a task's arrow may read the `const` locals and the parameters of the function it is written in, and `this` is neither; bind it, or what the task needs of it, to a `const` before the `spawn` (`const self = this`, `const k = this.k`)

NL2508

The message of an error contains:

…`, which belongs to the function it is written in: a task reads a captured value and never writes it, because it is handed a copy of the value when it is spawned, as it is its argument…

nl2508_task_capture_write.ts:

// NL2508: an arrow given to `spawn` assigns to a name it captures.
import { scope } from "nish/threads";

export const main = (): i32 => {
  const n: i32 = 3;
  {
    using s = scope();
    s.spawn(() => {
      n = 4;
    });
  }
  return n;
};
The arrow assigns to `n`, which belongs to the function it is written in: a task reads a captured value and never writes it, because it is handed a copy of the value when it is spawned, as it is its argument; make the task answer the value and give the `spawn` a destination, `s.spawn(task, arg, out, i)`, and read `out[i]` after the scope's block

NL2509

The message of an error contains:

…`, and a task cannot capture it: …

nl2509_task_capture_type.ts:

// NL2509: an arrow given to `spawn` reads a local whose type a task cannot
// capture: here a `Channel`, which reaches a task through its argument.
import { Channel, scope } from "nish/threads";

export const main = (): i32 => {
  const ch = new Channel<i32>();
  {
    using s = scope();
    s.spawn(() => {
      ch.send(1);
    });
  }
  return 0;
};
The arrow reads `ch`, which is `Channel<i32>`, and a task cannot capture it: a guard, a channel or a scope is shared only by the rules of its own block; a task reaches a channel through its argument, `s.spawn(entry, arg)`, where the scope counts its senders

NL2510

The message of an error contains:

…`: a captured value is the task's argument, and a `Mutex` reaches a task as an argument or as a field of the class an argument is, never deeper, so which tasks share a lock is a question the checker can answer

nl2510_task_capture_mutex_deep.ts:

// NL2510: a task's arrow captures a value that reaches a `Mutex` deeper than
// a field of its own class.
import { Mutex, scope } from "nish/threads";

class Tally {
  total: i32 = 0;
}

class Inner {
  m: Mutex<Tally>;

  constructor(m: Mutex<Tally>) {
    this.m = m;
  }
}

class Outer {
  inner: Inner;

  constructor(inner: Inner) {
    this.inner = inner;
  }
}

const bump = (o: Outer): void => {
  using g = o.inner.m.lock();
  g.value.total = g.value.total + 1;
};

export const main = (): i32 => {
  const o = new Outer(new Inner(new Mutex<Tally>(new Tally())));
  {
    using s = scope();
    s.spawn(() => bump(o));
  }
  return 0;
};
The task's arrow reads `o`, which is `Outer` and reaches a `Mutex` through `o.inner.m`: a captured value is the task's argument, and a `Mutex` reaches a task as an argument or as a field of the class an argument is, never deeper, so which tasks share a lock is a question the checker can answer

NL2511

The message of an error contains:

…`, and a `spawn` without a destination stores nothing: a task's answer is kept only by `s.spawn(entry, arg, dst, at)`, which stores it in `dst[at]` when the scope joins, so make the task answer `void` or give it a destination…

nl2511_task_void_result.ts:

// NL2511: a `spawn` without a destination is given an entry that answers a
// value, which nothing would store.
import { scope } from "nish/threads";

const twice = (n: i32): i32 => n * 2;

export const main = (): i32 => {
  {
    using s = scope();
    s.spawn(twice, 4);
  }
  return 0;
};
`twice` answers `i32`, and a `spawn` without a destination stores nothing: a task's answer is kept only by `s.spawn(entry, arg, dst, at)`, which stores it in `dst[at]` when the scope joins, so make the task answer `void` or give it a destination, `const answer = new Array<i32>(1)` above the scope's block and `s.spawn(twice, 4, answer, 0)`

NL2512

The message of an error contains:

…. That reference would read the key outside the `Secret`, and zeros once it is wiped, so pass a fresh value (`secret([1, 2, 3])`, a `new`, or a call that makes one) or a copy: …

NL2513

The message of an error contains:

…`, before the scope joins: natively the scope's tasks run when its block ends and under Node each runs where it is spawned, so the parent touches what they share only before the statement that holds the first `spawn` or after the join…

nl2513_atomic_region.ts:

// NL2513: after a scope's first `spawn` the parent does not touch an atomic its
// tasks share. Natively the task has not run here and under Node it has, so
// `seen` would be 0 compiled and 1 under Node.
import { AtomicI32, scope } from "nish/threads";

const bump = (a: AtomicI32): i32 => a.add(1);

export const main = (): i32 => {
  const a = new AtomicI32(0);
  const done: i32[] = [0];
  let seen: i32 = 0;
  {
    using s = scope();
    s.spawn(bump, a, done, 0);
    seen = a.load();
  }
  return seen;
};
This `load()` reaches an atomic a task of `s` can reach through `arg`, before the scope joins: natively the scope's tasks run when its block ends and under Node each runs where it is spawned, so the parent touches what they share only before the statement that holds the first `spawn` or after the join; move `a.load()` above the block that opens with `using s = scope()`, or below the end of that block

NL2514

The message of an error contains:

…` is not a member a program may name: an atomic is read and updated only by calling its methods, `load()`, `add(n)` and the rest, each one indivisible: a plain read or store of its word would race with the update a task makes at the same moment

nl2514_atomic_member.ts:

// NL2514: an atomic's word is reached only through its methods, each one
// indivisible; a plain read would race with a task's update.
import { AtomicI32 } from "nish/threads";

export const main = (): i32 => {
  const a = new AtomicI32(0);
  return a.value;
};
`value` is not a member a program may name: an atomic is read and updated only by calling its methods, `load()`, `add(n)` and the rest, each one indivisible: a plain read or store of its word would race with the update a task makes at the same moment

NL2515

The message of an error contains:

…` is a class of `nish/threads`, and its objects are made only as the module makes them: `new AtomicI32(v)`, `new AtomicI64(v)`, `new AtomicI32Array(n)`, `new AtomicI64Array(n)`, `new Mutex(v)`, `new Channel()`, `m.lock()` and `scope()`. An object literal would be a plain record under Node, with none of the methods the class is used through

nl2515_atomic_literal.ts:

// NL2515: a class of `nish/threads` is made only as the module makes it; an
// object literal would be a plain record under Node, with none of its methods.
import { AtomicI32 } from "nish/threads";

export const main = (): i32 => {
  const a: AtomicI32 = { value: 0 };
  return a.load();
};
`AtomicI32` is a class of `nish/threads`, and its objects are made only as the module makes them: `new AtomicI32(v)`, `new AtomicI64(v)`, `new AtomicI32Array(n)`, `new AtomicI64Array(n)`, `new Mutex(v)`, `new Channel()`, `m.lock()` and `scope()`. An object literal would be a plain record under Node, with none of the methods the class is used through

NL2516

The message of an error contains:

…` also reaches: the scopes one `using` statement opens join one after the other natively, the last declared first, while under Node each task runs where it is spawned, so the tasks of sibling scopes share no lock, no atomic and no destination; open each scope in a block of its own, so the first joins before the second opens…

nl2516_sibling_scopes.ts:

// NL2516: two scopes of one `using` whose tasks share an atomic: natively `t`
// joins first, under Node each task runs at its spawn.
import { AtomicI32, scope } from "nish/threads";
const bump = (a: AtomicI32): i32 => a.add(1);
const peek = (a: AtomicI32): i32 => a.load();
export const main = (): i32 => {
  const a = new AtomicI32(0);
  const out: i32[] = [0];
  const seen: i32[] = [0];
  {
    using s = scope(), t = scope();
    s.spawn(bump, a, out, 0);
    t.spawn(peek, a, seen, 0);
  }
  return seen[0];
};
This `spawn` on `t` hands its task an atomic through `a`, which a task of `s` also reaches: the scopes one `using` statement opens join one after the other natively, the last declared first, while under Node each task runs where it is spawned, so the tasks of sibling scopes share no lock, no atomic and no destination; open each scope in a block of its own, so the first joins before the second opens, `{ using s = scope(); s.spawn(bump, a, out, 0) } { using t = scope(); t.spawn(peek, a, seen, 0) }`

NL2517

The message of an error contains:

…` is not a type: only a member of an enum may be named with a dot in a type, and `…

nl2517_member_type_not_enum.ts:

// NL2517: a dotted type whose qualifier is not an enum.
class Box {
  size: i32 = 0;
}

export const main = (): i32 => {
  const n: Box.size = 0;
  return n;
};
`Box.size` is not a type: only a member of an enum may be named with a dot in a type, and `Box` is not an enum

NL2518

The message of an error contains:

…` to name as a type

nl2518_member_type_unknown.ts:

// NL2518: a member type naming a member the enum does not declare.
enum Kind {
  Scalar,
}

export const main = (): i32 => {
  const k: Kind.Array = Kind.Scalar;
  return 0;
};
Enum `Kind` has no member `Array` to name as a type

NL2519

The message of an error contains:

…` holds only the member `…

nl2519_member_type_holds_one.ts:

// NL2519: a slot of an enum member type given another member.
enum Kind {
  Scalar,
  Array,
}

export const main = (): i32 => {
  const k: Kind.Array = Kind.Scalar;
  return k === Kind.Array ? 0 : 1;
};
Type `Kind.Array` holds only the member `Kind.Array`, got `Kind.Scalar`

NL2520

The message of an error contains:

…>(n)` would zero-fill with 0, which is not the member `…

nl2520_member_type_zero_fill.ts:

// NL2520: a zero-filled array of a member type whose value is not 0.
enum Kind {
  Scalar,
  Array,
}

export const main = (): i32 => {
  const a = new Array<Kind.Array>(2);
  return a.length;
};
`new Array<Kind.Array>(n)` would zero-fill with 0, which is not the member `Kind.Array`; build it with `[]` and `push` instead

NL2521

The message of an error contains:

…` zero-fills the array its workers write, which a `…

nl2521_member_type_parallel_result.ts:

// NL2521: a parallel body answering a member type the template's zero fill
// cannot hold.
import { parallelMap } from "nish/threads";

enum Kind {
  Scalar,
  Array,
}

export const main = (): i32 => {
  const ys = parallelMap([1, 2], (x: i32): Kind.Array => Kind.Array);
  return ys.length;
};
`(x) => ...` answers the member type `Kind.Array`, and `parallelMap` zero-fills the array its workers write, which a `Kind.Array` cannot hold; declare the body to answer `Kind`

NL2522

The message of an error contains:

…` cannot be a union variant: a variant is a class or an interface that is not generic, because its tag is a field it declares itself

nl2522_union_generic_variant.ts:

// NL2522: a generic class instance named as a union variant.
enum Kind {
  Circle,
  Square,
  Tri,
}

class Circle {
  readonly kind: Kind.Circle = Kind.Circle;
  r: i32 = 1;
}

class Square {
  readonly kind: Kind.Square = Kind.Square;
  side: i32 = 2;
}

type Shape = Circle | Square;

const pick = (n: i32): Shape => (n > 0 ? new Circle() : new Square());

class Ring<T> {
  readonly kind: Kind.Tri = Kind.Tri;
  item: T;
  constructor(item: T) {
    this.item = item;
  }
}

type IntRing = Ring<i32>;

const take = (s: Circle | IntRing): Kind => s.kind;
`Ring<i32>` cannot be a union variant: a variant is a class or an interface that is not generic, because its tag is a field it declares itself

NL2523

The message of an error contains:

…` cannot be a union variant: its first field must be its tag, a field whose type is one member of an enum, such as `readonly kind: Kind.Circle = Kind.Circle`

nl2523_union_untagged_variant.ts:

// NL2523: a class whose first field is not an enum-member tag.
enum Kind {
  Circle,
  Square,
  Tri,
}

class Circle {
  readonly kind: Kind.Circle = Kind.Circle;
  r: i32 = 1;
}

class Square {
  readonly kind: Kind.Square = Kind.Square;
  side: i32 = 2;
}

type Shape = Circle | Square;

const pick = (n: i32): Shape => (n > 0 ? new Circle() : new Square());

class Point {
  x: i32 = 0;
}

class Line {
  length: i32 = 0;
}

type Thing = Point | Line;

export const main = (): void => {
  const t: Thing = new Point();
  console.log(t === null);
};
`Point` cannot be a union variant: its first field must be its tag, a field whose type is one member of an enum, such as `readonly kind: Kind.Circle = Kind.Circle`

NL2524

The message of an error contains:

Union variants must take their tags from one enum: `…

nl2524_union_mixed_enums.ts:

// NL2524: variants whose tags come from two enums.
enum Kind {
  Circle,
  Square,
  Tri,
}

class Circle {
  readonly kind: Kind.Circle = Kind.Circle;
  r: i32 = 1;
}

class Square {
  readonly kind: Kind.Square = Kind.Square;
  side: i32 = 2;
}

type Shape = Circle | Square;

const pick = (n: i32): Shape => (n > 0 ? new Circle() : new Square());

enum Other {
  Dot,
}

class Dot {
  readonly kind: Other.Dot = Other.Dot;
}

const take = (s: Circle | Dot): i32 => 0;
Union variants must take their tags from one enum: `Circle.kind` is a `Kind` and `Dot.kind` is a `Other`

NL2525

The message of an error contains:

Union variants must give their tag one name: the first field of `…

nl2525_union_tag_names.ts:

// NL2525: variants whose tag fields have different names.
enum Kind {
  Circle,
  Square,
  Tri,
}

class Circle {
  readonly kind: Kind.Circle = Kind.Circle;
  r: i32 = 1;
}

class Square {
  readonly kind: Kind.Square = Kind.Square;
  side: i32 = 2;
}

type Shape = Circle | Square;

const pick = (n: i32): Shape => (n > 0 ? new Circle() : new Square());

class Dot {
  readonly tag: Kind.Tri = Kind.Tri;
}

const take = (s: Circle | Dot): i32 => 0;
Union variants must give their tag one name: the first field of `Circle` is `kind` and the first field of `Dot` is `tag`

NL2526

The message of an error contains:

…`, so it cannot tell them apart

nl2526_union_duplicate_tag.ts:

// NL2526: two variants with the same tag.
enum Kind {
  Circle,
  Square,
  Tri,
}

class Circle {
  readonly kind: Kind.Circle = Kind.Circle;
  r: i32 = 1;
}

class Square {
  readonly kind: Kind.Square = Kind.Square;
  side: i32 = 2;
}

type Shape = Circle | Square;

const pick = (n: i32): Shape => (n > 0 ? new Circle() : new Square());

class Ball {
  readonly kind: Kind.Circle = Kind.Circle;
}

const take = (s: Circle | Ball): i32 => 0;
Union variants `Circle` and `Ball` both have the tag `Kind.Circle`, so it cannot tell them apart

NL2527

The message of an error contains:

A union needs two different variants: this one names only `…

nl2527_union_one_variant.ts:

// NL2527: a union that names one variant twice.
enum Kind {
  Circle,
  Square,
  Tri,
}

class Circle {
  readonly kind: Kind.Circle = Kind.Circle;
  r: i32 = 1;
}

class Square {
  readonly kind: Kind.Square = Kind.Square;
  side: i32 = 2;
}

type Shape = Circle | Square;

const pick = (n: i32): Shape => (n > 0 ? new Circle() : new Square());

const take = (s: Circle | Circle): i32 => 0;
A union needs two different variants: this one names only `Circle`, so write that type itself

NL2528

The message of an error contains:

…` until a test of the tag narrows it to one variant

nl2528_union_member.ts:

// NL2528: a variant field read through the union.
enum Kind {
  Circle,
  Square,
  Tri,
}

class Circle {
  readonly kind: Kind.Circle = Kind.Circle;
  r: i32 = 1;
}

class Square {
  readonly kind: Kind.Square = Kind.Square;
  side: i32 = 2;
}

type Shape = Circle | Square;

const pick = (n: i32): Shape => (n > 0 ? new Circle() : new Square());

class Ball {
  readonly kind: Kind.Tri = Kind.Tri;
  r: i32 = 2;
}

const radius = (s: Circle | Ball): i32 => s.r;
`r` cannot be used on the union `Circle | Ball`: a union has only its tag `kind` until a test of the tag narrows it to one variant

NL2529

The message of an error contains:

…`: a union is read-only, so assign to the variant itself

nl2529_union_assign.ts:

// NL2529: a write through the union.
enum Kind {
  Circle,
  Square,
  Tri,
}

class Circle {
  readonly kind: Kind.Circle = Kind.Circle;
  r: i32 = 1;
}

class Square {
  readonly kind: Kind.Square = Kind.Square;
  side: i32 = 2;
  x: i32 = 0;
}

type Shape = Circle | Square;

const pick = (n: i32): Shape => (n > 0 ? new Circle() : new Square());

class Spot {
  readonly kind: Kind.Tri = Kind.Tri;
  x: i32 = 0;
}

const reset = (s: Spot | Square): void => {
  s.x = 1;
};
Cannot assign to `x` through the union `Square | Spot`: a union is read-only, so assign to the variant itself

NL2530

The message of an error contains:

…` cannot give a type to what it initialises: TypeScript would infer `…

nl2530_union_inferred_tag.ts:

// NL2530: a const without an annotation given the union's tag.
enum Kind {
  Circle,
  Square,
  Tri,
}

class Circle {
  readonly kind: Kind.Circle = Kind.Circle;
  r: i32 = 1;
}

class Square {
  readonly kind: Kind.Square = Kind.Square;
  side: i32 = 2;
}

type Shape = Circle | Square;

const pick = (n: i32): Shape => (n > 0 ? new Circle() : new Square());

export const main = (): void => {
  const s: Shape = pick(1);
  const k = s.kind;
  console.log(k === Kind.Circle);
};
The tag `s.kind` cannot give a type to what it initialises: TypeScript would infer `Kind.Circle | Kind.Square`, a type this version does not have, so annotate it as `Kind`

NL2531

The message of an error contains:

…`, which share no member

nl2531_union_disjoint_compare.ts:

// NL2531: a tag compared with a member no variant has.
enum Kind {
  Circle,
  Square,
  Tri,
}

class Circle {
  readonly kind: Kind.Circle = Kind.Circle;
  r: i32 = 1;
}

class Square {
  readonly kind: Kind.Square = Kind.Square;
  side: i32 = 2;
}

type Shape = Circle | Square;

const pick = (n: i32): Shape => (n > 0 ? new Circle() : new Square());

const isTri = (s: Shape): boolean => s.kind === Kind.Tri;
This `===` is always false: `s.kind` is `Kind.Circle | Kind.Square` and `Kind.Tri` is `Kind.Tri`, which share no member

NL2532

The message of an error contains:

…` can never match: the discriminant is `…

nl2532_union_disjoint_case.ts:

// NL2532: a case label no variant has.
enum Kind {
  Circle,
  Square,
  Tri,
}

class Circle {
  readonly kind: Kind.Circle = Kind.Circle;
  r: i32 = 1;
}

class Square {
  readonly kind: Kind.Square = Kind.Square;
  side: i32 = 2;
}

type Shape = Circle | Square;

const pick = (n: i32): Shape => (n > 0 ? new Circle() : new Square());

const weight = (s: Shape): i32 => {
  switch (s.kind) {
    case Kind.Circle:
      return 1;
    case Kind.Tri:
      return 3;
    default:
      return 2;
  }
};
`case Kind.Tri` can never match: the discriminant is `Kind.Circle | Kind.Square`

NL2533

The message of an error contains:

` cannot take a union as a type argument: in this version a union is the type of a local, …

Retired: ... cannot take a union as a type argument, the refusal union type arguments lifted; a union is a generic's, a Result's and nish/map's helpers' type argument now (tests/cases/union_generic_class, union_generic_call, union_result, union_map_helpers), and what stays refused keeps its own code: a Set element or Map key NL2353, a generic variant NL2522, an inferred union NL2172, a variant stored through a generic field NL2534

NL2534

The message of an error contains:

…from here on, which this version does not track, so declare it as that type or give it a value of the whole union

nl2534_union_narrowing_store.ts:

// NL2534: a union local initialised with one variant.
enum Kind {
  Circle,
  Square,
  Tri,
}

class Circle {
  readonly kind: Kind.Circle = Kind.Circle;
  r: i32 = 1;
}

class Square {
  readonly kind: Kind.Square = Kind.Square;
  side: i32 = 2;
}

type Shape = Circle | Square;

const pick = (n: i32): Shape => (n > 0 ? new Circle() : new Square());

export const main = (): void => {
  const s: Shape = new Circle();
  console.log(s.kind === Kind.Circle);
};
The variable `s` is `Circle | Square` and is given a `Circle`: TypeScript narrows it to `Circle` from here on, which this version does not track, so declare it as that type or give it a value of the whole union

NL2535

The message of an error contains:

…` must be `readonly`, because the union tells its variants apart by it

nl2535_union_mutable_tag.ts:

// NL2535: a variant whose tag is not readonly.
enum Kind {
  Dot,
  Box,
}

interface Dot {
  readonly kind: Kind.Dot;
  x: i32;
}

interface Box {
  kind: Kind.Box;
  side: i32;
}

export const tagOf = (m: Dot | Box): Kind => m.kind;
`Box` cannot be a union variant: its tag `kind` must be `readonly`, because the union tells its variants apart by it

NL2536

The message of an error contains:

…a guard is handed only to a function named at the call…

nl2536_mutex_guard_helper_ctor.ts:

// NL2536: a constructor reached by `new` is a call too, so the helper the
// remedy writes reads what the constructor reads through the guard, `total`
// here, not the first field the data holds.
import { Mutex, MutexGuard } from "nish/threads";

class Counter {
  n: i32 = 0;
  total: f64 = 1.5;
}

class Other {
  label: string = "x";
}

class Report {
  shown: f64 = 0;

  constructor(o: Other, p: MutexGuard<Counter>) {
    console.log(o.label);
    this.shown = p.value.total;
  }
}

export const main = (): i32 => {
  const m = new Mutex<Counter>(new Counter());
  {
    using g = m.lock();
    new Report(new Other(), g);
  }
  return 0;
};
`g` is passed to a constructor: a guard is handed only to a function named at the call, neither a method, a constructor nor a generic, whose parameter is declared as the guard is, `MutexGuard<T>`, once per call, because that parameter is held to the rules of the guard's block; write the helper as `const useGuard = (g: MutexGuard<Counter>): void => { console.log(g.value.total) }` and call it as `useGuard(g)`

nl2536_mutex_guard_helper_field.ts:

// NL2536: the helper the remedy writes reads what the refused callee reads
// through the guard, `total` here, not the first field the data holds.
import { Mutex, MutexGuard } from "nish/threads";

class Counter {
  n: i32 = 0;
  total: f64 = 1.5;
}

class Report {
  shown: f64 = 0;

  show(p: MutexGuard<Counter>): void {
    this.shown = p.value.total;
  }
}

export const main = (): i32 => {
  const m = new Mutex<Counter>(new Counter());
  {
    using g = m.lock();
    new Report().show(g);
  }
  return 0;
};
`g` is passed to `show`, a method: a guard is handed only to a function named at the call, neither a method, a constructor nor a generic, whose parameter is declared as the guard is, `MutexGuard<T>`, once per call, because that parameter is held to the rules of the guard's block; write the helper as `const useGuard = (g: MutexGuard<Counter>): void => { console.log(g.value.total) }` and call it as `useGuard(g)`

nl2536_mutex_guard_helper_generic.ts:

// NL2536: the helper the remedy writes reads what a generic callee reads
// through the guard, `total` here, from the instantiation's own body, not
// the first field the data holds.
import { Mutex, MutexGuard } from "nish/threads";

class Counter {
  n: i32 = 0;
  total: f64 = 1.5;
}

class Other {
  label: string = "x";
}

const helper = <T>(o: Other, p: MutexGuard<Counter>, t: T): void => {
  console.log(o.label);
  console.log(p.value.total);
};

export const main = (): i32 => {
  const m = new Mutex<Counter>(new Counter());
  {
    using g = m.lock();
    helper(new Other(), g, 1);
  }
  return 0;
};
`g` is passed to `helper`, a generic function: a guard is handed only to a function named at the call, neither a method, a constructor nor a generic, whose parameter is declared as the guard is, `MutexGuard<T>`, once per call, because that parameter is held to the rules of the guard's block; write the helper as `const useGuard = (g: MutexGuard<Counter>): void => { console.log(g.value.total) }` and call it as `useGuard(g)`

nl2536_mutex_guard_helper_method.ts:

// NL2536: a guard is handed only to a function named at the call, which a
// method is not: `this` is a second way out that the parameter's rule does not cover.
import { Channel, Mutex, MutexGuard, scope } from "nish/threads";

class Counter {
  n: i32 = 0;
}

class Box {
  n: i32 = 0;

  take(g: MutexGuard<Counter>): void {
    this.n = g.value.n;
  }
}

export const main = (): i32 => {
  const m = new Mutex<Counter>(new Counter());
  {
    using g = m.lock();
    new Box().take(g);
  }
  return 0;
};
`g` is passed to `take`, a method: a guard is handed only to a function named at the call, neither a method, a constructor nor a generic, whose parameter is declared as the guard is, `MutexGuard<T>`, once per call, because that parameter is held to the rules of the guard's block; write the helper as `const useGuard = (g: MutexGuard<Counter>): void => { console.log(g.value.n) }` and call it as `useGuard(g)`

NL2537

The message of an error contains:

This stores `null` through a guard into a slot that holds an object, and a guard stores only a number, a `boolean`, an enum or a `string`, and a `null` only into a `string | null` slot, where the store frees the copy the slot held; keep a `boolean` field beside the slot, `g.value.hasNext = false`, and test it before reading the object

nl2537_mutex_guard_null_store.ts:

// NL2537: a `null` stored through a guard into a slot that holds an object: a
// guard stores no pointer, and a `null` only into a `string | null` slot.
import { Mutex } from "nish/threads"

class Link {
  n: i32 = 0
  next: Link | null = null
}

export const main = (): i32 => {
  const m = new Mutex<Link>(new Link())
  {
    using g = m.lock()
    g.value.next = null
  }
  return 0
};
This stores `null` through a guard into a slot that holds an object, and a guard stores only a number, a `boolean`, an enum or a `string`, and a `null` only into a `string | null` slot, where the store frees the copy the slot held; keep a `boolean` field beside the slot, `g.value.hasNext = false`, and test it before reading the object

NL2538

The message of an error contains:

…, and a declared C function returns a scalar, a `CPtr` or a `string` it copies from a `const char *`

NL2539

The message of an error contains:

` the program holds: a nullable foreign parameter takes the literal `null` or a pointer narrowed with `!== null`, so C is never handed a null by a path nobody tested

NL2540

The message of an error contains:

…`, so it declares `wipe(): void`, taking no parameters: a holder's key ends in that method, which wipes every `Secret` field it holds, `wipe(this.…

nl2540_secret_field_releaser.ts:

// NL2540: A class with a `Secret` field declares `wipe(): void`, which ends it.
import { Secret, expose, exposeWith, secret, wipe } from "nish:secret";
const sum = (k: u8[]): i32 => toI32(k.length);
const bytes = (): u8[] => [toU8(1), toU8(2)];
class Holder {
  readonly k: Secret<u8[]>;
  constructor(v: u8[]) {
    this.k = secret(bytes());
  }
}
export const main = (): i32 => 0;
`Holder` holds the `Secret` field `k`, so it declares `wipe(): void`, taking no parameters: a holder's key ends in that method, which wipes every `Secret` field it holds, `wipe(this.k)`, on every path

NL2541

The message of an error contains:

…` can leave this `wipe()` unwiped: a holder's `wipe()` ends every `Secret` field it holds itself, with `wipe(…

nl2541_secret_field_unwiped.ts:

// NL2541: A holder's `wipe()` that forgets one of its fields.
import { Secret, expose, exposeWith, secret, wipe } from "nish:secret";
const sum = (k: u8[]): i32 => toI32(k.length);
const bytes = (): u8[] => [toU8(1), toU8(2)];
class Holder {
  readonly k: Secret<u8[]>;
  readonly j: Secret<u8[]>;
  constructor(v: u8[]) {
    this.k = secret(bytes());
    this.j = secret([toU8(9)]);
  }
  wipe(): void {
    wipe(this.k);
  }
}
export const main = (): i32 => 0;
`this.j` can leave this `wipe()` unwiped: a holder's `wipe()` ends every `Secret` field it holds itself, with `wipe(this.j)` on every path (a helper it calls does not count), because nothing else ever will

NL2542

The message of an error contains:

…` is set from something other than `secret(...)`: a `Secret` field is set in its class's constructor, from `secret(v)`, so the holder is the only owner of the key it makes, and its `wipe()` the one place that key ends

nl2542_secret_field_source.ts:

// NL2542: A `Secret` field is set from `secret(...)`, not from a function that returns a `Secret`.
import { Secret, expose, exposeWith, secret, wipe } from "nish:secret";
const sum = (k: u8[]): i32 => toI32(k.length);
const bytes = (): u8[] => [toU8(1), toU8(2)];
const make = (): Secret<u8[]> => secret([toU8(1)]);
class Holder {
  readonly k: Secret<u8[]>;
  constructor() {
    this.k = make();
  }
  wipe(): void {
    wipe(this.k);
  }
}
export const main = (): i32 => 0;
`this.k` is set from something other than `secret(...)`: a `Secret` field is set in its class's constructor, from `secret(v)`, so the holder is the only owner of the key it makes, and its `wipe()` the one place that key ends

NL2543

The message of an error contains:

…` is set again while it may still hold a `Secret`: a `Secret` field is set once, in its class's constructor, and the key it held would be lost unwiped

nl2543_secret_field_set_again.ts:

// NL2543: A `Secret` field set twice in the constructor: the first key would be lost unwiped.
import { Secret, expose, exposeWith, secret, wipe } from "nish:secret";
const sum = (k: u8[]): i32 => toI32(k.length);
const bytes = (): u8[] => [toU8(1), toU8(2)];
class Holder {
  readonly k: Secret<u8[]>;
  constructor(v: u8[]) {
    this.k = secret(bytes());
    this.k = secret([toU8(2)]);
  }
  wipe(): void {
    wipe(this.k);
  }
}
export const main = (): i32 => 0;
`this.k` is set again while it may still hold a `Secret`: a `Secret` field is set once, in its class's constructor, and the key it held would be lost unwiped

NL2544

The message of an error contains:

…` is a `Secret` field, which is read only as a call's argument or an `===`/`!==` operand: bound, returned, chosen or stored, it would be a second owner of the key the holder's `wipe()` ends, so pass it to `expose`, `exposeWith` or a function that takes a `Secret`

nl2544_secret_field_read.ts:

// NL2544: A `Secret` field cannot be bound to a local: two names would own one key.
import { Secret, expose, exposeWith, secret, wipe } from "nish:secret";
const sum = (k: u8[]): i32 => toI32(k.length);
const bytes = (): u8[] => [toU8(1), toU8(2)];
class Holder {
  readonly k: Secret<u8[]>;
  constructor(seed: u8) {
    this.k = secret([seed, toU8(2)]);
  }
  wipe(): void {
    wipe(this.k);
  }
}
export const main = (): i32 => {
  const h = new Holder(toU8(1));
  const k = h.k;
  wipe(k);
  h.wipe();
  return 0;
};
`k` is a `Secret` field, which is read only as a call's argument or an `===`/`!==` operand: bound, returned, chosen or stored, it would be a second owner of the key the holder's `wipe()` ends, so pass it to `expose`, `exposeWith` or a function that takes a `Secret`

NL2545

The message of an error contains:

…` holds a `Secret` field and is made only by `new`: its constructor is the one place that field is set, from `secret(v)`, so an object literal or a decoded document cannot build it

nl2545_secret_field_literal.ts:

// NL2545: A holder cannot be built from an object literal: only its constructor sets a `Secret` field.
import { Secret, expose, exposeWith, secret, wipe } from "nish:secret";
const sum = (k: u8[]): i32 => toI32(k.length);
const bytes = (): u8[] => [toU8(1), toU8(2)];
class Holder {
  readonly k: Secret<u8[]>;
  constructor(seed: u8) {
    this.k = secret([seed, toU8(2)]);
  }
  wipe(): void {
    wipe(this.k);
  }
}
export const main = (): i32 => {
  const h: Holder = { k: secret(bytes()) };
  h.wipe();
  return 0;
};
`Holder` holds a `Secret` field and is made only by `new`: its constructor is the one place that field is set, from `secret(v)`, so an object literal or a decoded document cannot build it

NL2546

The message of an error contains:

…` is a `Secret` parameter, which its caller owns and wipes: returned, it would be a second name for that key in the caller, one of the two wiped by nobody or both read once it is wiped. Return a value computed from it inside `expose`, or a new `secret(...)` made from one

nl2546_secret_param_return.ts:

// NL2546: A function that returns the `Secret` it borrows would hand back a second owner of a holder's key.
import { Secret, expose, exposeWith, secret, wipe } from "nish:secret";
const sum = (k: u8[]): i32 => toI32(k.length);
const bytes = (): u8[] => [toU8(1), toU8(2)];
class Holder {
  readonly k: Secret<u8[]>;
  constructor() {
    this.k = secret(bytes());
  }
  wipe(): void {
    wipe(this.k);
  }
}
const id = (s: Secret<u8[]>): Secret<u8[]> => {
  return s;
};
export const main = (): i32 => {
  const h = new Holder();
  const a = id(h.k);
  wipe(a);
  h.wipe();
  return 0;
};
`s` is a `Secret` parameter, which its caller owns and wipes: returned, it would be a second name for that key in the caller, one of the two wiped by nobody or both read once it is wiped. Return a value computed from it inside `expose`, or a new `secret(...)` made from one

NL2547

The message of an error contains:

…` is wiped outside its holder's `wipe()`: a `Secret` field ends only in that method, on every path, so a wipe anywhere else would leave the holder holding zeros that its other uses then read and its own `wipe()` wipes again; call the holder's `wipe()` instead

nl2547_secret_field_wiped_outside.ts:

// NL2547: A holder's field is wiped only by its own `wipe()`: here it would then be read as zeros.
import { Secret, expose, exposeWith, secret, wipe } from "nish:secret";
const sum = (k: u8[]): i32 => toI32(k.length);
const bytes = (): u8[] => [toU8(1), toU8(2)];
class Holder {
  readonly k: Secret<u8[]>;
  constructor() {
    this.k = secret(bytes());
  }
  wipe(): void {
    wipe(this.k);
  }
}
export const main = (): i32 => {
  const h = new Holder();
  wipe(h.k);
  const n = expose(h.k, sum);
  h.wipe();
  return n;
};
`k` is wiped outside its holder's `wipe()`: a `Secret` field ends only in that method, on every path, so a wipe anywhere else would leave the holder holding zeros that its other uses then read and its own `wipe()` wipes again; call the holder's `wipe()` instead

NL2548

The message of an error contains:

…` is a `Secret` parameter, which its caller owns and wipes: wiped here, the caller would go on reading zeros and wipe it again. Leave the wipe to the caller, which knows when it is done

nl2548_secret_param_wipe.ts:

// NL2548: A function handed the field borrows it: wiping it would leave the holder reading zeros.
import { Secret, expose, exposeWith, secret, wipe } from "nish:secret";
const sum = (k: u8[]): i32 => toI32(k.length);
const bytes = (): u8[] => [toU8(1), toU8(2)];
class Holder {
  readonly k: Secret<u8[]>;
  constructor() {
    this.k = secret(bytes());
  }
  wipe(): void {
    wipe(this.k);
  }
}
const end = (s: Secret<u8[]>): void => {
  wipe(s);
};
export const main = (): i32 => {
  const h = new Holder();
  end(h.k);
  const n = expose(h.k, sum);
  h.wipe();
  return n;
};
`s` is a `Secret` parameter, which its caller owns and wipes: wiped here, the caller would go on reading zeros and wipe it again. Leave the wipe to the caller, which knows when it is done

NL2549

The message of an error contains:

` with no count after it: a run of `u8[]` parameters is followed by one `u64`, the byte count C may touch in each

NL2550

The message of an error contains:

…, and the parameter after a run of `u8[]` buffers is their byte count, a `u64`

NL2551

The message of an error contains:

…, and the only array a declared C function takes is a `u8[]` or a `readonly u8[]`, whose bytes have one layout on every target

NL2552

The message of an error contains:

…, and a buffer a declared C function takes is never null: it has no length to hold the count to, so a C function that accepts `NULL, 0` is declared with a `CPtr | null` parameter

NL2553

The message of an error contains:

`declare function syscall` is refused: `syscall` is variadic in C and bypasses libc, so declare the libc function that wraps the system call instead

NL2554

The message of an error contains:

`, and a call to a declared C function that takes a `u8[]` is not lowered yet: the declaration is checked, and the call waits for the release that passes the bytes and checks the count

Retired: the refusal of a call to a declare function that takes a u8[], until the call was lowered; the call passes data and holds the count to len now (tests/cases/ffi_buffer_call_sites, tests/cases/panics_ffi_length)

NL2555

The message of an error contains:

…, and a declared C function takes scalars, `CPtr`, a `string` it borrows as `const char *` and a `u8[]` buffer followed by its `u64` count

NL2556

The message of an error contains:

…` has methods, so it is made only by `new`: an object literal would be a plain object under Node, with none of the methods the class is used through. Write `new …

nl2556_class_literal.ts:

// NL2556: An object literal typed as a class with methods would be a plain object under Node, so the class is made only by `new`.
class Counter {
  n: i32;
  constructor(n: i32) {
    this.n = n;
  }
  get(): i32 {
    return this.n;
  }
}
export const main = (): i32 => {
  const c: Counter = { n: 5 };
  return c.get();
};
`Counter` has methods, so it is made only by `new`: an object literal would be a plain object under Node, with none of the methods the class is used through. Write `new Counter(...)`

NL2557

The message of an error contains:

…` has no variant left here: the tests before this read excluded every variant of its union, which TypeScript types `never` and this version has no type for

nl2557_union_exhausted.ts:

// NL2557: a union variable read after every tag was tested away.
enum Kind {
  Circle,
  Square,
  Tri,
}

class Circle {
  readonly kind: Kind.Circle = Kind.Circle;
  r: i32;
  constructor(r: i32) {
    this.r = r;
  }
}

class Square {
  readonly kind: Kind.Square = Kind.Square;
  side: i32;
  constructor(side: i32) {
    this.side = side;
  }
}

interface Tri {
  readonly kind: Kind.Tri;
  base: i32;
}

type Shape = Circle | Square | Tri;

const pick = (n: i32): Shape => {
  if (n % 3 === 0) {
    return new Circle(n);
  }
  if (n % 3 === 1) {
    return new Square(n);
  }
  const t: Tri = { kind: Kind.Tri, base: n };
  return t;
};
const f = (s: Shape): i32 => {
  if (s.kind === Kind.Circle) {
    return 1;
  }
  if (s.kind === Kind.Square) {
    return 2;
  }
  if (s.kind === Kind.Tri) {
    return 3;
  }
  const t: Shape = s;
  return t === s ? 0 : 4;
};
export const main = (): void => {
  console.log(f(pick(0)));
};
`s` has no variant left here: the tests before this read excluded every variant of its union, which TypeScript types `never` and this version has no type for

NL2558

The message of an error contains:

…` read through a property path: a test of the tag narrows a variable, not a path, so copy the union into a local first and test that

nl2558_union_path.ts:

// NL2558: a variant field read through a property path, which a tag test does not narrow.
enum Kind {
  Circle,
  Square,
  Tri,
}

class Circle {
  readonly kind: Kind.Circle = Kind.Circle;
  r: i32;
  constructor(r: i32) {
    this.r = r;
  }
}

class Square {
  readonly kind: Kind.Square = Kind.Square;
  side: i32;
  constructor(side: i32) {
    this.side = side;
  }
}

interface Tri {
  readonly kind: Kind.Tri;
  base: i32;
}

type Shape = Circle | Square | Tri;

const pick = (n: i32): Shape => {
  if (n % 3 === 0) {
    return new Circle(n);
  }
  if (n % 3 === 1) {
    return new Square(n);
  }
  const t: Tri = { kind: Kind.Tri, base: n };
  return t;
};
class H {
  node: Shape;
  constructor(node: Shape) {
    this.node = node;
  }
}
const f = (h: H): i32 => {
  if (h.node.kind === Kind.Circle) {
    return h.node.r;
  }
  return 0;
};
export const main = (): void => {
  console.log(f(new H(pick(0))));
};
`r` cannot be used on the union `Circle | Square | Tri` read through a property path: a test of the tag narrows a variable, not a path, so copy the union into a local first and test that

NL2559

The message of an error contains:

…` as its type, which every variant shares, so give it the one member it holds instead: `readonly …

nl2559_union_whole_enum.ts:

// NL2559: a variant whose tag is typed with its whole enum.
enum Kind {
  A,
  B,
}

class First {
  readonly kind: Kind = Kind.A;
  x: i32 = 1;
}

class Second {
  readonly kind: Kind.B = Kind.B;
  y: i32 = 2;
}

type Both = First | Second;

export const main = (): void => {
  const b: Both = new Second();
  console.log(b.kind === Kind.B);
};
`First` cannot be a union variant: its tag `kind` has the whole enum `Kind` as its type, which every variant shares, so give it the one member it holds instead: `readonly kind: Kind.A = Kind.A`

NL2560

The message of an error contains:

` cannot be used here: a handle may only be opened by `using`, called, or lent to a parameter of its type, so that the close its `using` block emits is the last thing that names it

NL2561

The message of an error contains:

` is a handle class, and its name may be written only as the type of a parameter that borrows one: a handle lives in the `using` block that opens it, and a value of this type anywhere else could outlive the close

NL2562

The message of an error contains:

` in a method of its class, passed straight to a `declare function` or bound once to a `const`, so that no name for it outlives the object or reaches a second close

NL2563

The message of an error contains:

… = g(...)` as a statement at the top of the constructor, where `g` is a `declare function` and nothing before it leaves the constructor: any other write would replace or copy a pointer the object owns

NL2564

The message of an error contains:

`: a handle class declares exactly one `CPtr` or `CPtr | null` field, `readonly` and with no initialiser, so the one pointer it owns is written once by its constructor and closed once by its dispose

NL2565

The message of an error contains:

…: a handle class has no type parameters, no `implements` and no `export`, so no other type and no other module can name it

NL2566

The message of an error contains:

` cannot declare `static readonly json`: a handle is never serialised, and its options would name the pointer it owns

NL2567

The message of an error contains:

`: a class that owns a C pointer and names no close would leak it at every exit

NL2568

The message of an error contains:

…; if (p !== null) { close(p); }` for a `declare function` `close`: the `using` block emits that call at every exit and runs nothing else

NL2569

The message of an error contains:

` is not a `declare function`: a handle is closed by the C function its dispose names, and nothing else

NL2570

The message of an error contains:

` must take exactly one argument, the pointer the object owns, and be declared with one parameter

NL2571

The message of an error contains:

`: test a `CPtr | null` field through a `const` first, as in `const p = this.

NL2572

The message of an error contains:

`, and only its `[Symbol.dispose]` may call it on the pointer the object owns: the `using` block closes it at every exit, so a close written here would be followed by a use after close and a second close

NL2573

The message of an error contains:

` is not lowered yet: until a release emits its close, hold the `CPtr` in a local and close it yourself

Retired: the refusal of a using of a handle class, until its close was lowered; every exit of the block closes the handle now (tests/cases/ffi_handle_file, tests/cases/ffi_handle_exits)

NL2574

The message of an error contains:

`Math.imul` expects an i32 or a u32, got …

NL2575

The message of an error contains:

`Math.sign` expects a number, got …

NL2576

The message of an error contains:

`, which the root package does not grant it; grant it with …

NL2577

The message of an error contains:

` does not record for it; review the chain, then record it with `--update-capability-lock`

NL2578

The message of an error contains:

` name and is used only through an import of it: add `import { …

NL2579

The message of an error contains:

` takes an array whose elements are the vector's lane type, …

NL2580

The message of an error contains:

` takes a lane index that is an integer literal, from 0 to …

NL2581

The message of an error contains:

…): compare lane by lane with `eq`, `lt` and the rest, which answer a mask, and ask it `all` or `any`

NL2582

The message of an error contains:

` vector as its first argument, got …

NL2583

The message of an error contains:

…, whose lanes are 0 to …

NL2584

The message of an error contains:

…: no target can start a thread on less, and a task that ran on it would overflow before it did any work; ask for at least …

NL2585

The message of an error contains:

…`, a `let` of the function it is written in: an arrow argument is handed what it captures as an argument when the call is made, so it may read only a `const` or a parameter of that function, whose value is the one the arrow is handed; bind the value to a `const` before the call…

nl2585_fnarg_capture_let.ts:

// NL2585: an arrow argument reads a `let`, whose value may change after the
// arrow is handed what it captures.
const apply = (f: (x: i32) => i32, x: i32): i32 => f(x);

export const main = (): i32 => {
  let k = 1;
  k = k + 1;
  return apply((x) => x + k, 1);
};
The arrow reads `k`, a `let` of the function it is written in: an arrow argument is handed what it captures as an argument when the call is made, so it may read only a `const` or a parameter of that function, whose value is the one the arrow is handed; bind the value to a `const` before the call, `const kNow = k`, and read `kNow` in the arrow

NL2586

The message of an error contains:

…`, which belongs to the function it is written in: an arrow argument is handed what it captures as an argument when the call is made, so an assignment would change only its own copy; make the arrow answer the value and assign it after the call, or write through an object the function holds (`box.n = v`, `list.push(v)`)

nl2586_fnarg_capture_write.ts:

// NL2586: an arrow argument assigns to a name it captured, which would change
// only its own copy.
const each = (xs: i32[], f: (x: i32) => void): void => {
  for (const x of xs) f(x);
};
const sum = (xs: i32[], total: i32): i32 => {
  each(xs, (x) => {
    total = total + x;
  });
  return total;
};
export const main = (): i32 => sum([1, 2], 0);
The arrow assigns to `total`, which belongs to the function it is written in: an arrow argument is handed what it captures as an argument when the call is made, so an assignment would change only its own copy; make the arrow answer the value and assign it after the call, or write through an object the function holds (`box.n = v`, `list.push(v)`)

NL2587

The message of an error contains:

…`, a function parameter of the function it is written in: a function parameter is bound when that function is instantiated, not handed over as a value, so an arrow lifted into a function of its own cannot reach it; pass `…

nl2587_fnarg_capture_function_param.ts:

// NL2587: an arrow argument names a function parameter of the function it is
// written in, which is no value it can be handed.
const apply = (f: (x: i32) => i32, x: i32): i32 => f(x);
const twiceThenOne = (g: (x: i32) => i32, x: i32): i32 => apply((v) => g(g(v)) + 1, x);
export const main = (): i32 => twiceThenOne((x) => x * 2, 1);
The arrow names `g`, a function parameter of the function it is written in: a function parameter is bound when that function is instantiated, not handed over as a value, so an arrow lifted into a function of its own cannot reach it; pass `g` itself where the arrow stands when it fits the parameter, or give the callee a function parameter of its own for it

NL2588

The message of an error contains:

…`, and an arrow argument cannot capture it: …

nl2588_fnarg_capture_secret.ts:

// NL2588: an arrow argument captures a value of a kind no arrow argument may
// carry, here a `Secret`.
import { Secret, expose, secret, wipe } from "nish:secret";
const bytes = (): u8[] => [1, 2];
const apply = (f: (x: i32) => i32, x: i32): i32 => f(x);
export const main = (): i32 => {
  const k = secret(bytes());
  const n = apply((x) => x + expose(k, (b: u8[]): i32 => b.length), 1);
  wipe(k);
  return n;
};
The arrow reads `k`, which is `Secret<u8[]>`, and an arrow argument cannot capture it: a `Secret` is read only inside `expose`

NL2589

The message of an error contains:

…` runs captures nothing: a capture it could write through would be a channel out of it that its result type does not name; pass the value as `exposeWith`'s argument instead

nl2589_secret_expose_capture_forwarded.ts:

// NL2589: an arrow that captures is handed on to `expose`, which runs only
// arrows that capture nothing.
import { Secret, expose, secret, wipe } from "nish:secret";
const bytes = (): u8[] => [1, 2];
const open = (k: Secret<u8[]>, f: (b: u8[]) => i32): i32 => expose(k, f);
export const main = (): i32 => {
  const k = secret(bytes());
  const leak: u8[] = [0, 0];
  const n = open(k, (b: u8[]): i32 => {
    leak.set(b, 0);
    return 0;
  });
  wipe(k);
  return n;
};
`(b) => ...` carries a value an arrow captured from the function it is written in, and what `expose` runs captures nothing: a capture it could write through would be a channel out of it that its result type does not name; pass the value as `exposeWith`'s argument instead

NL2590

The message of an error contains:

The arrow reads `this`: an arrow argument may read the `const` locals and the parameters of the function it is written in, and `this` is neither; bind it, or what the arrow needs of it, to a `const` before the call (`const self = this`, `const k = this.k`)

nl2590_fnarg_capture_this.ts:

// NL2590: an arrow argument reads `this`, which is neither a `const` local
// nor a parameter of the function it is written in.
const apply = (f: (x: i32) => i32, x: i32): i32 => f(x);
class Scaler {
  k: i32 = 2;
  run(x: i32): i32 {
    return apply((v) => v * this.k, x);
  }
}
export const main = (): i32 => new Scaler().run(3);
The arrow reads `this`: an arrow argument may read the `const` locals and the parameters of the function it is written in, and `this` is neither; bind it, or what the arrow needs of it, to a `const` before the call (`const self = this`, `const k = this.k`)

NL2591

The message of an error contains:

` takes an `i32[]`, got …

NL2592

The message of an error contains:

…: a presence field is a `boolean` that is not `readonly` and records whether one other field's key was given; it is off the wire, so no option renames it, makes it optional or records it

nl2592_json_presence_recorded.ts:

// NL2592: a field whose presence is recorded is itself named as a presence field.
import { JsonLimits } from "nish/json-tokenizer";
import { JsonDecoder, jsonDecode } from "nish/json-typed";

class Patch {
  static readonly json = { presence: { hasA: "hasB", a: "hasA" } };
  a: i32 = 0;
  b: i32 = 0;
  hasA: boolean = false;
  hasB: boolean = false;
}

export const main = (): i32 => {
  const src = new Array<u8>(2);
  const d = new JsonDecoder(src, 0, 2, new JsonLimits());
  jsonDecode(d, new Patch());
  return d.failed;
};
`Patch.json`'s `presence` gives `a` the field `hasA`, which is recorded by a presence field of its own: a presence field is a `boolean` that is not `readonly` and records whether one other field's key was given; it is off the wire, so no option renames it, makes it optional or records it

NL2593

The message of an error contains:

…); review the grant, then record it with `--update-capability-lock`

NL2594

The message of an error contains:

` grant does not cover: it is granted …

NL2595

The message of an error contains:

…`: a default is a literal, an enum member or the name of an earlier parameter of the same number, `boolean` or enum type, because each call that leaves the argument out passes it as a constant

nl2595_default_not_constant.ts:

// NL2595: A default is a constant the call passes, not an expression the callee runs.
const f = (x: i32, y: i32 = x + 1): i32 => {
  return x + y
};
export const main = (): void => {
  console.log(f(1))
};
Default of parameter `y`: a default is a literal, an enum member or the name of an earlier parameter of the same number, `boolean` or enum type, because each call that leaves the argument out passes it as a constant

NL2596

The message of an error contains:

…` has a type parameter's type, so a default cannot be one value: the type is the caller's, and TypeScript refuses a constant for it too

nl2596_default_type_parameter.ts:

// NL2596: A parameter of a type parameter's type has no one constant default.
const f = <T>(x: T, y: T = 0): T => {
  return x
};
export const main = (): void => {
  console.log(f(1))
};
Parameter `y` has a type parameter's type, so a default cannot be one value: the type is the caller's, and TypeScript refuses a constant for it too

NL2597

The message of an error contains:

…` follows a parameter with a default, so it needs a default too: a call leaves out arguments only from the end

nl2597_default_order.ts:

// NL2597: A parameter after one with a default has a default too.
const f = (x: i32 = 1, y: i32): i32 => {
  return x + y
};
export const main = (): void => {
  console.log(f(1, 2))
};
Parameter `y` follows a parameter with a default, so it needs a default too: a call leaves out arguments only from the end

NL2598

The message of an error contains:

…` has a default and no type annotation; write its type before the default, `…

nl2598_default_untyped.ts:

// NL2598: A default's parameter is annotated: the type is not inferred from the default.
const f = (x: i32, y = 2): i32 => {
  return x + y
};
export const main = (): void => {
  console.log(f(1))
};
Parameter `y` has a default and no type annotation; write its type before the default, `y: T = 2`

NL2599

The message of an error contains:

…` has both a parameter with a default and a rest parameter, which this version does not combine: write every argument before the rest, or pass the rest as an array

nl2599_rest_and_default.ts:

// NL2599: A default and a rest in one signature are not combined in this version.
const f = (x: i32 = 1, ...xs: i32[]): i32 => {
  return x + xs.length
};
export const main = (): void => {
  console.log(f())
};
`f` has both a parameter with a default and a rest parameter, which this version does not combine: write every argument before the rest, or pass the rest as an array

NL2600

The message of an error contains:

…` cannot have a default: a call that passes nothing for it passes `[]`

nl2600_rest_default.ts:

// NL2600: A rest parameter has no default: leaving it out passes `[]`.
const f = (...xs: i32[] = [1]): i32 => {
  return xs.length
};
export const main = (): void => {
  console.log(f())
};
Rest parameter `xs` cannot have a default: a call that passes nothing for it passes `[]`

NL2601

The message of an error contains:

…` has a rest parameter, which a call through a function parameter does not gather; declare it `…

nl2601_rest_function_value.ts:

// NL2601: A function with a rest is no value for a function parameter: a call through
// `f` passes one array where Node would gather every argument into `xs`.
const total = (...xs: i32[]): i32 => {
  return xs.length
};
const apply = (f: (a: i32[]) => i32): i32 => {
  return f([1, 2, 3])
};
export const main = (): void => {
  console.log(apply(total))
};
Argument 1 of `apply`: `total` has a rest parameter, which a call through a function parameter does not gather; declare it `xs: T[]` to pass it here

NL2602

The message of an error contains:

…` must have an array type, `...…

nl2602_rest_not_array.ts:

// NL2602: A rest parameter has an array type.
const f = (...xs: i32): i32 => {
  return xs
};
export const main = (): void => {
  console.log(f(1))
};
Rest parameter `xs` must have an array type, `...xs: T[]`

NL2603

The message of an error contains:

…` must be the last parameter

nl2603_rest_not_last.ts:

// NL2603: A rest parameter is the last one.
const f = (...xs: i32[], y: i32): i32 => {
  return xs.length + y
};
export const main = (): void => {
  console.log(f(1, 2))
};
Rest parameter `xs` must be the last parameter

NL2604

The message of an error contains:

…` cannot have a default or be a rest parameter: a call names the function it passes, and that is what instantiates the callee

nl2604_function_parameter_default.ts:

// NL2604: A function parameter names the function a call passes; it has no default.
const twice = (x: i32): i32 => {
  return x * 2
};
const apply = (x: i32, g: (a: i32) => i32 = twice): i32 => {
  return g(x)
};
export const main = (): void => {
  console.log(apply(1, twice))
};
Function parameter `g` cannot have a default or be a rest parameter: a call names the function it passes, and that is what instantiates the callee

NL2605

The message of an error contains:

Optional parameters are not supported: give `…

nl2605_optional_parameter.ts:

// NL2605: `x?: T` stays refused: a parameter a call may leave out has a default.
const f = (x: i32, y?: i32): i32 => {
  return x
};
export const main = (): void => {
  console.log(f(1))
};
Optional parameters are not supported: give `y` a default instead, `y: i32 = <default>`, which a call that leaves it out passes

NL2606

The message of an error contains:

Spread arguments are not supported: pass the array itself to a parameter of array type, or write each argument

nl2606_spread_argument.ts:

// NL2606: `f(...xs)` is refused: pass the array to a parameter of array type.
const total = (...xs: i32[]): i32 => {
  return xs.length
};
export const main = (): void => {
  const a = [1, 2]
  console.log(total(...a))
};
Spread arguments are not supported: pass the array itself to a parameter of array type, or write each argument

NL2607

The message of an error contains:

…` and cannot have a default in this version: a default is a number, a `boolean`, a `string` or an enum member

nl2607_default_type.ts:

// NL2607: A default is a number, a boolean, a string or an enum member.
const f = (x: i32, ys: i32[] = []): i32 => {
  return x + ys.length
};
export const main = (): void => {
  console.log(f(1))
};
Parameter `ys` is `i32[]` and cannot have a default in this version: a default is a number, a `boolean`, a `string` or an enum member

NL2608

The message of an error contains:

A rest parameter copies each `…

nl2608_rest_record_copy.ts:

// NL2608: A rest parameter of records takes no named record: the array would copy it.
interface Cell {
  v: number
}
const bumpAll = (...cs: Cell[]): void => {
  for (const c of cs) {
    c.v = c.v + 100
  }
};
export const main = (): void => {
  const p: Cell = { v: 1 }
  bumpAll(p, { v: 2 })
  console.log(p.v)
};
A rest parameter copies each `Cell` record it gathers, so a write through the copy would not reach this one as it does in TypeScript; take a `Cell[]` parameter and pass `[...]`, which copies the same way and says so

NL2609

The message of an error contains:

…region memory kept by memory outside the region points at freed memory once …

NL2610

The message of an error contains:

…memory outside a region kept by the region's memory may be released

NL2611

The message of an error contains:

…may answer only a number, a `boolean`, an `enum` or a `Region`

NL2612

The message of an error contains:

…another thread allocates from an arena of its own, not from the region

NL2613

The message of an error contains:

`, which holds a `Region`, and a region is entered only on the thread that made it

NL2614

The message of an error contains:

… runs answers the region's root, which must be memory it allocated, and …

NL2615

The message of an error contains:

…storage outside the region that grows inside it would move into the region

NL2616

The message of an error contains:

…, which the region rules have no facts for

No program provokes it: NL2425 for a region: every callee and every f handed to region, regionWith, enter or enterWith has whole-program facts, so the region rules never meet one they cannot see; the refusal stays so that such a callee is never let through unexamined

NL2617

The message of an error contains:

…inside a region they would read the region's own arena, not the thread's

NL2618

The message of an error contains:

A `Region` is made by `region(f)` or `regionWith(arg, f)`

NL2619

The message of an error contains:

`sort` takes one comparator: write `xs.sort((a, b) => a - b)`, a function answering a negative number, zero or a positive number (without one JavaScript compares the elements as strings)

NL2620

The message of an error contains:

`sort` needs a comparator: write `xs.sort((a, b) => a - b)`, a function answering a negative number, zero or a positive number (without one JavaScript compares the elements as strings)

NL2621

The message of an error contains:

` is a mask type and has no constructor: a mask is what a lane-wise compare (`eq`, `lt` and the rest) answers

NL2622

The message of an error contains:

… mask is a value held only in a local, a parameter or a return of a function this module keeps; store the vectors it selects, or its `bitmask`

NL2623

The message of an error contains:

` is float lane arithmetic and takes an `f32x4` or an `f64x2`, got …

NL2624

The message of an error contains:

` takes an `f32x4` or an `f64x2`, got …

NL2625

The message of an error contains:

` takes an integer vector, a `u8x16`, `u16x8`, `i32x4` or `u32x4`, got …

NL2626

The message of an error contains:

` takes an integer vector, a `u8x16`, `u16x8`, `i32x4` or `u32x4`, or a mask, got …

NL2627

The message of an error contains:

` takes a `nish:simd` mask, which a lane-wise compare answers, got …

NL2628

The message of an error contains:

…; reinterpret the lanes with `asU8x16` and the rest

NL2629

The message of an error contains:

… takes the mask their compare answers, …

NL2630

The message of an error contains:

` takes a count that is an integer literal below the lane width, from 0 to …

NL2631

The message of an error contains:

…: a shift by the width or more moves every bit out, and a rotate by it is a rotate by …

NL2632

The message of an error contains:

… lane indices, one per lane of the answer, got …

NL2633

The message of an error contains:

` takes lane indices that are integer literals, from 0 to …

NL2634

The message of an error contains:

` takes two vectors of one type and one lane index per lane, got …

NL2635

The message of an error contains:

… is out of range for two …

NL2636

The message of an error contains:

… can overflow its lane type, so its total is `sumWrapping`, which wraps as the lanes do

NL2637

The message of an error contains:

`sumWrapping` takes an `i32x4` or a `u32x4`, got …

NL2638

The message of an error contains:

` takes a `nish:simd` vector, got …

NL2639

The message of an error contains:

` does not return a `Promise<T>`: an async function is passed only where the function type says it is awaited

NL2640

The message of an error contains:

` returns a `Promise<T>`: only an async function or an `async` arrow is passed where the function type is awaited

NL2641

The message of an error contains:

` is only written inside an `async` function: make the function that holds it `async`, and `await` its calls

NL2642

The message of an error contains:

`await` stands at the top of a statement — an expression statement, a declaration's initializer, a `return` or the right side of `=` — and not inside another expression, which would hold a value across the wait

NL2643

The message of an error contains:

` takes a direct call to an `async` function, written where it is made: a promise is never a value to hold and await later

NL2644

The message of an error contains:

` takes a call to an `async` function, and `

NL2645

The message of an error contains:

` is `async`: a call to it is awaited where it is made (`await`), or handed to `spawn` or `awaitAll` — a promise is never a value

NL2646

The message of an error contains:

…: a spawned task answers `void` or a `Result` it reports itself

NL2647

The message of an error contains:

`awaitAll` is awaited into one name per call: `const [a, b] = await awaitAll(f(x), g(y))`, from two to eight calls

NL2648

The message of an error contains:

`): an awaited call's frame is nested in its caller's, so recursion through `await` has no size — write it as a loop

NL2649

The message of an error contains:

` is not supported: a promise is never a value — `await` each call where it is made, or `awaitAll(f(x), g(y))` from `nish:async` to wait on several together

NL2650

The message of an error contains:

` is not supported: a promise is never a value — `await` the call to `

NL2651

The message of an error contains:

`await(...)` outside an `async` function is a call to a function named `await`, and there is none: `await` is written inside an `async` function, before its call, `await f(x)`

NL2652

The message of an error contains:

` cannot be built for wasm: a task is resumed by the native event loop, which a wasm module does not have

NL2653

The message of an error contains:

` is checked, but not yet compiled: the lowering of `async` is not in this release

Retired: async refused as "checked, but not yet compiled" until its native lowering landed; a wasm build is still refused, by NL2652

NL2654

The message of an error contains:

`Promise<T>` is only an `async` function's return type: a promise is never a value, so it cannot be held, passed or stored — `await` the call where it is made

NL2655

The message of an error contains:

An `async` function's return type is `Promise<T>`: write the answer `T` inside it, `Promise<void>` for none

NL2656

The message of an error contains:

` is a variable of the entry module's top-level code, which runs as `main`, so a function cannot read it; pass it as a parameter, or declare it as an annotated module `const` above the code

NL2657

The message of an error contains:

… that reaches it: under Node a top-level statement runs where it stands, before the declarations after it exist, so declare it above that line

NL2658

The message of an error contains:

` is a variable of the top-level code and also a name the module declares or imports: under Node both live in the one module scope, so rename one

NL2659

The message of an error contains:

`, which has no `null`, so `null` cannot be its default: only a `T | null` parameter defaults to `null`

NL2660

The message of an error contains:

`, and the one default a `T | null` parameter may have is `null`, which each call that leaves the argument out passes as a constant; any other value would be built by every such call

NL2661

The message of an error contains:

…, and only a scalar, an enum or a `HostHandle` crosses between the page and wasm: write it into module storage and pass its index

nl2661_host_any_parameter.ts (compiled with --target wasm32 --host tests/wordings/host/any_parameter.ts):

import { size } from "./host/any_parameter"
export const f = (): i32 => {
  return 0
};
Parameter `v` of host function `size` is `any`, and only a scalar, an enum or a `HostHandle` crosses between the page and wasm: write it into module storage and pass its index

nl2661_host_class_parameter.ts (compiled with --target wasm32 --host tests/wordings/host/class_parameter.ts):

import { norm } from "./host/class_parameter"
export const f = (): i32 => {
  return 0
};
Parameter `p` of host function `norm` is `Point`, and only a scalar, an enum or a `HostHandle` crosses between the page and wasm: write it into module storage and pass its index

nl2661_host_function_parameter.ts (compiled with --target wasm32 --host tests/wordings/host/function_parameter.ts):

import { each } from "./host/function_parameter"
export const f = (): i32 => {
  return 0
};
Parameter `f` of host function `each` is a function type, and only a scalar, an enum or a `HostHandle` crosses between the page and wasm: write it into module storage and pass its index

nl2661_host_object_array_parameter.ts (compiled with --target wasm32 --host tests/wordings/host/object_array_parameter.ts):

import { count } from "./host/object_array_parameter"
export const f = (): i32 => {
  return 0
};
A parameter of host function `count` is an object type, and only a scalar, an enum or a `HostHandle` crosses between the page and wasm: write it into module storage and pass its index

nl2661_host_object_parameter.ts (compiled with --target wasm32 --host tests/wordings/host/object.ts):

import { area } from "./host/object"
export const f = (): i32 => {
  return 0
};
A parameter of host function `area` is an object type, and only a scalar, an enum or a `HostHandle` crosses between the page and wasm: write it into module storage and pass its index

nl2661_host_object_result.ts (compiled with --target wasm32 --host tests/wordings/host/object_result.ts):

import { corner } from "./host/object_result"
export const f = (): i32 => {
  return 0
};
The result of host function `corner` is an object type, and only a scalar, an enum or a `HostHandle` crosses between the page and wasm: write it into module storage and pass its index

nl2661_host_unknown_parameter.ts (compiled with --target wasm32 --host tests/wordings/host/unknown_parameter.ts):

import { size } from "./host/unknown_parameter"
export const f = (): i32 => {
  return 0
};
Parameter `v` of host function `size` is `unknown`, and only a scalar, an enum or a `HostHandle` crosses between the page and wasm: write it into module storage and pass its index

NL2662

The message of an error contains:

…` is generic, and a wasm import is one function with one signature: write it once per type it takes

nl2662_host_generic_function.ts (compiled with --target wasm32 --host tests/wordings/host/generic.ts):

import { first } from "./host/generic"
export const f = (): i32 => {
  return 0
};
Host function `first` is generic, and a wasm import is one function with one signature: write it once per type it takes

NL2663

The message of an error contains:

…` returns a `Promise`, which wasm cannot wait for: await it in the page and call an export with the result

nl2663_host_promise_result.ts (compiled with --target wasm32 --host tests/wordings/host/promise.ts):

import { later } from "./host/promise"
export const f = (): i32 => {
  return 0
};
Host function `later` returns a `Promise`, which wasm cannot wait for: await it in the page and call an export with the result

NL2664

The message of an error contains:

…, a buffer the page would lend wasm: pass it through module storage

Retired: a typed-array parameter of a host function refused until buffer windows landed; a window's element is NL2681 now, and its at: u32, n: u32 pair NL2682/NL2683

NL2665

The message of an error contains:

` is a `string`: strings did not cross yet, so write the text into module storage and pass its length

Retired: a string parameter of a host function, refused until strings crossed; it passes as its UTF-8 bytes' address and count, which the loader decodes

NL2666

The message of an error contains:

…` declares a C function, and a wasm module a page loads links no C: export `…

nl2666_declare_function_emit_dts.ts (compiled with --emit-dts build/test/wordings/nl2666_declare_function_emit_dts.d.ts):

declare function c_abs(x: i32): i32
export const f = (): i32 => {
  return c_abs(-1)
};
`declare function c_abs` declares a C function, and a wasm module a page loads links no C: export `c_abs` from a host module the page provides (`--host`) and import it from there

nl2666_declare_function_wasm.ts (compiled with --target wasm32):

declare function c_abs(x: i32): i32
export const f = (): i32 => {
  return c_abs(-1)
};
`declare function c_abs` declares a C function, and a wasm module a page loads links no C: export `c_abs` from a host module the page provides (`--host`) and import it from there

NL2667

The message of an error contains:

…` returns a `string`, which wasm cannot receive: return the text by writing it into a module-storage u8[] the module names, and return its length

nl2667_host_string_result.ts (compiled with --target wasm32 --host tests/wordings/host/name.ts):

import { title } from "./host/name"
export const f = (): i32 => {
  return 0
};
Host function `title` returns a `string`, which wasm cannot receive: return the text by writing it into a module-storage u8[] the module names, and return its length

NL2668

The message of an error contains:

…` is only allowed as the kind of a `HostHandle<"kind">`

nl2668_string_literal_type.ts:

export const f = (): i32 => {
  const s: "x" = "x"
  return 0
};
A string literal type `"x"` is only allowed as the kind of a `HostHandle<"kind">`

NL2669

The message of an error contains:

`HostHandle` takes one type argument, its kind as a string literal of letters, digits and `_`, e.g. `HostHandle<"canvas">`

nl2669_host_handle_bare.ts:

// A handle has a kind, and the bare name gives none.
type Canvas = HostHandle
export const f = (c: Canvas): i32 => {
  return 0
};
`HostHandle` takes one type argument, its kind as a string literal of letters, digits and `_`, e.g. `HostHandle<"canvas">`

nl2669_host_handle_kind.ts:

type Canvas = HostHandle<canvas>
export const f = (c: Canvas): i32 => {
  return 0
};
`HostHandle` takes one type argument, its kind as a string literal of letters, digits and `_`, e.g. `HostHandle<"canvas">`

NL2670

The message of an error contains:

…, and a handle starts as `0`, the handle that names nothing: every other one is given out by the page

nl2670_host_handle_field_literal.ts:

// A field's default may not forge a handle: only `0`, the one that names nothing.
type Canvas = HostHandle<"canvas">
type Socket = HostHandle<"socket">
class Slot {
  c: Canvas = 7
}
export const f = (): Canvas => {
  return new Slot().c
};
Field `c` of class `Slot` is HostHandle<"canvas">, and a handle starts as `0`, the handle that names nothing: every other one is given out by the page

nl2670_host_handle_field_negative.ts:

// Nor may a negative one: `0` is the one number a handle field starts as.
type Canvas = HostHandle<"canvas">
type Socket = HostHandle<"socket">
class Slot {
  c: Canvas = -5
}
export const f = (): Canvas => {
  return new Slot().c
};
Field `c` of class `Slot` is HostHandle<"canvas">, and a handle starts as `0`, the handle that names nothing: every other one is given out by the page

nl2670_host_handle_param_literal.ts:

// A parameter's default may not forge a handle either: only `0`, the one that names nothing.
type Canvas = HostHandle<"canvas">
export const f = (c: Canvas = 7): Canvas => {
  return c
};
Parameter `c` is HostHandle<"canvas">, and a handle starts as `0`, the handle that names nothing: every other one is given out by the page

NL2671

The message of an error contains:

` is a wait on the executor, written only where it is awaited: `await sleep(ms)`, `await readable(fd)` or `await writable(fd)`, inside an `async` function

NL2672

The message of an error contains:

` takes one `i32`: the milliseconds `sleep` waits, or the descriptor `readable` and `writable` wait on

NL2673

The message of an error contains:

…, which lives in the spawner's region and is freed when the spawner returns, while the task may still wait: a spawned task takes numbers, `boolean`s, enums and `string`s, which are copied into its own region, and objects and arrays, which it shares with the spawner

NL2674

The message of an error contains:

` is a place, not a value: read and write its fields (`

NL2675

The message of an error contains:

` cannot be passed as an argument: it is a place, not a value; pass the index, or the fields the callee needs

NL2676

The message of an error contains:

` needs a length known at compile time: a literal, a module constant, or arithmetic over them, because the page reads its layout from the type

NL2677

The message of an error contains:

` is a place, not a value: index it (`

NL2678

The message of an error contains:

`, which is a place, not a value: loop over the index and use `

NL2679

The message of an error contains:

… but its `new` makes …

NL2680

The message of an error contains:

…; a storage holds from 1 to …

NL2681

The message of an error contains:

…, and a buffer window lends only `u8[]`, `u16[]`, `u32[]`, `i32[]`, `f32[]` or `f64[]` (or the typed array of each): copy the values into one of those

nl2681_host_window_element.ts (compiled with --target wasm32 --host tests/wordings/host/window_element.ts):

import { fill } from "./host/window_element"
export const f = (): i32 => {
  return 0
};
Parameter `xs` of host function `fill` is an array, and a buffer window lends only `u8[]`, `u16[]`, `u32[]`, `i32[]`, `f32[]` or `f64[]` (or the typed array of each): copy the values into one of those

NL2682

The message of an error contains:

…` is a buffer window with no `at: u32, n: u32` after it: a window is the buffer followed by the first element the page may touch and how many

nl2682_host_window_missing.ts (compiled with --target wasm32 --host tests/wordings/host/buffer.ts):

import { fill } from "./host/buffer"
export const f = (): i32 => {
  return 0
};
Parameter `xs` of host function `fill` is a buffer window with no `at: u32, n: u32` after it: a window is the buffer followed by the first element the page may touch and how many

NL2683

The message of an error contains:

…`: a window's index and count are `u32`, the width of a wasm32 address

nl2683_host_window_width.ts (compiled with --target wasm32 --host tests/wordings/host/window_width.ts):

import { fill } from "./host/window_width"
export const f = (): i32 => {
  return 0
};
Parameter `at` of host function `fill` is `u64`, the first element of the window `xs`: a window's index and count are `u32`, the width of a wasm32 address

NL2684

The message of an error contains:

` go further than indexing: storage keeps its length, so a callee it is lent to only indexes the parameter, reads its `length`, loops over it, calls a method that reads it, or lends it on the same way — never passes it elsewhere, binds, returns or stores it, where it could be spelled `T[]` and resized

NL2685

The message of an error contains:

…: a record field that is itself a record is a pointer to it, and storage holds no pointer; spell the inner fields on the outer record

NL2686

The message of an error contains:

…: its bytes are zero-filled where a page can read them, so an element is a number, a `boolean`, an enum or range whose zero is a member, a `HostHandle`, or an interface no class implements whose fields are all of these — never a string, an array, a class, a `Map` or a `| null`

NL2687

The message of an error contains:

…; give the type a zero member, or store the number and convert it where it is read

NL2688

The message of an error contains:

` is not available: index it, and keep a count beside it for the slots in use

NL2689

The message of an error contains:

…what a task hands on may be memory of its own region, freed while the task it spawned still holds it

nl2689_task_nested_spawn.ts:

// NL2689: a spawned task may not itself spawn a task with an object or an
// array: what it hands on may be memory of its own region, which is freed when
// it ends while the task it spawned still holds it.
import { sleep, spawn } from "nish:async"

class Box {
  n: i32 = 0
}

async function inner(box: Box): Promise<void> {
  await sleep(1)
  console.log(`${box.n}`)
}

async function outer(n: i32): Promise<void> {
  const box = new Box()
  box.n = n
  spawn(inner(box))
}

export async function main(): Promise<i32> {
  spawn(outer(3))
  return 0
}
`outer` runs as a spawned task, in a region of its own that is freed when it ends, and spawns a task with an object or an array, itself or through a callee: what a task hands on may be memory of its own region, freed while the task it spawned still holds it

NL2690

The message of an error contains:

…a spawned task's function argument captures only numbers, booleans, enums and objects or arrays it may share…

nl2690_spawn_capture.ts:

// NL2690: a spawned task's function argument captures only numbers, booleans,
// enums and objects or arrays it may share. The arrow reads the string `tag`
// of `start`, which the task may not share.
import { sleep, spawn } from "nish:async"

async function later(f: (n: i32) => i32, n: i32): Promise<void> {
  await sleep(1)
  console.log(`${f(n)}`)
}

async function start(base: i32): Promise<void> {
  const tag = `n${base}`
  spawn(later((n: i32): i32 => n + tag.length, 2))
}

export async function main(): Promise<i32> {
  await start(40)
  return 0
}
`spawn` hands `later<(n) => ...>` an arrow that reads `tag` of type string of the function it is written in: a spawned task's function argument captures only numbers, booleans, enums and objects or arrays it may share, because the task may run after that function returns; pass the value to the task as an argument of its own, or hand it a top-level function

NL2691

The message of an error contains:

…a task shares with its spawner only objects and arrays whose every part outlives the spawner's call; pass the task what it needs of it instead

NL2692

The message of an error contains:

` takes two `i32`s: the descriptor it waits on, and the milliseconds it waits at most before it answers false

NL2693

The message of an error contains:

` from `nish:async` takes …

NL2694

The message of an error contains:

`nish.host` is an array of strings, each the path of a host module relative to `package.json`

NL2695

The message of an error contains:

…a `nish.host` entry is a host module's path relative to the directory `package.json` is in

NL2696

The message of an error contains:

…a `nish.host` entry names a host module the package has

NL2697

The message of an error contains:

`find` is not supported: TypeScript types its answer `T | undefined`, and Nish has no `undefined`, so a test of it would pass `tsc` and mean something else under Node; use `findIndex` and index the array, `const i = xs.findIndex(f); if (i >= 0) { ... xs[i] ... }`

NL2698

The message of an error contains:

… parameters, and Nish passes at most two, the element and its index: JavaScript's third, the array itself, is the receiver the call already names, so read that instead

NL2699

The message of an error contains:

`slice` on an array expects 0 to 2 arguments, got …

NL2700

The message of an error contains:

…the clock and the random source answer differently on every run

NL2701

The message of an error contains:

…the environment and the terminal answer differently on every host

NL2702

The message of an error contains:

…: a child region's parent is the region entered when it is made, so `child` and `childWith` are called only in the function `enter` or `enterWith` runs, and that function only by them

NL2703

The message of an error contains:

…` may spread `null`, which under Node copies no field at all: narrow it first, taking a member into a local to do so (`const s = o.f; if (s !== null) { ... { ...s } ... }`)

nl2703_object_spread_nullable.ts:

// NL2703: Spreading `null` copies no field under Node, so a maybe-null source is
// narrowed first.
interface Point {
  x: i32;
}

class Holder {
  point: Point | null = null;
}

export const main = (): i32 => {
  const h = new Holder();
  const p: Point = { ...h.point };
  return p.x;
};
Spread of `Point | null` into `Point` may spread `null`, which under Node copies no field at all: narrow it first, taking a member into a local to do so (`const s = o.f; if (s !== null) { ... { ...s } ... }`)

NL2704

The message of an error contains:

…`: which fields a spread copies depends on the variant it holds at run time, so narrow it to one variant first, or set the fields by name

nl2704_object_spread_union.ts:

// NL2704: Which fields a union value has depends on the variant it holds, so a spread
// of one is refused until it is narrowed.
enum Kind {
  Dot,
  Box,
}

interface Dot {
  readonly kind: Kind.Dot;
  x: i32;
}

interface Box {
  readonly kind: Kind.Box;
  side: i32;
}

interface Out {
  kind: Kind;
  x: i32;
}

export const pick = (m: Dot | Box): i32 => {
  const o: Out = { ...m, x: 1 };
  return o.x;
};

export const main = (): i32 => {
  const d: Dot = { kind: Kind.Dot, x: 3 };
  return pick(d);
};
Spread of the union `Dot | Box` into `Out`: which fields a spread copies depends on the variant it holds at run time, so narrow it to one variant first, or set the fields by name

NL2705

The message of an error contains:

…`: only a value of a class or interface the program declares has fields an object spread copies one by one, so set each field by name

nl2705_object_spread_not_struct.ts:

// NL2705: An object spread copies a declared class's or interface's fields one by one;
// a number has none, and Node would copy nothing at all from it.
interface Point {
  x: i32;
}

export const main = (): i32 => {
  const a: i32 = 1;
  const p: Point = { ...a, x: 2 };
  return p.x;
};
Spread of `i32` into `Point`: only a value of a class or interface the program declares has fields an object spread copies one by one, so set each field by name

NL2706

The message of an error contains:

…` does not declare: Node copies every own field of an instance and none of its methods, so a class instance is spread only into a type that declares each of its fields; otherwise set the fields by name

nl2706_object_spread_class_extra.ts:

// NL2706: Node copies every own field of a class instance, so one is spread only into a
// type that declares each of them.
interface Point {
  x: i32;
}

class Tagged {
  x: i32 = 1;
  tag: i32 = 2;
}

export const main = (): i32 => {
  const p: Point = { ...new Tagged() };
  return p.x;
};
Spread of the `Tagged` instance into `Point` copies its field `tag`, which `Point` does not declare: Node copies every own field of an instance and none of its methods, so a class instance is spread only into a type that declares each of its fields; otherwise set the fields by name

NL2707

The message of an error contains:

…` does not declare, and Node would carry it on the object anyway: spread a value whose every field the target declares, or set the fields by name

nl2707_object_spread_extra_field.ts:

// NL2707: Node carries every field of the source onto the object, so a source field
// the target does not declare is refused rather than dropped.
interface Point {
  x: i32;
}

interface Point3 {
  x: i32;
  z: i32;
}

export const main = (): i32 => {
  const a: Point3 = { x: 1, z: 2 };
  const p: Point = { ...a };
  return p.x;
};
Spread of `Point3` into `Point` copies field `z`, which `Point` does not declare, and Node would carry it on the object anyway: spread a value whose every field the target declares, or set the fields by name

NL2708

The message of an error contains:

…`: a spread copies each field as it is, so the two types must be the same; set it by name (`…

nl2708_object_spread_field_type.ts:

// NL2708: A spread copies each field as it is, so a field's type in the source and the
// target must be the same.
interface Wide {
  x: i64;
}

interface Narrow {
  x: i32;
}

export const main = (): i32 => {
  const a: Narrow = { x: 1 };
  const w: Wide = { ...a };
  return 0;
};
Field `x` spread from `Narrow` is `i32`, but `Wide` declares it `i64`: a spread copies each field as it is, so the two types must be the same; set it by name (`x: s.x`)

NL2709

The message of an error contains:

An object literal with a spread needs a contextual class or interface type, whose layout the spread's fields are copied into: annotate the variable (`const p: P = { ...s, x: 1 }`)

nl2709_object_spread_no_target.ts:

// NL2709: A spread copies its source's fields into the target's layout, so the literal
// needs a declared class or interface type to copy into.
interface Point {
  x: i32;
}

export const main = (): i32 => {
  const a: Point = { x: 1 };
  const p = { ...a };
  return p.x;
};
An object literal with a spread needs a contextual class or interface type, whose layout the spread's fields are copied into: annotate the variable (`const p: P = { ...s, x: 1 }`)

NL2710

The message of an error contains:

…` after it, which TypeScript refuses (TS2783): set it after the spread, or leave it out

nl2710_object_spread_overwritten.ts:

// NL2710: A field set by name and then always overwritten by a later spread is
// TypeScript's TS2783; the name was dead, so it is refused.
interface Point {
  x: i32;
  y: i32;
}

export const main = (): i32 => {
  const a: Point = { x: 1, y: 2 };
  const p: Point = { x: 5, ...a };
  return p.x;
};
Field `x` is set by name and then always overwritten by the spread of `Point` after it, which TypeScript refuses (TS2783): set it after the spread, or leave it out

NL2711

The message of an error contains:

… spreads another value into the typed JSON options: typed JSON reads a class's options from its text, where a spread names nothing it can see, so write each option out by name

nl2711_json_options_spread.ts:

// NL2711: Typed JSON options are read from the class's text: a spread into them names
// nothing that text can show, so it is refused with its own message.
import { JsonWriter } from "nish/json";
import { jsonEncode } from "nish/json-typed";

const base: i32 = 0;

class User {
  static readonly json = { ...base, optional: ["id"] };
  id: i32 = 0;
}

export const main = (): i32 => {
  const w = new JsonWriter(new Array<u8>(64), 0);
  jsonEncode(w, new User());
  return w.failed;
};
`User.json` spreads another value into the typed JSON options: typed JSON reads a class's options from its text, where a spread names nothing it can see, so write each option out by name

NL2712

The message of an error contains:

…: use it, delete it, or rename it `…

nl2712_unread_local.ts:

// NL2712: a local nothing reads is a value the program forgot to use; a name
// that starts with `_` says it is unused on purpose.
export const main = (): i32 => {
  const total: i32 = 2 + 3;
  const doubled = total * 2;
  return total;
};
`doubled` is declared but never read: use it, delete it, or rename it `_doubled` if it is unused on purpose

NL2713

The message of an error contains:

This statement has no effect: its value is computed and thrown away…

nl2713_no_effect.ts:

// NL2713: a statement with no call, store or `await` in it computes a value
// and throws it away.
export const main = (): i32 => {
  let state: i32 = 0;
  const next: i32 = 3;
  state === next;
  return state;
};
This statement has no effect: its value is computed and thrown away (to assign, write `=`); delete it, or use the value

NL2714

The message of an error contains:

…` to itself, which does nothing

nl2714_self_assign.ts:

// NL2714: a value stored into itself does nothing.
export const main = (): i32 => {
  let n: i32 = 4;
  n = n;
  return n;
};
`n = n` assigns `n` to itself, which does nothing

NL2715

The message of an error contains:

…` does not depend on its value; one of them is probably meant to be something else (to mean it, copy the value to a second `const` first)

nl2715_same_operands.ts:

// NL2715: with the same operand on both sides, `===` is always true; the
// other side was meant to be something else.
export const main = (): i32 => {
  const a: i32 = 1;
  const b: i32 = 2;
  if (a === a) {
    return b;
  }
  return 0;
};
Both operands of `===` are `a`, so `a === a` does not depend on its value; one of them is probably meant to be something else (to mean it, copy the value to a second `const` first)

NL2716

The message of an error contains:

…` is already tested by an earlier branch of this `if`, so this branch never runs; test something else, or merge the two branches

nl2716_repeated_condition.ts:

// NL2716: an `else if` that repeats an earlier condition can never run.
const size = (n: i32): i32 => {
  if (n > 10) {
    return 2;
  } else if (n > 10) {
    return 3;
  }
  return 1;
};

export const main = (): i32 => size(5) - 1;
`n > 10` is already tested by an earlier branch of this `if`, so this branch never runs; test something else, or merge the two branches

NL2717

The message of an error contains:

…; compare a signed value, or against a bound other than 0

nl2717_unsigned_zero.ts:

// NL2717: an unsigned value is never below zero, so this test always answers
// the same.
export const main = (): i32 => {
  const n: u32 = toU32(7);
  if (n < 0) {
    return 1;
  }
  return 0;
};
`n` is u32, which is never below 0, so `n < 0` is always false; compare a signed value, or against a bound other than 0

NL2718

The message of an error contains:

` reads a file, and a freestanding wasm32 build has no file system to read (build with `--profile wasi`)

NL2719

The message of an error contains:

…`, a tagged union: `jsonDecode` decodes into the object it is given, and which variant a document holds is known only once its tag is read, so a union is decoded with `jsonDecodeUnion(d, prev)`, which answers the object it decoded into

nl2719_json_union_decode.ts:

// NL2719: `jsonDecode` decodes into the object it is given, and a union's
// variant is known only once its tag is read.
import { JsonLimits } from "nish/json-tokenizer";
import { JsonDecoder, jsonDecode } from "nish/json-typed";

enum Kind {
  A,
  B,
}
class A {
  readonly kind: Kind.A = Kind.A;
}
class B {
  readonly kind: Kind.B = Kind.B;
}
type AB = A | B;
const make = (): AB => new A();

export const main = (): i32 => {
  const src = new Array<u8>(2);
  const d = new JsonDecoder(src, 0, 2, new JsonLimits());
  return jsonDecode(d, make());
};
`jsonDecode`'s value is `A | B`, a tagged union: `jsonDecode` decodes into the object it is given, and which variant a document holds is known only once its tag is read, so a union is decoded with `jsonDecodeUnion(d, prev)`, which answers the object it decoded into

NL2720

The message of an error contains:

…`: a decoder picks a union's variant by the string its tag is written as, so each variant of a union writes a different one

nl2720_json_union_same_tag.ts:

// NL2720: two variants of one union write their tag as the same string, so a
// decoder could not tell them apart.
import { JsonWriter } from "nish/json";
import { jsonEncode } from "nish/json-typed";

enum Kind {
  A,
  B,
}
class A {
  readonly kind: Kind.A = Kind.A;
}
class B {
  static readonly json = { tag: "A" };
  readonly kind: Kind.B = Kind.B;
}
type AB = A | B;
const make = (): AB => new A();

export const main = (): i32 => {
  const w = new JsonWriter(new Array<u8>(16), 0);
  jsonEncode(w, make());
  return w.failed;
};
`A` and `B` both write their tag in `A | B` as `"A"`: a decoder picks a union's variant by the string its tag is written as, so each variant of a union writes a different one

NL2721

The message of an error contains:

…`: a decoder finds a union's tag before it knows which variant the object is, so every variant of a union writes its tag under the same key

nl2721_json_union_tag_key.ts:

// NL2721: the variants of one union write their tag under different keys, so a
// decoder could not find the tag before it knows the variant.
import { JsonWriter } from "nish/json";
import { jsonEncode } from "nish/json-typed";

enum Kind {
  A,
  B,
}
class A {
  readonly kind: Kind.A = Kind.A;
}
class B {
  static readonly json = { rename: { kind: "type" } };
  readonly kind: Kind.B = Kind.B;
}
type AB = A | B;
const make = (): AB => new A();

export const main = (): i32 => {
  const w = new JsonWriter(new Array<u8>(16), 0);
  jsonEncode(w, make());
  return w.failed;
};
`A` writes its tag `kind` under the key `"kind"` and `B` under `"type"`: a decoder finds a union's tag before it knows which variant the object is, so every variant of a union writes its tag under the same key

NL2722

The message of an error contains:

…`'s `tag` is not a string literal: a variant's tag is written on the wire as a string, and typed JSON reads which one from the class's text

nl2722_json_union_tag_literal.ts:

// NL2722: a variant's `tag` is written on the wire as a string, read from the
// class's text.
import { JsonWriter } from "nish/json";
import { jsonEncode } from "nish/json-typed";

enum Kind {
  A,
  B,
}
class A {
  static readonly json = { tag: 1 };
  readonly kind: Kind.A = Kind.A;
}
class B {
  readonly kind: Kind.B = Kind.B;
}
type AB = A | B;
const make = (): AB => new A();

export const main = (): i32 => {
  const w = new JsonWriter(new Array<u8>(16), 0);
  jsonEncode(w, make());
  return w.failed;
};
`A.json`'s `tag` is not a string literal: a variant's tag is written on the wire as a string, and typed JSON reads which one from the class's text

NL2723

The message of an error contains:

…`, which is not a tagged union: `jsonDecodeUnion` picks a variant by the object's tag and answers it, so its value is a union of classes; a class is decoded into with `jsonDecode(d, out)`

nl2723_json_union_not_union.ts:

// NL2723: `jsonDecodeUnion` picks a variant by its tag, so its value is a
// union; a class is decoded into with `jsonDecode`.
import { JsonLimits } from "nish/json-tokenizer";
import { JsonDecoder, jsonDecodeUnion } from "nish/json-typed";

class Point {
  x: i32 = 0;
}

export const main = (): i32 => {
  const src = new Array<u8>(2);
  const d = new JsonDecoder(src, 0, 2, new JsonLimits());
  const p = jsonDecodeUnion(d, new Point());
  return p.x;
};
`jsonDecodeUnion`'s value is `Point`, which is not a tagged union: `jsonDecodeUnion` picks a variant by the object's tag and answers it, so its value is a union of classes; a class is decoded into with `jsonDecode(d, out)`

NL2724

The message of an error contains:

…` has no field whose type is one enum member: `tag` is the string a tagged union's variant writes its tag as, so only a variant gives one

nl2724_json_union_tag_no_field.ts:

// NL2724: `tag` names the string a variant writes its tag as, and a class with
// no tag field is no variant.
import { JsonWriter } from "nish/json";
import { jsonEncode } from "nish/json-typed";

class Point {
  static readonly json = { tag: "point" };
  x: i32 = 0;
}

export const main = (): i32 => {
  const w = new JsonWriter(new Array<u8>(16), 0);
  jsonEncode(w, new Point());
  return w.failed;
};
`Point.json` gives a `tag`, and `Point` has no field whose type is one enum member: `tag` is the string a tagged union's variant writes its tag as, so only a variant gives one

NL2725

The message of an error contains:

…` is generic: a function reference names one non-generic top-level function

nl2725_fnref_generic.ts:

// NL2725: a generic function named as a value. A reference names one body,
// and a generic function has one per instantiation.
const id = <T>(x: T): T => x;

export const main = (): i32 => {
  const f = id;
  return 0;
};
`id` is generic: a function reference names one non-generic top-level function

NL2726

The message of an error contains:

…` takes a function parameter, so it has no single body to refer to

nl2726_fnref_template.ts:

// NL2726: a function that takes a function parameter, named as a value. It is
// a template compiled once per callee, so there is no single body to tag.
const apply = (f: (x: i32) => i32, x: i32): i32 => f(x);

export const main = (): i32 => {
  const g = apply;
  return 0;
};
`apply` takes a function parameter, so it has no single body to refer to

NL2727

The message of an error contains:

…` is async: a function reference cannot hold it, because its call is a promise

nl2727_fnref_async.ts:

// NL2727: an async function named as a value.
const later = async (x: i32): Promise<i32> => x;

export const main = (): i32 => {
  const f = later;
  return 0;
};
`later` is async: a function reference cannot hold it, because its call is a promise

NL2728

The message of an error contains:

…` is declared in C: a function reference names a function this program defines

nl2728_fnref_foreign.ts:

// NL2728: a `declare function` named as a value: its body is C's.
declare function abs(x: i32): i32;

export const main = (): i32 => {
  const f = abs;
  return 0;
};
`abs` is declared in C: a function reference names a function this program defines

NL2729

The message of an error contains:

…: a function reference names a function whose every parameter the caller passes

nl2729_fnref_default.ts:

// NL2729: a function with a defaulted parameter named as a value. A call
// through a reference passes every parameter, so the function must take
// exactly those.
const scale = (x: i32, by: i32 = 2): i32 => x * by;

export const main = (): i32 => {
  const f = scale;
  return 0;
};
`scale` has a parameter with a default: a function reference names a function whose every parameter the caller passes

nl2729_fnref_rest.ts:

// NL2729: a function with a rest parameter named as a value. A call through
// a reference passes every parameter, so the function must take exactly those.
const sum = (...xs: i32[]): i32 => xs.length;

export const main = (): i32 => {
  const f = sum;
  return 0;
};
`sum` has a rest parameter: a function reference names a function whose every parameter the caller passes

NL2730

The message of an error contains:

…>(n)` would zero-fill with 0, which names no function; build it with `[]` and `push` instead

nl2730_fnref_zero_fill.ts:

// NL2730: an array of references zero-filled by its constructor. The tags
// start at 1, so 0 names no function.
export const main = (): i32 => {
  const fs = new Array<(x: i32) => i32>(4);
  return fs.length;
};
`new Array<(i32) => i32>(n)` would zero-fill with 0, which names no function; build it with `[]` and `push` instead

NL2731

The message of an error contains:

…`: a function reference names a top-level function and carries no environment

nl2731_fnref_capturing_arrow.ts:

// NL2731: an arrow in a value position that reads a local of the function
// around it. A reference is a tag, so it has nowhere to keep `n`.
export const main = (): i32 => {
  const n = 3;
  const f = (x: i32): i32 => x + n;
  return f(1);
};
this arrow captures `n`: a function reference names a top-level function and carries no environment

NL2732

The message of an error contains:

…` is a method: a function reference names a top-level function, and a method needs its receiver

nl2732_fnref_method.ts:

// NL2732: a method named as a value. A reference names a top-level function,
// and a method needs the receiver it was named through.
class Counter {
  n: i32 = 0;
  bump(by: i32): i32 {
    this.n = this.n + by;
    return this.n;
  }
}

export const main = (): i32 => {
  const c = new Counter();
  const f = c.bump;
  return 0;
};
`c.bump` is a method: a function reference names a top-level function, and a method needs its receiver

NL2733

The message of an error contains:

… holds a function reference that code outside this program can reach: a reference is a tag this program's dispatcher reads, not an address, and a host could hand back one no dispatcher knows

nl2733_fnref_host_return.ts:

// NL2733: an exported function returns a reference. Code outside the program
// could keep the tag and hand back one no dispatcher's set holds.
const double = (x: i32): i32 => x * 2;

export const pick = (): ((x: i32) => i32) => double;

export const main = (): i32 => pick()(2);
The return type of `pick` holds a function reference that code outside this program can reach: a reference is a tag this program's dispatcher reads, not an address, and a host could hand back one no dispatcher knows

NL2734

The message of an error contains:

…` takes a top-level function or an arrow named at the call

nl2734_fnref_to_function_parameter.ts:

// NL2734: a reference passed where a function parameter of `parallelMap` is.
// An ordinary function takes a reference as the arrow that calls it; what runs
// on other threads takes only a function or an arrow named at the call.
import { parallelMap } from "nish/threads";

type F = (x: i32) => i32;
const double = (x: i32): i32 => x * 2;

export const main = (): i32 => {
  const h: F = double;
  return parallelMap([1, 2], h).length;
};
`h` is a function reference: `parallelMap` takes a top-level function or an arrow named at the call

NL2735

The message of an error contains:

…` takes a function parameter, so no function it could name has a single body to refer to

nl2735_fnref_type_function_param.ts:

// NL2735: a function type that takes a function, as a reference's type. Every
// function it could name is a template, with no single body to tag.
export const main = (): i32 => {
  const fs: ((g: (x: i32) => i32) => i32)[] = [];
  return fs.length;
};
`(g: (x: i32) => i32) => i32` takes a function parameter, so no function it could name has a single body to refer to

NL2736

The message of an error contains:

…`, which does not, so every call here would allocate `…

nl2736_fnref_escape_join.ts:

// NL2736: a call through a reference that can reach a function keeping its
// argument and one that does not. The dispatcher's facts are its arms' join,
// so every call would lose the argument's stack place.
class Point {
  x: i32;
  constructor(x: i32) {
    this.x = x;
  }
}

class Box {
  last: Point | null;
  constructor() {
    this.last = null;
  }
}

const u0 = (p: Point, b: Box): i32 => {
  return p.x;
};

const u3 = (p: Point, b: Box): i32 => {
  b.last = p;
  return p.x;
};

export const main = (): i32 => {
  let h = u0;
  if (Date.now() > 0) {
    h = u3;
  }
  return h(new Point(1), new Box());
};
this call through `h` can reach `u3`, which keeps `p`, and `u0`, which does not, so every call here would allocate `p` in the arena; `u3` reaches `h` through `h = u3` (nl2736_fnref_escape_join.ts:30): call `u3` directly, or store it where `h` does not read

NL2737

The message of an error contains:

` is a function reference: it can be called, compared, stored and passed, nothing else

NL2738

The message of an error contains:

… cannot hold a function reference as its value: a `Map` value is not yet a place whose functions the compiler tracks; keep the references in an array and store the index in the map

Retired: ... cannot hold a function reference as its value: a Map value is not yet a place ...; a Map value became a place, so a reference, an array of them or a nullable of either is a value now (tests/cases/fnref_map*)

NL2739

The message of an error contains:

…` takes a `MutexGuard`: a guard is handed only to a function named at the call, so a function reference cannot name it; call the helper by name

nl2739_fnref_guard.ts:

// NL2739: a function that takes a `MutexGuard` named as a value.
import { MutexGuard } from "nish/threads";

class Counter {
  n: i32 = 0;
}

const bump = (g: MutexGuard<Counter>): void => {
  g.value.n = g.value.n + 1;
};

export const main = (): i32 => {
  const f = bump;
  return f === bump ? 0 : 1;
};
`bump` takes a `MutexGuard`: a guard is handed only to a function named at the call, so a function reference cannot name it; call the helper by name

NL2740

The message of an error contains:

` cannot be an array element: a C struct layout is lent to C one at a time, by pointer

NL2741

The message of an error contains:

A generic `declare class` is not supported: a C struct layout has no type parameters

NL2742

The message of an error contains:

…, and a C struct is only lent to C by pointer: take a `

NL2743

The message of an error contains:

….<field>`) or pass it to a `declare function`, but it cannot be held as a value

NL2744

The message of an error contains:

…()`, which zero-fills it: an object literal cannot lay out a C struct

NL2745

The message of an error contains:

`: a `declare class` embedded in its object is written field by field

NL2746

The message of an error contains:

` cannot be exported: a C struct layout is used in the module that declares it

NL2747

The message of an error contains:

… must be a plain field: a C struct field is not `static`, optional or `readonly`

NL2748

The message of an error contains:

… cannot have an initializer: `new` zero-fills a C struct

NL2749

The message of an error contains:

` in itself: a C struct cannot contain itself by value

NL2750

The message of an error contains:

` cannot `extends` or `implements`: a C struct layout is its own fields

NL2751

The message of an error contains:

…: a C struct layout has fields only

NL2752

The message of an error contains:

…, which has no C layout: a field of a `declare class` is i32, i64, u8, u16, u32, u64, f32, f64, `CPtr`, `CPtr | null` or another `declare class`

NL2753

The message of an error contains:

`, a `declare class`: C reads and writes its fields, and typed JSON only those of a class Nish lays out

NL2754

The message of an error contains:

` is a `declare class` embedded in its object, which a spread cannot copy, so set each field by name

NL2755

The message of an error contains:

… has type `number`, whose width `--number-mode` chooses: a C struct field has one layout, so spell it `i32` or `f64`

NL2756

The message of an error contains:

…: a C struct layout stays in the module that declares it and is lent only to a `declare function`, so no header, `.d.ts` or N-API binding describes it

NL2757

The message of an error contains:

…` declares a C struct, and a wasm module a page loads links no C to lend it to: keep the layout and the `declare function` that takes it in a native build

nl2757_declare_class_emit_dts.ts (compiled with --emit-dts build/test/wordings/nl2757_declare_class_emit_dts.d.ts):

declare class Point {
  x: i32
}
export const f = (): i32 => {
  return 0
};
`declare class Point` declares a C struct, and a wasm module a page loads links no C to lend it to: keep the layout and the `declare function` that takes it in a native build

nl2757_declare_class_wasm32.ts (compiled with --target wasm32):

declare class Point {
  x: i32
}
export const f = (): i32 => {
  return 0
};
`declare class Point` declares a C struct, and a wasm module a page loads links no C to lend it to: keep the layout and the `declare function` that takes it in a native build

NL2758

The message of an error contains:

…` may be `null`: a nullable function reference is called only after a test such as `…

nl2758_fnref_nullable_call.ts:

// NL2758: a call through a nullable function reference no test has narrowed.
type F = (x: i32) => i32

export const run = (h: F | null): i32 => h(1)

export const main = (): i32 => run(null)
`h` may be `null`: a nullable function reference is called only after a test such as `h !== null` proves it holds a function

NL2759

The message of an error contains:

…` makes, where JavaScript reads it once at the call: read it into a `const` first and pass the `const`

nl2759_fnarg_reference_field.ts:

// NL2759: a field of function reference type passed for a function parameter
// would be read again at each call the callee makes; pass a `const` instead.
type F = (x: i32) => i32;

const inc = (x: i32): i32 => x + 1;

class Box {
  op: F;
  constructor(op: F) {
    this.op = op;
  }
}

const apply = (f: (x: i32) => i32, x: i32): i32 => f(x);

export const main = (): i32 => {
  const b = new Box(inc);
  return apply(b.op, 2);
};
`b.op` is read again at each call `apply` makes, where JavaScript reads it once at the call: read it into a `const` first and pass the `const`

NL2760

The message of an error contains:

…` calls it: pass it once a test such as `…

nl2760_fnarg_nullable_reference.ts:

// NL2760: a nullable function reference passed for a function parameter
// before a test has narrowed it.
type F = (x: i32) => i32;

const inc = (x: i32): i32 => x + 1;

const apply = (f: (x: i32) => i32, x: i32): i32 => f(x);

export const main = (): i32 => {
  let h: F | null = null;
  h = inc;
  return apply(h, 2);
};
`h` may be `null`, and `apply` calls it: pass it once a test such as `h !== null` has narrowed it

NL2761

The message of an error contains:

…`, and the call reads it after its arguments where JavaScript reads it before them: read it into a `const` first and pass the `const`

nl2761_fnarg_reference_assigned_later.ts:

// NL2761: a reference local passed for a function parameter that a later
// argument of the same call assigns.
type F = (x: i32) => i32;

const sq = (x: i32): i32 => x * x;
const inc = (x: i32): i32 => x + 1;

const apply = (f: (x: i32) => i32, x: i32): i32 => f(x);
const k = (f: F): i32 => f(1);

export const main = (): i32 => {
  let h: F = sq;
  return apply(h, k((h = inc)));
};
`h` is assigned by a later argument of `apply`, and the call reads it after its arguments where JavaScript reads it before them: read it into a `const` first and pass the `const`

NL2762

The message of an error contains:

…` cannot hold a function reference in an arm: an arm is not a place a reference's set is built from, so hold it in a class field or an array

nl2762_fnref_result_arm.ts:

// NL2762: a function reference directly in a `Result` arm, which is not a
// place a reference's set is built from.
type F = (x: i32) => i32;

const inc = (x: i32): i32 => x + 1;

const pick = (): Result<F, string> => Ok(inc);

export const main = (): i32 => {
  const r = pick();
  return r.ok ? 1 : 0;
};
`Result<(i32) => i32, string>` cannot hold a function reference in an arm: an arm is not a place a reference's set is built from, so hold it in a class field or an array

NL2763

The message of an error contains:

…: `readStruct` copies into the `declare class` its context names, so write `const s: Stat = readStruct(p)`

NL2764

The message of an error contains:

`readStruct(p)` needs a contextual `declare class` type to copy into (annotate it, e.g. `const s: Stat = readStruct(p)`)

NL2765

The message of an error contains:

`readStruct(p)` copies into a `declare class` layout, and …

NL2766

The message of an error contains:

…: the runtime moves what a root reaches between calls, so everything it reaches is a number, a `boolean`, an enum, a string, an array, a class or a discriminated union of classes — never a `Map`, a `Set`, an interface a class implements, a `Region`, a `Secret`, a `CPtr`, a handle class or a C struct layout

nl2766_root_storage_handle.ts:

// Root storage keeps only what the runtime can trace: a handle class owns a
// C resource the runtime cannot move.
declare function fopen(path: string, mode: string): CPtr | null

class File {
  readonly file: CPtr | null
  constructor(path: string) {
    this.file = fopen(path, "r")
  }
}

class Log {
  out: File

  constructor() {
    this.out = new File("/dev/null")
  }
}

const log = new Array<Log | null>(1)

export const open = (): boolean => log[0] !== null
Root storage `log` cannot keep Log.out (File): the runtime moves what a root reaches between calls, so everything it reaches is a number, a `boolean`, an enum, a string, an array, a class or a discriminated union of classes — never a `Map`, a `Set`, an interface a class implements, a `Region`, a `Secret`, a `CPtr`, a handle class or a C struct layout

nl2766_root_storage_interface.ts:

// Root storage keeps only what the runtime can trace: an interface a class
// implements is a pointer to an object of any of its classes, with no tag.
interface Shape {
  side: i32
}

class Square implements Shape {
  side: i32 = 2
}

class Scene {
  shapes: Shape[]

  constructor() {
    this.shapes = [new Square()]
  }
}

const scene = new Array<Scene | null>(1)

export const total = (): i32 => {
  const s = scene[0]
  return s === null ? 0 : s.shapes.length
}
Root storage `scene` cannot keep Scene.shapes[] (Shape): the runtime moves what a root reaches between calls, so everything it reaches is a number, a `boolean`, an enum, a string, an array, a class or a discriminated union of classes — never a `Map`, a `Set`, an interface a class implements, a `Region`, a `Secret`, a `CPtr`, a handle class or a C struct layout

nl2766_root_storage_layout.ts:

// Root storage keeps only what the runtime can trace: a C struct layout is
// C's to own.
declare class Point {
  x: i32
  y: i32
}

class Path {
  end: Point

  constructor() {
    this.end = new Point()
  }
}

const path = new Array<Path | null>(1)

export const has = (): boolean => path[0] !== null
Root storage `path` cannot keep Path.end (Point): the runtime moves what a root reaches between calls, so everything it reaches is a number, a `boolean`, an enum, a string, an array, a class or a discriminated union of classes — never a `Map`, a `Set`, an interface a class implements, a `Region`, a `Secret`, a `CPtr`, a handle class or a C struct layout

nl2766_root_storage_map.ts:

// Root storage keeps only what the runtime can trace: a `Map` it cannot.
class Cache {
  names: Map<string, i32>

  constructor() {
    this.names = new Map<string, i32>()
  }
}

const cache = new Array<Cache | null>(1)

export const size = (): i32 => {
  const c = cache[0]
  return c === null ? 0 : c.names.size
}
Root storage `cache` cannot keep Cache.names (Map<string, i32>): the runtime moves what a root reaches between calls, so everything it reaches is a number, a `boolean`, an enum, a string, an array, a class or a discriminated union of classes — never a `Map`, a `Set`, an interface a class implements, a `Region`, a `Secret`, a `CPtr`, a handle class or a C struct layout

nl2766_root_storage_set.ts:

// Root storage keeps only what the runtime can trace: a `Set` it cannot.
class Seen {
  ids: Set<i32>

  constructor() {
    this.ids = new Set<i32>()
  }
}

const seen = new Array<Seen | null>(1)

export const count = (): i32 => {
  const s = seen[0]
  return s === null ? 0 : s.ids.size
}
Root storage `seen` cannot keep Seen.ids (Set<i32>): the runtime moves what a root reaches between calls, so everything it reaches is a number, a `boolean`, an enum, a string, an array, a class or a discriminated union of classes — never a `Map`, a `Set`, an interface a class implements, a `Region`, a `Secret`, a `CPtr`, a handle class or a C struct layout

NL2767

The message of an error contains:

…` cannot be exported: the runtime moves the objects its elements point to between calls, so a page has no layout it could read; export a function that reads it instead

nl2767_root_storage_export.ts:

// Root storage moves its objects between calls, so a page cannot read it.
class World {
  tick: i32 = 0
}

export const world = new Array<World | null>(1)

export const tick = (): i32 => {
  const w = world[0]
  return w === null ? -1 : w.tick
}
Root storage `world` cannot be exported: the runtime moves the objects its elements point to between calls, so a page has no layout it could read; export a function that reads it instead

NL2768

The message of an error contains:

…` cannot be kept in a program that loads `nish/threads` or `nish:region`: a thread and a region allocate from arenas of their own, and a root could be left pointing into one that is freed; keep that work in a program of its own

nl2768_root_storage_region.ts:

// A region allocates from an arena of its own, which a root could outlive.
import { enter, free, region } from "nish:region"

class World {
  tick: i32 = 0
}

const world = new Array<World | null>(1)

const build = (): i32[] => [1, 2, 3]
const count = (xs: i32[]): i32 => xs.length

export const tick = (): i32 => {
  const r = region(build)
  const n = enter(r, count)
  free(r)
  const w = world[0]
  return w === null ? n : w.tick
}
Root storage `world` cannot be kept in a program that loads `nish/threads` or `nish:region`: a thread and a region allocate from arenas of their own, and a root could be left pointing into one that is freed; keep that work in a program of its own

nl2768_root_storage_threads.ts:

// A thread allocates from an arena of its own, which a root could outlive.
import { AtomicI32 } from "nish/threads"

class World {
  tick: i32 = 0
}

const world = new Array<World | null>(1)

export const tick = (): i32 => {
  const count = new AtomicI32(0)
  count.add(1)
  const w = world[0]
  return w === null ? count.load() : w.tick
}
Root storage `world` cannot be kept in a program that loads `nish/threads` or `nish:region`: a thread and a region allocate from arenas of their own, and a root could be left pointing into one that is freed; keep that work in a program of its own

NL2769

The message of an error contains:

…` cannot be kept in a build that writes a native shim (`--emit-napi`, `--emit-napi-async` or `--emit-header`): the shim releases the arena after a call that passes a string or an array, which would free what the root reaches; keep root storage in a native program or a page module

nl2769_root_storage_header.ts (compiled with --emit-header build/test/wordings/nl2769_root_storage_header.h):

// A native shim releases the arena after a call that passes a string, which
// would free the name a root keeps.
class Player {
  name: string = ""
}

const player = new Array<Player | null>(1)

export const join = (name: string): i32 => {
  const p = new Player()
  p.name = name
  player[0] = p
  return name.length
}

export const nameLength = (): i32 => {
  const p = player[0]
  return p === null ? 0 : p.name.length
}
Root storage `player` cannot be kept in a build that writes a native shim (`--emit-napi`, `--emit-napi-async` or `--emit-header`): the shim releases the arena after a call that passes a string or an array, which would free what the root reaches; keep root storage in a native program or a page module

nl2769_root_storage_napi.ts (compiled with --emit-napi build/test/wordings/nl2769_root_storage_napi.c):

// A native shim releases the arena after a call that passes a string, which
// would free the name a root keeps.
class Player {
  name: string = ""
}

const player = new Array<Player | null>(1)

export const join = (name: string): i32 => {
  const p = new Player()
  p.name = name
  player[0] = p
  return name.length
}

export const nameLength = (): i32 => {
  const p = player[0]
  return p === null ? 0 : p.name.length
}
Root storage `player` cannot be kept in a build that writes a native shim (`--emit-napi`, `--emit-napi-async` or `--emit-header`): the shim releases the arena after a call that passes a string or an array, which would free what the root reaches; keep root storage in a native program or a page module

nl2769_root_storage_napi_async.ts (compiled with --threads --emit-napi-async build/test/wordings/nl2769_root_storage_napi_async.c):

// A native shim releases the arena after a call that passes a string, which
// would free the name a root keeps.
class Player {
  name: string = ""
}

const player = new Array<Player | null>(1)

export const join = (name: string): i32 => {
  const p = new Player()
  p.name = name
  player[0] = p
  return name.length
}

export const nameLength = (): i32 => {
  const p = player[0]
  return p === null ? 0 : p.name.length
}
Root storage `player` cannot be kept in a build that writes a native shim (`--emit-napi`, `--emit-napi-async` or `--emit-header`): the shim releases the arena after a call that passes a string or an array, which would free what the root reaches; keep root storage in a native program or a page module

NL2770

The message of an error contains:

…` cannot be kept in a WASI build that writes a loader (`--emit-dts`): its loader releases the arena after every call, which would free what the root reaches; build the page module freestanding (`--target wasm32`) or as an `--emit-dts` compile for the host

nl2770_root_storage_wasi_dts.ts (compiled with --profile wasi --emit-dts build/test/wordings/nl2770_root_storage_wasi_dts.d.ts):

// A WASI build's loader releases the arena after every call, and only a page
// module emits the keep a loader would call instead.
class Player {
  score: i32 = 0
}

const player = new Array<Player | null>(1)

export const join = (score: i32): i32 => {
  const p = new Player()
  p.score = score
  player[0] = p
  return score
}
Root storage `player` cannot be kept in a WASI build that writes a loader (`--emit-dts`): its loader releases the arena after every call, which would free what the root reaches; build the page module freestanding (`--target wasm32`) or as an `--emit-dts` compile for the host

NL2771

The message of an error contains:

… parameters, and Nish passes at most two, the value and its key: JavaScript's third, the map itself, is the receiver the call already names, so read that instead

nl2771_map_foreach_arity.ts:

// NL2771: A `Map`'s `forEach` callback takes the value, or the value and its key;
// JavaScript's third parameter, the map, is refused.
export const main = (): i32 => {
  const m = new Map<string, i32>()
  m.set("a", 1)
  m.forEach((v, k, all) => console.log(`${k}${v}${all.size}`))
  return 0
}
The callback of `forEach` on a `Map` takes 3 parameters, and Nish passes at most two, the value and its key: JavaScript's third, the map itself, is the receiver the call already names, so read that instead

NL2772

The message of an error contains:

… parameters, and Nish passes one, the element: JavaScript passes it again as the second, and the set itself as the third, which is the receiver the call already names

nl2772_set_foreach_arity.ts:

// NL2772: A `Set`'s `forEach` callback takes the element alone; JavaScript passes it
// twice, and the set third, and Nish refuses the second parameter.
export const main = (): i32 => {
  const s = new Set<i32>()
  s.add(1)
  s.forEach((v, again) => console.log(v + again))
  return 0
}
The callback of `forEach` on a `Set` takes 2 parameters, and Nish passes one, the element: JavaScript passes it again as the second, and the set itself as the third, which is the receiver the call already names

NL2773

The message of an error contains:

Destructuring assignment is not supported; destructure in a `const` or `let` declaration, or assign each name

nl2773_destructure_assignment.ts:

// NL2773: a pattern on the left of `=` is not a declaration.
export const main = (): void => {
  let a = 1
  let b = 2
  ;[a, b] = [b, a]
  console.log(a + b)
}
Destructuring assignment is not supported; destructure in a `const` or `let` declaration, or assign each name

nl2773_destructure_assignment_object.ts:

// NL2773: an object pattern on the left of `=` is not a declaration either.
class Point {
  x: number
  constructor(x: number) {
    this.x = x
  }
}
export const main = (): void => {
  const p = new Point(1)
  let x = 0
  ;({ x } = p)
  console.log(x)
}
Destructuring assignment is not supported; destructure in a `const` or `let` declaration, or assign each name

nl2773_destructure_assignment_renamed.ts:

// NL2773: an object pattern on the left of `=`, renamed.
class Point {
  x: number
  constructor(x: number) {
    this.x = x
  }
}
export const main = (): void => {
  const p = new Point(1)
  let px = 0
  ;({ x: px } = p)
  console.log(px)
}
Destructuring assignment is not supported; destructure in a `const` or `let` declaration, or assign each name

NL2774

The message of an error contains:

A default in a destructuring pattern is not supported; every name is read, and one that is missing has no default to take

nl2774_destructure_default.ts:

// NL2774: a missing name has no default to take.
class Point {
  x: number
  y: number
  constructor(x: number, y: number) {
    this.x = x
    this.y = y
  }
}
export const main = (): void => {
  const { x = 0 } = new Point(1, 2)
  console.log(x)
}
A default in a destructuring pattern is not supported; every name is read, and one that is missing has no default to take

NL2775

The message of an error contains:

A rest element in a destructuring pattern is not supported; read each element you need by name

nl2775_destructure_rest.ts:

// NL2775: a rest element would copy the array's tail.
export const main = (): void => {
  const [a, ...rest] = [1, 2, 3]
  console.log(a + rest.length)
}
A rest element in a destructuring pattern is not supported; read each element you need by name

NL2776

The message of an error contains:

A nested destructuring pattern is not supported; bind the inner value to a name and destructure that

nl2776_destructure_nested.ts:

// NL2776: a pattern inside a pattern.
export const main = (): void => {
  const [[a]] = [[1]]
  console.log(a)
}
A nested destructuring pattern is not supported; bind the inner value to a name and destructure that

NL2777

The message of an error contains:

A destructuring key must be a field's name, not a computed or quoted key

nl2777_destructure_key.ts:

// NL2777: a key that is not a field's name.
class Point {
  x: number
  y: number
  constructor(x: number, y: number) {
    this.x = x
    this.y = y
  }
}
export const main = (): void => {
  const { "x": a } = new Point(1, 2)
  console.log(a)
}
A destructuring key must be a field's name, not a computed or quoted key

NL2778

The message of an error contains:

Cannot destructure field `…

nl2778_destructure_field.ts:

// NL2778: a field the receiver does not have.
class Point {
  x: number
  y: number
  constructor(x: number, y: number) {
    this.x = x
    this.y = y
  }
}
export const main = (): void => {
  const { z } = new Point(1, 2)
  console.log(z)
}
Cannot destructure field `z`: Point has no such field

NL2779

The message of an error contains:

…, which may be absent; narrow it first, as in `if (p !== null)`

nl2779_destructure_nullable.ts:

// NL2779: a value that may be absent.
class Point {
  x: number
  y: number
  constructor(x: number, y: number) {
    this.x = x
    this.y = y
  }
}
const pick = (p: Point | null): number => {
  const { x } = p
  return x
}
export const main = (): void => {
  console.log(pick(null))
}
Cannot destructure Point | null, which may be absent; narrow it first, as in `if (p !== null)`

NL2780

The message of an error contains:

A `Map` cannot be destructured; read each value with `get`

nl2780_destructure_map.ts:

// NL2780: a Map has no fields to read.
export const main = (): void => {
  const m = new Map<string, number>()
  const { size } = m
  console.log(size)
}
A `Map` cannot be destructured; read each value with `get`

NL2781

The message of an error contains:

A `string` cannot be destructured; read each character by its index

nl2781_destructure_string.ts:

// NL2781: a string's characters are read by index.
export const main = (): void => {
  const [c] = "ab"
  console.log(c)
}
A `string` cannot be destructured; read each character by its index

NL2782

The message of an error contains:

An object pattern reads a class or interface value, got …

nl2782_destructure_object_of_number.ts:

// NL2782: an object pattern reads a class or interface value.
export const main = (): void => {
  const n = 3
  const { x } = n
  console.log(x)
}
An object pattern reads a class or interface value, got i32

NL2783

The message of an error contains:

An array pattern reads an array, got …

nl2783_destructure_array_of_object.ts:

// NL2783: an array pattern reads an array.
class Point {
  x: number
  y: number
  constructor(x: number, y: number) {
    this.x = x
    this.y = y
  }
}
export const main = (): void => {
  const [a] = new Point(1, 2)
  console.log(a)
}
An array pattern reads an array, got Point

NL2784

The message of an error contains:

A destructuring declaration must be initialized

nl2784_destructure_uninitialized.ts:

// NL2784: a pattern has nothing to read without a value.
export const main = (): void => {
  let { x }
  console.log(x)
}
A destructuring declaration must be initialized

NL2785

The message of an error contains:

A destructuring `for...of` head walks an array only; bind the element to a name and destructure it in the body

nl2785_destructure_for_of_set.ts:

// NL2785: a destructuring head walks an array only.
export const main = (): void => {
  const s = new Set<string>()
  for (const [c] of s) {
    console.log(c)
  }
}
A destructuring `for...of` head walks an array only; bind the element to a name and destructure it in the body

NL2786

The message of an error contains:

…`: each member of a string enum has its own string, because the string is how JSON and `String` name the member

nl2786_string_enum_same_value.ts:

// Two members with one string would decode to the same member, so the second
// is refused: the string is how JSON and `String` name a member.
enum Role {
  Admin = "admin",
  Root = "admin",
}

export const test = (): i32 => 0;
Enum member `Role.Root` repeats the string of `Role.Admin`: each member of a string enum has its own string, because the string is how JSON and `String` name the member

NL2787

The message of an error contains:

…` is not valid UTF-8: a string enum's values are written to and matched against JSON, which is UTF-8

nl2787_string_enum_utf8.ts:

// A string enum's values are matched against JSON, so each is valid UTF-8.
enum Role {
  Bad = "\uD800",
}

export const test = (): i32 => 0;
Enum member `Role.Bad` is not valid UTF-8: a string enum's values are written to and matched against JSON, which is UTF-8

NL2788

The message of an error contains:

…`: an enum value is not its string, so compare it with a member, such as `…

nl2788_string_enum_compare_string.ts:

// A string enum is not a `string`: compare it with a member, not with text.
enum Role {
  Admin = "admin",
  Guest = "guest",
}

const isAdmin = (r: Role): boolean => r === "admin";

export const test = (): i32 => (isAdmin(Role.Admin) ? 1 : 0);
Cannot compare a `Role` with a string using `===`: an enum value is not its string, so compare it with a member, such as `Role.Admin`

NL2789

The message of an error contains:

…` is not a string: write `String(…

nl2789_string_enum_to_string.ts:

// A string enum does not convert to `string` on its own: `String(r)` does it.
enum Role {
  Admin = "admin",
}

export const test = (): i32 => {
  const r: Role = Role.Admin;
  const s: string = r;
  return s.length;
};
A `Role` is not a string: write `String(r)` for the member's string

NL2790

The message of an error contains:

…` is not one: write the member itself, such as `…

nl2790_string_enum_from_string.ts:

// A string does not become a member: write the member.
enum Role {
  Admin = "admin",
}

export const test = (): i32 => {
  const r: Role = "admin";
  return r === Role.Admin ? 1 : 0;
};
`"admin"` is a string, and a `Role` is not one: write the member itself, such as `Role.Admin`

NL2791

The message of an error contains:

…: a table holds u8, u16, u32, i32, i64 or f64, the numbers whose bytes are their value

NL2792

The message of an error contains:

`: its elements are constant data that nothing may write, and a `

NL2793

The message of an error contains:

` has no elements; a table holds at least one

NL2794

The message of an error contains:

… of constant table `

NL2795

The message of an error contains:

…` is a `declare class`, which C lays out with a pointer-wide slot per link, so it cannot implement `Compact`; drop the marker

nl2795_compact_declare.ts:

// WP48 rule 2: a `declare class` is a C struct and cannot implement `Compact`.
import { Compact } from "nish:compact"

declare class Clock implements Compact {
  t: i64
}

export const main = (): number => {
  const c = new Clock()
  return Number(c.t)
}
`Clock` is a `declare class`, which C lays out with a pointer-wide slot per link, so it cannot implement `Compact`; drop the marker

NL2796

The message of an error contains:

…`, whose links C would read as pointers; a `declare class` holds no object of a class that implements `Compact`

nl2796_compact_declare_field.ts:

// WP48 rule 2: a `declare class` holds no object of a compact class.
import { Compact } from "nish:compact"

class Cell implements Compact {
  value: i32 = 0
  next: Cell | null = null
}

declare class Holder {
  cell: Cell | null
}

export const main = (): number => {
  const h = new Holder()
  return h.cell === null ? 0 : 1
}
Field `cell` of `declare class` `Holder` holds the compact class `Cell`, whose links C would read as pointers; a `declare class` holds no object of a class that implements `Compact`

NL2797

The message of an error contains:

…`: the wipe reads a secret through a full pointer, so a class that holds one cannot implement `Compact`; keep the secret in a class of its own that does not

nl2797_compact_secret.ts:

// WP48 rule 3: a compact class holds no `Secret`.
import { Compact } from "nish:compact"
import { Secret, secret, wipe } from "nish:secret"

class Holder implements Compact {
  readonly k: Secret<u8[]>
  next: Holder | null = null
  constructor(seed: u8) {
    this.k = secret([seed, toU8(2)])
  }
  wipe(): void {
    wipe(this.k)
  }
}

export const main = (): i32 => {
  const h = new Holder(toU8(1))
  h.wipe()
  return 0
}
`Holder` implements `Compact` and holds the `Secret` field `k`: the wipe reads a secret through a full pointer, so a class that holds one cannot implement `Compact`; keep the secret in a class of its own that does not

NL2798

The message of an error contains:

…` is generic, and a generic class cannot implement `Compact` in this version: its links would stay full pointers; drop the type parameters or the marker

nl2798_compact_generic.ts:

// WP48: a generic class cannot implement `Compact` yet (NL2798), since its
// instantiations keep full-pointer links.
import { Compact } from "nish:compact"

class Gen<T> implements Compact {
  val: T
  next: Gen<T> | null = null
  constructor(val: T) {
    this.val = val
  }
}

export const main = (): i32 => {
  const g = new Gen<i32>(1)
  return g.val - 1
}
`Gen` is generic, and a generic class cannot implement `Compact` in this version: its links would stay full pointers; drop the type parameters or the marker

NL2799

The message of an error contains:

A `case` label of a `string` switch must be a string literal or a module constant (a label is compared without being run)

NL2800

The message of an error contains:

` is not compatible with its implementation: …

NL2801

The message of an error contains:

…): a call is held to the signatures written before the implementation, not to the implementation's own

NL2802

The message of an error contains:

` has overload signatures and cannot be described to C or Node

NL2803

The message of an error contains:

…can only be read as the left operand of `??`

NL2804

The message of an error contains:

…which has no `null` to stand for an omitted argument