Skip to content
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

Fix Gentype: Support mutual recursion in types #6528

Merged

Conversation

JonoPrest
Copy link
Contributor

Hi there 👋🏼

This is my first PR into the rescript compiler. I've been a rescript user for around 2 years and absolutely love the language and the ecosystem. I would like to contribute more where I can. Please let me know if the contribution format is suitable. I've read the contribution guidlines.

I stumbled upon the issue I made at #6524 and thought I would take a stab at fixing it.

Changes:

  • adds a test to validate mutually recursive types produce valid typescript. (This test fails before support was added)
  • add a function that adds all type ids in a recursive type declaration to the type environment before translating type declarations

Closes #6526

let translateTypeDeclaration ~config ~outputFileRelative ~recursive ~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.

Copy link
Collaborator

@cristianoc cristianoc left a comment

Choose a reason for hiding this comment

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

Looks great.
Would you update the changelog too?

@zth
Copy link
Collaborator

zth commented Dec 13, 2023

Hi there 👋🏼

This is my first PR into the rescript compiler. I've been a rescript user for around 2 years and absolutely love the language and the ecosystem. I would like to contribute more where I can. Please let me know if the contribution format is suitable. I've read the contribution guidlines.

I stumbled upon the issue I made at #6524 and thought I would take a stab at fixing it.

Changes:

  • adds a test to validate mutually recursive types produce valid typescript. (This test fails before support was added)
  • add a function that adds all type ids in a recursive type declaration to the type environment before translating type declarations

Closes #6526

Awesome to see your first contribution, great work! 👏

@JonoPrest JonoPrest force-pushed the fix-gentyp-mutual-recursion branch from a2563b1 to 3fac89e Compare December 13, 2023 20:29
@JonoPrest
Copy link
Contributor Author

Looks great. Would you update the changelog too?

Thanks @cristianoc I have updated the changelog and ammended to the commit.

@JonoPrest
Copy link
Contributor Author

Awesome to see your first contribution, great work! 👏

Thanks very much!

@@ -98,13 +98,17 @@ and translateSignatureItem ~config ~outputFileRelative ~resolver ~typeEnv
signatureItem : Translation.t =
match signatureItem with
| {Typedtree.sig_desc = Typedtree.Tsig_type (recFlag, typeDeclarations)} ->
let recursive = recFlag = Recursive in
Copy link
Collaborator

Choose a reason for hiding this comment

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

Right now the treatments of recursive and non-recursive cases are quite far apart, separated by 2 function calls.
Possible refactor: do this work inside translateTypeDeclarations.
Also, the non-recursive case could also be moved inside translateTypeDeclarations and the ~recursive parameter could then be omitted from translateTypeDeclaration.

@JonoPrest what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That sounds good @cristianoc.

I'll update it 👍🏼

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cristianoc I've added a refactor as suggested. I think it's much cleaner 👍🏼

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sweet. Thanks for the great contribution.

@cristianoc cristianoc merged commit 8031c13 into rescript-lang:master Dec 14, 2023
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GenType: mutually recursive types inside module fails to reference type by flattened alias
3 participants