Skip to content

Normalize and simplify types before printing them as RBS #479

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 4 commits into from
Jun 9, 2025

Conversation

Morriar
Copy link
Collaborator

@Morriar Morriar commented Jun 5, 2025

This will avoid some translation bug where nested nilables such as T.nilable(T.nilable(X)) get incorrectly translated as X??.

This PR introduce two important concept:

  1. Type normalization: transforms a type into a canonical version that is easy to analyze.

     # Returns a normalized version of the type.
     #
     # Normalized types are meant to be easier to process, not to read.
     # For example, `T.any(TrueClass, FalseClass)` instead of `T::Boolean` or
     # `T.any(String, NilClass)` instead of `T.nilable(String)`.
     #
     # This is the inverse of `#simplify`.
     #
     # @abstract
     #: -> Type
     def normalize; end
  2. Type simplification: transforms a type in an easy to read version.

     # Returns a simplified version of the type.
     #
     # Simplified types are meant to be easier to read, not to process.
     # For example, `T::Boolean` instead of `T.any(TrueClass, FalseClass)` or
     # `T.nilable(String)` instead of `T.any(String, NilClass)`.
     #
     # This is the inverse of `#normalize`.
     #
     # @abstract
     #: -> Type
     def simplify; end

We need type normalization to implement type simplification. Consider this example:

T.any(T.any(A, TrueClass, T.any(B, FalseClass, T.any(C, NilClass)))

The normalized version of this type is

T.any(A, TrueClass, B, FalseClass, C, NilClass)

Which can then be simplified into

T.nilable(A, B, C, T::Boolean)

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
@Morriar Morriar self-assigned this Jun 5, 2025
@Morriar Morriar requested a review from a team as a code owner June 5, 2025 16:02
@Morriar Morriar added feature New feature bugfix Fix a bug labels Jun 5, 2025
Morriar added 2 commits June 6, 2025 10:35
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
@Morriar Morriar force-pushed the at-flatten-types branch from bebe3c8 to b4654e4 Compare June 6, 2025 14:35
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
@Morriar Morriar force-pushed the at-flatten-types branch from b4654e4 to 7e06df4 Compare June 6, 2025 14:38
@Morriar Morriar requested a review from paracycle June 6, 2025 14:38
jesse-shopify added a commit to Shopify/spoom that referenced this pull request Jun 6, 2025
@Morriar Morriar merged commit f450c86 into main Jun 9, 2025
8 checks passed
@Morriar Morriar deleted the at-flatten-types branch June 9, 2025 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix Fix a bug feature New feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants