-
Notifications
You must be signed in to change notification settings - Fork 512
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
ruby27: Introduce numbered parameters support #3344
Conversation
5d5c331
to
6644684
Compare
6644684
to
4aed149
Compare
This looks really good. I'm done leaving comments for now. Feel free to re-request review when you want another look. |
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
…cks and lambdas Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
4aed149
to
54a07a2
Compare
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
// For each scope we save the highest numparam used (or 0) and all the nodes in the | ||
// scope referencing a numparam. | ||
// | ||
// The stack as 3 states: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// The stack as 3 states: | |
// The stack has 3 states: |
if (auto *lvar = parser::cast_node<parser::LVar>(decl.get())) { | ||
if (numparamNum(dctx, decl.get()) == num) { | ||
return MK::Local(lvar->loc, lvar->name); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be an ENFORCE if this cast ever fails?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(i.e., it's fine for it to fallback to the dummy MK::Local
if this search fails to find something so that sorbet doesn't crash, but would that be an expected situation or a bug in sorbet?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh i see, we might be making locals for like _1
and _2
only because we saw _3
in the body. makes sense, i see that the comment is getting at that now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
We have a policy of testing changes to Sorbet against Stripe's codebase before Stripe employees can see the build result here: |
Motivation
Ruby 27 introduced numbered parameters:
With numbered parameters, this snippet:
Is equivalent to
This pull request introduces full support for numbered parameters in Sorbet:
See the tests for more usage examples.
Implementation
During parsing blocks containing identifiers
_1
to_9
are created asNumBlock
instead ofBlock
and we keep a stack of the numbers used.During desugar, the
NumBlock
is translated into an ordinary block. Implicit parameters are added to the block args from_1
to the max number used.At this point, Sorbet can work its magic on the block and perform the typing.
Test plan
See included automated tests.