Skip to content

Fix Gentype: Support mutual recursion in types #6528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- Add missing check that the runtime representation of variants matches implementation and interface. https://github.com/rescript-lang/rescript-compiler/pull/6513/files
- GenType: only export types (not values) from module types. https://github.com/rescript-lang/rescript-compiler/pull/6516
- Fix compiler crash with unboxed variant definition with only 1 constructor. https://github.com/rescript-lang/rescript-compiler/pull/6523
- GenType: support mutual recursive types inside modules. https://github.com/rescript-lang/rescript-compiler/pull/6528

# 11.0.0-rc.7

Expand Down
33 changes: 20 additions & 13 deletions jscomp/gentype/TranslateTypeDeclarations.ml
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,12 @@ let hasSomeGADTLeaf constructorDeclarations =
(fun declaration -> declaration.Types.cd_res != None)
constructorDeclarations

let translateTypeDeclaration ~config ~outputFileRelative ~recursive ~resolver
~typeEnv
let translateTypeDeclaration ~config ~outputFileRelative ~resolver ~typeEnv
({typ_attributes; typ_id; typ_loc; typ_manifest; typ_params; typ_type} :
Typedtree.type_declaration) : CodeItem.typeDeclaration list =
if !Debug.translation then
Log_.item "Translate Type Declaration %s\n" (typ_id |> Ident.name);
if recursive then typeEnv |> TypeEnv.newType ~name:(typ_id |> Ident.name);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue here was that, a recursive type would not have access to any other mutually recursive types in the type declaration. So instead of checking inline here if it's recursive and adding it to the typeEnv. This happens a step above this function and adds all mutually recursive type ids to the typeEnv before proceeding.


let typeName = Ident.name typ_id in
let typeVars =
typ_params
Expand All @@ -335,19 +334,27 @@ let translateTypeDeclaration ~config ~outputFileRelative ~recursive ~resolver
| Type_abstract -> GeneralDeclaration typ_manifest
| _ -> NoDeclaration
in
let res =
declarationKind
|> traslateDeclarationKind ~config ~loc:typ_loc ~outputFileRelative
~resolver ~typeAttributes:typ_attributes ~typeEnv ~typeName ~typeVars
in
if not recursive then typeEnv |> TypeEnv.newType ~name:(typ_id |> Ident.name);
res
declarationKind
|> traslateDeclarationKind ~config ~loc:typ_loc ~outputFileRelative ~resolver
~typeAttributes:typ_attributes ~typeEnv ~typeName ~typeVars

let addTypeDeclarationIdToTypeEnv ~typeEnv
({typ_id} : Typedtree.type_declaration) =
typeEnv |> TypeEnv.newType ~name:(typ_id |> Ident.name)

let translateTypeDeclarations ~config ~outputFileRelative ~recursive ~resolver
~typeEnv (typeDeclarations : Typedtree.type_declaration list) :
CodeItem.typeDeclaration list =
if recursive then
typeDeclarations |> List.iter (addTypeDeclarationIdToTypeEnv ~typeEnv);
typeDeclarations
|> List.map
(translateTypeDeclaration ~config ~outputFileRelative ~recursive
~resolver ~typeEnv)
|> List.map (fun typeDeclaration ->
let res =
typeDeclaration
|> translateTypeDeclaration ~config ~outputFileRelative ~resolver
~typeEnv
in
if not recursive then
typeDeclaration |> addTypeDeclarationIdToTypeEnv ~typeEnv;
res)
|> List.concat
35 changes: 13 additions & 22 deletions jscomp/gentype_tests/typescript-react-example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ export type notRecursive = number;
export type M_notRecursive = notRecursive[];

export type M_recursive = { readonly self: M_recursive };

export type M_mutualRecursive = { readonly a: M_a };

export type M_a = { readonly self: M_mutualRecursive };
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ module M = {

@genType
type rec recursive = {self: recursive}

@genType
type rec mutualRecursive = {a: a}
and a = {self: mutualRecursive}
}