diff --git a/CHANGELOG.md b/CHANGELOG.md index a8e2b13544..be4f2516c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,10 @@ - experimental support of tagged template literals, eg ```sql`select * from ${table}```. https://github.com/rescript-lang/rescript-compiler/pull/6250 +#### :bug: Bug Fix + +- GenType: distinguish inline records from unary variant cases of object type. https://github.com/rescript-lang/rescript-compiler/pull/6586 + # 11.0.1 #### :bug: Bug Fix diff --git a/jscomp/gentype/EmitType.ml b/jscomp/gentype/EmitType.ml index 349d9215a0..faf600f530 100644 --- a/jscomp/gentype/EmitType.ml +++ b/jscomp/gentype/EmitType.ml @@ -203,7 +203,7 @@ let rec renderType ~(config : Config.t) ?(indent = None) ~typeNameIsInterface |> field ~name:(Runtime.jsVariantValue ~polymorphic); ] |> fields - | false, Object (_, flds) -> + | false, Object (Inline, flds) -> (* inlined record *) tagField :: flds |> fields | false, type_ -> diff --git a/jscomp/gentype/GenTypeCommon.ml b/jscomp/gentype/GenTypeCommon.ml index d7356e28c9..4581323aa2 100644 --- a/jscomp/gentype/GenTypeCommon.ml +++ b/jscomp/gentype/GenTypeCommon.ml @@ -62,7 +62,7 @@ let labelJSToString case = | IntLabel i -> i | StringLabel s -> s |> EmitText.quotes -type closedFlag = Open | Closed +type closedFlag = Open | Closed | Inline type type_ = | Array of type_ * mutable_ diff --git a/jscomp/gentype/TranslateTypeDeclarations.ml b/jscomp/gentype/TranslateTypeDeclarations.ml index 64e103fce7..07905ea092 100644 --- a/jscomp/gentype/TranslateTypeDeclarations.ml +++ b/jscomp/gentype/TranslateTypeDeclarations.ml @@ -85,7 +85,8 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver in {CodeItem.importTypes; exportFromTypeDeclaration} in - let translateLabelDeclarations ~recordRepresentation labelDeclarations = + let translateLabelDeclarations ?(inline = false) ~recordRepresentation + labelDeclarations = let isOptional l = match recordRepresentation with | Types.Record_optional_labels lbls -> List.mem l lbls @@ -131,7 +132,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver let type_ = match fields with | [field] when unboxedAnnotation -> field.type_ - | _ -> Object (Closed, fields) + | _ -> Object ((if inline then Inline else Closed), fields) in {TranslateTypeExprFromTypes.dependencies; type_} in @@ -244,7 +245,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver | Cstr_record labelDeclarations -> [ labelDeclarations - |> translateLabelDeclarations + |> translateLabelDeclarations ~inline:true ~recordRepresentation:Types.Record_regular; ] in diff --git a/jscomp/gentype_tests/typescript-react-example/package-lock.json b/jscomp/gentype_tests/typescript-react-example/package-lock.json index a322751c5e..5fdd739d29 100644 --- a/jscomp/gentype_tests/typescript-react-example/package-lock.json +++ b/jscomp/gentype_tests/typescript-react-example/package-lock.json @@ -24,7 +24,7 @@ }, "../../..": { "name": "rescript", - "version": "11.0.1", + "version": "11.1.0", "dev": true, "hasInstallScript": true, "license": "SEE LICENSE IN LICENSE", diff --git a/jscomp/gentype_tests/typescript-react-example/src/NestedVariants.gen.tsx b/jscomp/gentype_tests/typescript-react-example/src/NestedVariants.gen.tsx index 7775b7ea52..529c7e7fab 100644 --- a/jscomp/gentype_tests/typescript-react-example/src/NestedVariants.gen.tsx +++ b/jscomp/gentype_tests/typescript-react-example/src/NestedVariants.gen.tsx @@ -41,7 +41,8 @@ export type unboxedBinary = { TAG: "UB"; _0: typeD; _1: number }; export type inline = { TAG: "I"; readonly i: number; readonly j: number } | { TAG: "J"; readonly i: number; readonly j: number } - | { TAG: "K"; _0: number; _1: number }; + | { TAG: "K"; _0: number; _1: number } + | { TAG: "L"; _0: { readonly j: number; readonly i: number } }; export const makeVariant: () => typeL = NestedVariantsJS.makeVariant as any; diff --git a/jscomp/gentype_tests/typescript-react-example/src/NestedVariants.res b/jscomp/gentype_tests/typescript-react-example/src/NestedVariants.res index 8b06ebb5f4..cb32d311fc 100644 --- a/jscomp/gentype_tests/typescript-react-example/src/NestedVariants.res +++ b/jscomp/gentype_tests/typescript-react-example/src/NestedVariants.res @@ -61,6 +61,7 @@ type inline = | I({i: int, j: int}) | J({i: int, j: int}) | K(int, int) + | L({"i": int, "j": int}) @genType let testBoxedBinary = (_: boxedBinary) => 34 @@ -74,5 +75,6 @@ let testInline = x => | I(q) => I({...q, i: q.i}) | J(q) => J(q) | K(a, b) => K(b, a) + | L(q) => L(q) } diff --git a/jscomp/gentype_tests/typescript-react-example/src/NestedVariants.res.js b/jscomp/gentype_tests/typescript-react-example/src/NestedVariants.res.js index 96669d6b4e..81711cbe59 100644 --- a/jscomp/gentype_tests/typescript-react-example/src/NestedVariants.res.js +++ b/jscomp/gentype_tests/typescript-react-example/src/NestedVariants.res.js @@ -136,6 +136,11 @@ function testInline(x) { _0: x._1, _1: x._0 }; + case "L" : + return { + TAG: "L", + _0: x._0 + }; } }