Repository navigation
TScert: print nested options by depth and polymorphic option code as overloads under -bs-emit-ts - #9213
TScert: print nested options by depth and polymorphic option code as overloads under -bs-emit-ts#9213cristianoc wants to merge 11 commits into
Conversation
Why:
A feasibility study for checking ReScript output with TScert, which reads
TypeScript whose top-level functions annotate their parameters and
result. The flag writes `.res.ts` (`.mts` for `.mjs`) in place of the
JavaScript output, annotates each top-level function from the typed tree,
and puts the module's type declarations at the top through genType's
translation. The annotations are keyed by `Ident.t` (name and stamp), so
one reaches the output only when the J IR prints the identifier the
typed tree bound; `RESCRIPT_EMIT_TS_DEBUG=1` reports, per printed
top-level function, whether its stamp and its parameters' stamps are the
typed tree's.
JS modified (top-level functions under -bs-emit-ts):
Example:
type point = {x: int, y: int, label: string}
let moveX = (p: point, dx: int) => {...p, x: p.x + dx}
Before (js):
function moveX(p, dx) {
return {
x: p.x + dx | 0,
y: p.y,
label: p.label
};
}
After (js): Rec.res.ts
export type point = {
readonly x: number;
readonly y: number;
readonly label: string
};
function moveX(p: point, dx: number): point {
return {
x: p.x + dx | 0,
y: p.y,
label: p.label
};
}
Review: Do the stamps survive to the printer? For every binding
`let f = (…) => …` at the top level or in a nested module, uncurried,
inlined into its callers, private or exported, the printed function and each
of its parameters carry the typed tree's stamp. A function the optimizer
synthesizes, such as `let partial = curried(1)`, has fresh parameters and
is printed unannotated.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Cristiano Calcagno <cristianoc@users.noreply.github.com>
…t-ts
Why:
TScert's source fragment admits `export function f(…)` and refuses the
trailing export list `export { f, … }`. Under -bs-emit-ts a top-level
function exported under its own name is printed with `export` in front
and leaves the list; other exports, such as a module record or an alias,
stay in it. The printer is assumed to print a top-level function under
its source name.
JS modified (exports under -bs-emit-ts):
Example:
let add = (a: int, b: int) => a + b
Before (js):
function add(a: number, b: number): number {
return a + b | 0;
}
export {
add,
}
After (js):
export function add(a: number, b: number): number {
return a + b | 0;
}
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Cristiano Calcagno <cristianoc@users.noreply.github.com>
Why:
A polymorphic function printed its type variables without declaring
them, which tsc rejects, and each parameter's variables were named on
their own, so distinct variables shared the name T1. Optional labelled
arguments printed as mandatory `undefined | T` parameters.
JS modified (-bs-emit-ts type parameters):
Example:
let id = x => x
let second = (_x: 'a, y: 'b): 'b => y
Before (js):
export function id(x: T1): T1 {
return x;
}
export function second(_x: T1, y: T1): T1 {
return y;
}
After (js):
export function id<T1>(x: T1): T1 {
return x;
}
export function second<a, b>(_x: a, y: b): b {
return y;
}
JS modified (-bs-emit-ts optional labelled arguments):
Example:
let scale = (x: int, ~by=2) => x * by
let offset = (~from=0, x: int) => x - from
Before (js):
export function scale(x: number, byOpt: (undefined | number)): number {
…
export function offset(fromOpt: (undefined | number), x: number): number {
After (js):
export function scale(x: number, byOpt?: number): number {
…
export function offset(fromOpt: (undefined | number), x: number): number {
Review: Why does `fromOpt` keep `undefined | number`? TypeScript requires
every parameter after an optional one to be optional, so `?` goes only on
the optional arguments after the last mandatory one; a ReScript call
passes `undefined` for an omitted argument in either position.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Cristiano Calcagno <ccrisccris@gmail.com>
The build test tests/build_tests/emit_ts compiles seven examples with -bs-emit-ts and type-checks the checked-in .res.ts output with tsc --strict against declarations for the runtime functions the output imports. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Cristiano Calcagno <ccrisccris@gmail.com>
Why:
`option<t>` printed `undefined | t`, but `Some(v)` is a marker object
`{BS_PRIVATE_NESTED_SOME_NONE: depth}` when `v` is `undefined` or a
marker, so the type was false for `option<option<int>>` and
`option<unit>`. At a type parameter it was false at a nested
instantiation, and the runtime declarations
`valFromOption<A>(x: A): A`, `getOr` and `map` were false there: at
`A` the marker type, `valFromOption(some(undefined))` returns
`undefined`. A verifier taking those declarations accepted programs
returning wrong values.
Representation modified (option under -bs-emit-ts):
Before (representation):
option<int> ↦ undefined | number
option<option<int>> ↦ undefined | (undefined | number)
option<'a> ↦ undefined | a
After (representation):
option<int> ↦ undefined | number
option<option<int>> ↦ undefined | (undefined | number) | { readonly BS_PRIVATE_NESTED_SOME_NONE: number }
option<'a> ↦ undefined | a, with a extends string | number | boolean | bigint
option<'a> in a type declaration ↦ undefined | a | { readonly BS_PRIVATE_NESTED_SOME_NONE: number }
rule: the marker arm is printed where Typeopt.type_cannot_contain_undefined fails for the payload, the test that selects Psome over Psome_not_nest
JS modified (-bs-emit-ts type parameters):
Example:
let getOr = (o, d) =>
switch o {
| Some(v) => v
| None => d
}
Before (js):
export function getOr<T1>(o: (undefined | T1), d: T1): T1 {
After (js):
export function getOr<T1 extends string | number | boolean | bigint>(o: (undefined | T1), d: T1): T1 {
JS modified (-bs-emit-ts nested options):
Example:
type box<'a> = {v: option<'a>}
let keep = (o: option<option<int>>) => o
Before (js):
export type box<a> = { readonly v: (undefined | a) };
export function keep(o: (undefined | (undefined | number))): (undefined | (undefined | number)) {
After (js):
export type box<a> = { readonly v: (undefined | (
a
| { readonly BS_PRIVATE_NESTED_SOME_NONE: number })) };
export function keep(o: (undefined | (
(undefined | number)
| { readonly BS_PRIVATE_NESTED_SOME_NONE: number }))): …
Review: Why bound a type parameter by the primitives, and not by every
type without the marker key? `some(x)` and `valFromOption(x)` return `x`
exactly when `x` is not `undefined` and its key
`BS_PRIVATE_NESTED_SOME_NONE` reads `undefined`. A primitive owns no
keys and its untampered prototypes hold none. A TypeScript record type
admits values owning further keys, and a weak type
`{ BS_PRIVATE_NESTED_SOME_NONE?: undefined }` rejects every record that
does not state that field, so no wider bound is both true and usable.
Review: What does the bound refuse? A call of such a function at a
non-primitive instantiation, `getOr` of an `option<point>` say, fails
tsc. A nested match such as `Some(Some(n))` fails tsc too: the marker
arm erases the depth, so no true declaration of `valFromOption` returns
the payload type there.
The runtime declarations in runtime-dts state the same bound, each line
audited against Primitive_option.res and Stdlib_Option.res, and
Stdlib_Array.reduce's callback receives the index and the array that
Array.prototype.reduce passes. The Nest example shows the marker arm and
the bound.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Cristiano Calcagno <ccrisccris@gmail.com>
…rrows
Why:
The marker arm reached only options genType translated from a type
expression as `option<t>`. A type declaration's manifest, translated
from its core type, and an optional argument of a function type, whose
option genType rebuilds, still printed `undefined | t` for a payload
that can hold a marker. An optional argument of a function type in a
type declaration also lost its option: the core type of `~x: int=?` is
its payload `int`, which genType read as the whole argument.
JS modified (-bs-emit-ts type declarations):
Example:
type opt<'a> = option<'a>
type callback = (~x: option<int>=?) => int
type cb2 = (~x: int=?) => int
Before (js):
export type opt<a> = (undefined | a);
export type callback = (x:(undefined | number)) => number;
export type cb2 = (x:number) => number;
After (js):
export type opt<a> = (undefined | (
a
| { readonly BS_PRIVATE_NESTED_SOME_NONE: number }));
export type callback = (x:(undefined | (
(undefined | number)
| { readonly BS_PRIVATE_NESTED_SOME_NONE: number }))) => number;
export type cb2 = (x:(undefined | number)) => number;
Review: Does this change genType's own output? Only where a type
declaration has an optional argument in a function type, which genType
printed without its `undefined`; the marker arm is off outside
-bs-emit-ts, and the gentype tests' outputs are unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Cristiano Calcagno <ccrisccris@gmail.com>
Why:
The printer drops a lone `return;`, so such a function prints an empty
body. TypeScript requires a `return` in a function whose declared result
type is other than `undefined`, `void` or `any`, and rejects the empty
body with TS2355.
JS modified (-bs-emit-ts body that is only return;):
Example:
let none = (): option<int> => None
Before (js):
export function none(): (undefined | number) {
}
After (js):
export function none(): (undefined | number) {
return;
}
Review: Does JavaScript output change? No: the statement is kept only
under -bs-emit-ts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Cristiano Calcagno <ccrisccris@gmail.com>
… unwrapping The emit_ts test writes what tsc --strict reports for the generated TypeScript to a checked-in tsc.txt, so an example the printed types refuse is checked in with its diagnostics. NestMatch unwraps nested options: a nested match and the generic getOr at 'a = option<int>, which the primitive bound on Primitive_option refuses. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Cristiano Calcagno <ccrisccris@gmail.com>
Why:
A nested match and a polymorphic function at a nested option failed
tsc under -bs-emit-ts. The marker arm printed every depth as `number`,
`{ readonly BS_PRIVATE_NESTED_SOME_NONE: number }`, so no true
declaration of `valFromOption` gave the payload back: at depth 0 it
returns `undefined`, at depth d > 0 the marker of depth d - 1. And a
function whose type parameter is an option's payload was bounded to the
primitives, so `getOr` at `'a = option<int>` (NestMatch.useNested) and
`Stdlib_Option.getOr` at that instance (Memo.cached) had no signature.
Representation modified (option under -bs-emit-ts):
Before (representation):
option<option<int>> ↦ undefined | number | { readonly BS_PRIVATE_NESTED_SOME_NONE: number }
option<unit> ↦ undefined | void | { readonly BS_PRIVATE_NESTED_SOME_NONE: number }
After (representation):
option<option<int>> ↦ undefined | number | { readonly BS_PRIVATE_NESTED_SOME_NONE: 0 }
option<option<option<int>>> ↦ undefined | number | { …: 0 } | { …: 1 }
option<unit> ↦ undefined | void | { readonly BS_PRIVATE_NESTED_SOME_NONE: 0 }
option<'a> in a type declaration, or at an abstract payload ↦ undefined | a | { …: number }
rule: Some(v) is the marker of depth 0 when v is undefined, of depth d + 1 when v is the marker of depth d, and v otherwise (Primitive_option.some); the depths are read off the payload's encoding, an option's, unit's, or a type that cannot hold undefined (no marker)
JS modified (-bs-emit-ts polymorphic option code):
Example:
let getOr = (o: option<'a>, d: 'a) =>
switch o {
| Some(v) => v
| None => d
}
Before (js):
export function getOr<a extends string | number | boolean | bigint>(o: (undefined | a), d: a): a {
After (js):
export function getOr<a extends string | number | boolean | bigint>(o: (undefined | a), d: a): a;
export function getOr<a extends string | number | boolean | bigint>(o: (undefined | (
(undefined | a)
| { readonly BS_PRIVATE_NESTED_SOME_NONE: 0 })), d: (undefined | a)): (undefined | a);
export function getOr<a extends string | number | boolean | bigint>(o: (undefined | (
(undefined | a)
| { readonly BS_PRIVATE_NESTED_SOME_NONE: 0 })), d: (undefined | a)): (undefined | a) {
JS modified (-bs-emit-ts a function returning undefined alone):
Example:
let useNestedFolded = () => getOr(Some(None), Some(3))
Before (js):
export function useNestedFolded(): (undefined | number) {
}
After (js):
export function useNestedFolded(): (undefined | number) {
return;
}
Review: Why is each overload true? It is the function's signature at the
instance `'a := option^k<a>`, k = 0 or 1, with `a` bounded to the primitives
as before, printed by the same rule: the function's code is the code of
every instance. The signature the body carries is the deepest instance's,
which no caller sees; tsc checks the body there.
Review: Why no conditional type for `Some<A>`? `Some<A>` at an unbounded
`A` is true only if no value but the runtime's markers owns the key
`BS_PRIVATE_NESTED_SOME_NONE`; a record type admits values owning further
keys. The overloads hold at primitive payloads with no assumption.
Review: What do the runtime declarations state? `Primitive_option.some` and
`valFromOption`, and `Stdlib_Option.getOr`, one signature per nesting of a
primitive payload, up to an option of an option; each line audited against
Primitive_option.res and Stdlib_Option.res. `Stdlib_Option.map` keeps its
primitive signature.
Review: Does this change genType's output? No: the marker hook answers "no
marker" outside -bs-emit-ts, and the gentype tests' outputs are unchanged.
The examples NestMatch (nested matches, `getOr` at a nested instance) and
Memo (`Stdlib_Option.getOr` at `'a = option<int>`) join the build test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Cristiano Calcagno <ccrisccris@gmail.com>
Why:
The depth rule read "cannot hold undefined" as "is no marker". A record
naming a field `BS_PRIVATE_NESTED_SOME_NONE` at run time satisfies the
first and owns the key `Primitive_option.some` reads: with
`x = {BS_PRIVATE_NESTED_SOME_NONE: 0, tag: "payload"}`, `Some(x)` at
`option<option<keyed>>` is the marker of depth 1, which the printed
`undefined | keyed | { …: 0 }` excludes (a review found it and ran it).
Representation modified (option under -bs-emit-ts, payloads that may own the key):
Before (representation):
option<option<keyed>> ↦ undefined | keyed | { readonly BS_PRIVATE_NESTED_SOME_NONE: 0 }
After (representation):
option<option<keyed>> ↦ undefined | keyed | { readonly BS_PRIVATE_NESTED_SOME_NONE: number }
rule: a payload is no marker only where it cannot hold undefined and may own no key BS_PRIVATE_NESTED_SOME_NONE: a record or an inline record naming that field at run time, an object type, and a variant case whose payload may, may own it
Review: Does the example fail tsc? `keepKeyed` passes the value through and
type-checks; wrapping such a payload calls `Primitive_option.some`, which no
runtime declaration states at a non-primitive payload, and fails tsc.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Cristiano Calcagno <ccrisccris@gmail.com>
Why:
Printing nested options by depth and polymorphic option code as
overloads gives NestMatch's nested match and its getOr at option<int>
a signature tsc accepts, so the four errors at the primitive bound are
gone.
Tests modified (tests/build_tests/emit_ts/tsc.txt):
empty
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ChangelogNo changelog entry. Semantic Diff3 commits · Full page Print nested options by depth and polymorphic option code as overloads7c88be8 · cristianoc + Claude · Nov 04, 2026, 06:01 UTC
Representation modified (option under -bs-emit-ts)
-option<option<int>> ↦ undefined | number | { readonly BS_PRIVATE_NESTED_SOME_NONE: number }
-option<unit> ↦ undefined | void | { readonly BS_PRIVATE_NESTED_SOME_NONE: number }
+option<option<int>> ↦ undefined | number | { readonly BS_PRIVATE_NESTED_SOME_NONE: 0 }
+option<option<option<int>>> ↦ undefined | number | { …: 0 } | { …: 1 }
+option<unit> ↦ undefined | void | { readonly BS_PRIVATE_NESTED_SOME_NONE: 0 }
+option<'a> in a type declaration, or at an abstract payload ↦ undefined | a | { …: number }
+rule: Some(v) is the marker of depth 0 when v is undefined, of depth d + 1 when v is the marker of depth d, and v otherwise (Primitive_option.some); the depths are read off the payload's encoding, an option's, unit's, or a type that cannot hold undefined (no marker)JS modified (-bs-emit-ts polymorphic option code) let getOr = (o: option<'a>, d: 'a) =>
switch o {
| Some(v) => v
| None => d
}
-export function getOr<a extends string | number | boolean | bigint>(o: (undefined | a), d: a): a {
+export function getOr<a extends string | number | boolean | bigint>(o: (undefined | a), d: a): a;
+export function getOr<a extends string | number | boolean | bigint>(o: (undefined | (
+ (undefined | a)
+ | { readonly BS_PRIVATE_NESTED_SOME_NONE: 0 })), d: (undefined | a)): (undefined | a);
+export function getOr<a extends string | number | boolean | bigint>(o: (undefined | (
+ (undefined | a)
+ | { readonly BS_PRIVATE_NESTED_SOME_NONE: 0 })), d: (undefined | a)): (undefined | a) {JS modified (-bs-emit-ts a function returning undefined alone) let useNestedFolded = () => getOr(Some(None), Some(3))
export function useNestedFolded(): (undefined | number) {
+ return;
}The examples NestMatch (nested matches, Review
Print a marker of any depth at a payload that may own the marker keybd978bd · cristianoc + Claude · Nov 04, 2026, 06:11 UTC
Representation modified (option under -bs-emit-ts, payloads that may own the key)
-option<option<keyed>> ↦ undefined | keyed | { readonly BS_PRIVATE_NESTED_SOME_NONE: 0 }
+option<option<keyed>> ↦ undefined | keyed | { readonly BS_PRIVATE_NESTED_SOME_NONE: number }
+rule: a payload is no marker only where it cannot hold undefined and may own no key BS_PRIVATE_NESTED_SOME_NONE: a record or an inline record naming that field at run time, an object type, and a variant case whose payload may, may own itReview
Record tsc's diagnostics for the emit_ts examples: none with overloadsdc30f4e · cristianoc + Claude · Nov 04, 2026, 08:16 UTC
Tests modified (tests/build_tests/emit_ts/tsc.txt) empty |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## exp/emit-ts-2 #9213 +/- ##
=================================================
- Coverage 81.76% 81.75% -0.01%
=================================================
Files 459 459
Lines 65480 65613 +133
=================================================
+ Hits 53539 53645 +106
- Misses 11941 11968 +27
🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
8a1bb7e to
dcba023
Compare
|
Folded into #9211. |
Stacked on #9211. It prints nested options and polymorphic option code under
-bs-emit-tsso that tsc and TScert accept the nested matches #9211 lists as open.What is printed:
option<t>printstbeside the marker of each depthSome(v)can take. For example,option<option<int>>printsundefined | number | { readonly BS_PRIVATE_NESTED_SOME_NONE: 0 }, and each further level adds the next depth.numberat abstract types, at a type declaration's parameter, and at a payload that may own the marker key. That covers a record with a field@as("BS_PRIVATE_NESTED_SOME_NONE"), an object type, and a variant whose payload may own the key.'a := option^k<a>, witha extends string | number | boolean | bigint. The implementation's signature is the deepest instance.Runtime declarations (
runtime-dts/):Primitive_option.someandvalFromOption, andStdlib_Option.getOr, are stated at depths 0–2. Each line is checked againstPrimitive_option.resorStdlib_Option.res, and none assumes anything about who owns the marker key.Tests:
NestMatch(inner,useNested) andMemo(cached).tsc.txtis now empty: the four errors at the primitive bound are gone.make test-gentypepasses with its outputs unchanged.TScert: with its generic-overloads change, all 10
emit_tsmodules verify end to end. TScert is not released yet, so a reviewer can't reproduce that run.Limit: polymorphic option code at a record payload, or nested deeper than the printed overloads, still fails tsc and is refused, never falsely accepted. Covering every payload needs either an assumption that only the runtime's markers own the key, or conditional types.
🤖 Generated with Claude Code