From ca2b7e892cd4b8375877ed5337f2197479bf2a2d Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 2 Oct 2026 06:57:42 +0200 Subject: [PATCH 1/3] Type inline-record cases of untagged variants as objects in genType genType collapsed every single-field inline record of an untagged variant case to its field type, so `@unboxed type t = Array(array) | Record({x: int}) | Function(int => int)` was emitted as `number[] | number | ((_1:number) => number)`. At runtime the `Record` case is the object `{x}`: the compiled match dispatches on `typeof v === "object"` and reads `v.x`. A single field collapses to its type only when the whole declaration is unboxed (`type_representation = Unboxed`, as in `@unboxed type r2 = B({g: string})`, whose runtime value is the string). Unboxed.res gains `tLength`, called from index.tsx with `{x: 3}`; tsc rejected that call before this change (TS2353) and accepts it now. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Cristiano Calcagno --- compiler/gentype/translate_type_declarations.ml | 5 +---- .../typescript-react-example/src/Unboxed.gen.tsx | 7 ++++++- .../typescript-react-example/src/Unboxed.res | 8 ++++++++ .../typescript-react-example/src/Unboxed.res.js | 13 +++++++++++++ .../typescript-react-example/src/index.tsx | 3 +++ 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/compiler/gentype/translate_type_declarations.ml b/compiler/gentype/translate_type_declarations.ml index 542fb5b80a0..246c26e3ff3 100644 --- a/compiler/gentype/translate_type_declarations.ml +++ b/compiler/gentype/translate_type_declarations.ml @@ -282,10 +282,7 @@ let traslate_declaration_kind ~config ~loc ~output_file_relative ~resolver [ label_declarations |> translate_label_declarations ~inline:true - ~unboxed: - (type_representation = Unboxed - || Variant_runtime.constructor_is_untagged layout - position); + ~unboxed:(type_representation = Unboxed); ] in let arg_types = diff --git a/tests/gentype_tests/typescript-react-example/src/Unboxed.gen.tsx b/tests/gentype_tests/typescript-react-example/src/Unboxed.gen.tsx index 43d5469d46e..2460e456f27 100644 --- a/tests/gentype_tests/typescript-react-example/src/Unboxed.gen.tsx +++ b/tests/gentype_tests/typescript-react-example/src/Unboxed.gen.tsx @@ -13,7 +13,10 @@ export type r1 = number; export type r2 = string; -export type t = number[] | number | ((_1:number) => number); +export type t = + number[] + | { readonly x: number } + | ((_1:number) => number); export type tabIndex = "0" | "1" | 0; @@ -21,6 +24,8 @@ export const testV1: (x:v1) => v1 = UnboxedJS.testV1 as any; export const r2Test: (x:r2) => r2 = UnboxedJS.r2Test as any; +export const tLength: (v:t) => number = UnboxedJS.tLength as any; + export const a: tabIndex = UnboxedJS.a as any; export const b: tabIndex = UnboxedJS.b as any; diff --git a/tests/gentype_tests/typescript-react-example/src/Unboxed.res b/tests/gentype_tests/typescript-react-example/src/Unboxed.res index 3bb3cf77816..d2f305c4ee7 100644 --- a/tests/gentype_tests/typescript-react-example/src/Unboxed.res +++ b/tests/gentype_tests/typescript-react-example/src/Unboxed.res @@ -17,6 +17,14 @@ type r2 = B({g: string}) @genType @unboxed type t = Array(array) | Record({x: int}) | Function(int => int) +@genType +let tLength = (v: t) => + switch v { + | Array(a) => Array.length(a) + | Record({x}) => x + | Function(f) => f(0) + } + @genType type tabIndex = | @as("0") Activity | @as("1") UserKeyword | @as(0) NumZero diff --git a/tests/gentype_tests/typescript-react-example/src/Unboxed.res.js b/tests/gentype_tests/typescript-react-example/src/Unboxed.res.js index ef4ba819f0e..96d724a1a14 100644 --- a/tests/gentype_tests/typescript-react-example/src/Unboxed.res.js +++ b/tests/gentype_tests/typescript-react-example/src/Unboxed.res.js @@ -9,6 +9,18 @@ function r2Test(x) { return x; } +function tLength(v) { + if (Array.isArray(v)) { + return v.length; + } + switch (typeof v) { + case "object" : + return v.x; + case "function" : + return v(0); + } +} + let a = "0"; let b = "1"; @@ -18,6 +30,7 @@ let zero = 0; export { testV1, r2Test, + tLength, a, b, zero, diff --git a/tests/gentype_tests/typescript-react-example/src/index.tsx b/tests/gentype_tests/typescript-react-example/src/index.tsx index 053cee18a1e..110e7f2b566 100644 --- a/tests/gentype_tests/typescript-react-example/src/index.tsx +++ b/tests/gentype_tests/typescript-react-example/src/index.tsx @@ -10,6 +10,7 @@ import * as MyMath from "./MyMath.tsx"; import { Universe_Nested2_Nested3_nested3Value } from "./NestedModules.gen.tsx"; import * as Records from "./Records.gen.tsx"; import * as TestPromise from "./TestPromise.gen.tsx"; +import * as Unboxed from "./Unboxed.gen.tsx"; import * as Variants from "./Variants.gen.tsx"; import { printManyPayloads, @@ -129,6 +130,8 @@ export const make: React.FC = (_x: Props) => { return
; }; +consoleLog("Unboxed: tLength({ x: 3 }) =", Unboxed.tLength({ x: 3 })); + const signedMessage = DocStrings.signMessage("hello", 42); consoleLog("signedMessage:", signedMessage); From c1e58c9075b68091d30b23178d3e03b0a27c1021 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 2 Oct 2026 06:57:43 +0200 Subject: [PATCH 2/3] Remove Variant_runtime.constructor_is_untagged genType's inline-record translation was its only caller, and that caller now decides field collapsing from the declaration's type_representation alone. Nothing else in compiler, analysis, tools or tests references it, and Variant_runtime is a compiler-internal module. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Cristiano Calcagno --- compiler/ml/variant_runtime.ml | 6 ------ compiler/ml/variant_runtime.mli | 1 - tests/tests/src/variantsMatching.gen.tsx | 6 +++++- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/compiler/ml/variant_runtime.ml b/compiler/ml/variant_runtime.ml index d3d365c7292..88329ad8325 100644 --- a/compiler/ml/variant_runtime.ml +++ b/compiler/ml/variant_runtime.ml @@ -160,12 +160,6 @@ let constructor_tag layout position = | Constant tag -> tag.literal | Block (Tagged {tag} | Untagged {tag}) -> tag.literal -let constructor_is_untagged layout position = - match constructor_at layout position with - | Constant _ -> false - | Block (Tagged _) -> false - | Block (Untagged _) -> true - let representation ({variant; position} : constructor_reference) = constructor_at (get_layout variant) position diff --git a/compiler/ml/variant_runtime.mli b/compiler/ml/variant_runtime.mli index 8f310a47b2a..c49d40a4d4b 100644 --- a/compiler/ml/variant_runtime.mli +++ b/compiler/ml/variant_runtime.mli @@ -109,7 +109,6 @@ val matching_facts : layout -> matching_facts val configuration : layout -> configuration val constructor_at : layout -> int -> constructor_case val constructor_tag : layout -> int -> literal_tag option -val constructor_is_untagged : layout -> int -> bool val representation : constructor_reference -> constructor_case val length : layout -> int val num_constants : layout -> int diff --git a/tests/tests/src/variantsMatching.gen.tsx b/tests/tests/src/variantsMatching.gen.tsx index b1344de6950..67cb6a01f13 100644 --- a/tests/tests/src/variantsMatching.gen.tsx +++ b/tests/tests/src/variantsMatching.gen.tsx @@ -15,6 +15,10 @@ export type MyNullable_t = null | undefined | a; export type MyNullableExtended_t = null | undefined | "WhyNotAnotherOne" | a; -export type UntaggedWithBool_t = string | number | boolean | string; +export type UntaggedWithBool_t = + string + | number + | boolean + | { readonly name: string }; export type UntaggedWithTuple_t = string | [number, number, string]; From 1abeeb3bf1a157d6f793e0e2139eb735ea26670b Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 2 Oct 2026 08:31:54 +0200 Subject: [PATCH 3/3] Add CHANGELOG entry Co-Authored-By: Claude Opus 5.5 Signed-off-by: Cristiano Calcagno --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 426a36292bc..e8c492ac82b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ #### :bug: Bug fix - Make rewatch compile independent modules after an unrelated failure and recompile blocked dependents when a changed interface survives a failed implementation, including across full watcher rebuilds. https://github.com/rescript-lang/rescript/pull/8667 +- GenType: type inline-record cases of untagged variants as objects instead of their single field's type. https://github.com/rescript-lang/rescript/pull/8693 #### :memo: Documentation