Skip to content

Commit 8031c13

Browse files
authored
Fix Gentype: Support mutual recursion in types (#6528)
* In GenType Add mutual recursive test Signed-Off-By: Jono Prest <[email protected]> * In GenType add support for mutual recursion Signed-Off-By: Jono Prest <[email protected]> * Refactor GenType support mutual type recursion Signed-Off-By: Jono Prest <[email protected]> --------- Signed-off-by: Jono Prest <[email protected]>
1 parent 799eb1d commit 8031c13

File tree

5 files changed

+42
-35
lines changed

5 files changed

+42
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- Add missing check that the runtime representation of variants matches implementation and interface. https://github.com/rescript-lang/rescript-compiler/pull/6513/files
2222
- GenType: only export types (not values) from module types. https://github.com/rescript-lang/rescript-compiler/pull/6516
2323
- Fix compiler crash with unboxed variant definition with only 1 constructor. https://github.com/rescript-lang/rescript-compiler/pull/6523
24+
- GenType: support mutual recursive types inside modules. https://github.com/rescript-lang/rescript-compiler/pull/6528
2425

2526
# 11.0.0-rc.7
2627

jscomp/gentype/TranslateTypeDeclarations.ml

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,12 @@ let hasSomeGADTLeaf constructorDeclarations =
313313
(fun declaration -> declaration.Types.cd_res != None)
314314
constructorDeclarations
315315

316-
let translateTypeDeclaration ~config ~outputFileRelative ~recursive ~resolver
317-
~typeEnv
316+
let translateTypeDeclaration ~config ~outputFileRelative ~resolver ~typeEnv
318317
({typ_attributes; typ_id; typ_loc; typ_manifest; typ_params; typ_type} :
319318
Typedtree.type_declaration) : CodeItem.typeDeclaration list =
320319
if !Debug.translation then
321320
Log_.item "Translate Type Declaration %s\n" (typ_id |> Ident.name);
322-
if recursive then typeEnv |> TypeEnv.newType ~name:(typ_id |> Ident.name);
321+
323322
let typeName = Ident.name typ_id in
324323
let typeVars =
325324
typ_params
@@ -335,19 +334,27 @@ let translateTypeDeclaration ~config ~outputFileRelative ~recursive ~resolver
335334
| Type_abstract -> GeneralDeclaration typ_manifest
336335
| _ -> NoDeclaration
337336
in
338-
let res =
339-
declarationKind
340-
|> traslateDeclarationKind ~config ~loc:typ_loc ~outputFileRelative
341-
~resolver ~typeAttributes:typ_attributes ~typeEnv ~typeName ~typeVars
342-
in
343-
if not recursive then typeEnv |> TypeEnv.newType ~name:(typ_id |> Ident.name);
344-
res
337+
declarationKind
338+
|> traslateDeclarationKind ~config ~loc:typ_loc ~outputFileRelative ~resolver
339+
~typeAttributes:typ_attributes ~typeEnv ~typeName ~typeVars
340+
341+
let addTypeDeclarationIdToTypeEnv ~typeEnv
342+
({typ_id} : Typedtree.type_declaration) =
343+
typeEnv |> TypeEnv.newType ~name:(typ_id |> Ident.name)
345344

346345
let translateTypeDeclarations ~config ~outputFileRelative ~recursive ~resolver
347346
~typeEnv (typeDeclarations : Typedtree.type_declaration list) :
348347
CodeItem.typeDeclaration list =
348+
if recursive then
349+
typeDeclarations |> List.iter (addTypeDeclarationIdToTypeEnv ~typeEnv);
349350
typeDeclarations
350-
|> List.map
351-
(translateTypeDeclaration ~config ~outputFileRelative ~recursive
352-
~resolver ~typeEnv)
351+
|> List.map (fun typeDeclaration ->
352+
let res =
353+
typeDeclaration
354+
|> translateTypeDeclaration ~config ~outputFileRelative ~resolver
355+
~typeEnv
356+
in
357+
if not recursive then
358+
typeDeclaration |> addTypeDeclarationIdToTypeEnv ~typeEnv;
359+
res)
353360
|> List.concat

jscomp/gentype_tests/typescript-react-example/package-lock.json

Lines changed: 13 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/gentype_tests/typescript-react-example/src/NonrecursiveTypes.gen.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ export type notRecursive = number;
88
export type M_notRecursive = notRecursive[];
99

1010
export type M_recursive = { readonly self: M_recursive };
11+
12+
export type M_mutualRecursive = { readonly a: M_a };
13+
14+
export type M_a = { readonly self: M_mutualRecursive };

jscomp/gentype_tests/typescript-react-example/src/NonrecursiveTypes.res

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ module M = {
77

88
@genType
99
type rec recursive = {self: recursive}
10+
11+
@genType
12+
type rec mutualRecursive = {a: a}
13+
and a = {self: mutualRecursive}
1014
}

0 commit comments

Comments
 (0)