8000 --stripe-packages: Hide PackageSpecs from top-level namespace by ngroman · Pull Request #5644 · sorbet/sorbet · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

--stripe-packages: Hide PackageSpecs from top-level namespace #5644

8000
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 14 commits into from
Apr 18, 2022

Conversation

ngroman
Copy link
Contributor
@ngroman ngroman commented Apr 15, 2022

Wrap all package spec declarations so that they don't resolve at the top level. This is done by prefixing them with a symbol <PackageSpecRegistry> during packaging.

Motivation

Referencing unimported packages should result in a constant resolution error. However, in cases, especially where the package had behavior at the top level, you could get confusing errors or no error at all. For example:

If you were to include Unimported::Package this would be a no-op and not result in any error.

Test plan

Added tests.

  • Test on Stripe's codebase

See included automated tests.

Comment on lines +5 to +6
BBB
# ^^^ error: Unable to resolve constant `BBB`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Prior to this change, this would have resolved. That's the whole point of this change.

Comment on lines +4 to +9
module ::Project
end

module ::Project::Foo
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't have a ton of context on the rbi gen stuf going on here, but this seems like effectively a noop. cc @aadi-stripe @jez

Copy link
Collaborator

Choose a reason for hiding this comment

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

Doesn't this just mean that because these symbols are not available at the top-level by default anymore (due to the re-namespacing), we end up generating the shims for them?

Copy link
Collaborator

Choose a reason for hiding this comment

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

seems fine to me, we can figure it out later if it's a problem

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, that's my understanding.

@ngroman ngroman marked this pull request as ready for review April 18, 2022 16:00
@ngroman ngroman requested a review from a team as a code owner April 18, 2022 16:00
@ngroman ngroman requested review from jez and removed request for a team April 18, 2022 16:00
@nroman-stripe
Copy link
Contributor

We have a policy of testing changes to Sorbet against Stripe's codebase before
merging them. I've kicked off a test run for the current PR. When the build
finishes, I'll share with you whether or how it failed. Thanks!

Stripe employees can see the build results here:

https://go/builds/bui_LX17eQnRsMp3z6
https://go/builds/bui_LX17CgP04zHcLm

@ngroman ngroman force-pushed the nr-package-toplevel branch from 6c27594 to 1b489c6 Compare April 18, 2022 16:16
@ngroman
Copy link
Contributor Author
ngroman commented Apr 18, 2022

I got this passing on Stripe's codebase with some minor changes:
See http://go/builds/bui_LX5BNbw7PBh6nj

@ngroman ngroman force-pushed the nr-package-toplevel branch from 1b489c6 to adcc35a Compare April 18, 2022 20:59
@ngroman
Copy link
Contributor Author
ngroman commented Apr 18, 2022

(Rebased)

@@ -1191,6 +1214,10 @@ struct PackageInfoFinder {

info->loc = ctx.locAt(classDef.loc);
info->declLoc_ = ctx.locAt(classDef.declLoc);

// `class Foo < PackageSpace` -> `class <PackageSpecRegistry>::Foo < PackageSpec`
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// `class Foo < PackageSpace` -> `class <PackageSpecRegistry>::Foo < PackageSpec`
// `class Foo < PackageSpec` -> `class <PackageSpecRegistry>::Foo < PackageSpec`

Comment on lines 627 to 628
auto *lastConstLit = &ast::cast_tree_nonnull<ast::UnresolvedConstantLit>(scope);
while (auto constLit = ast::cast_tree<ast::UnresolvedConstantLit>(lastConstLit->scope)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hm, why not just use pointers?

Suggested change
auto *lastConstLit = &ast::cast_tree_nonnull<ast::UnresolvedConstantLit>(scope);
while (auto constLit = ast::cast_tree<ast::UnresolvedConstantLit>(lastConstLit->scope)) {
auto lastConstLit = ast::cast_tree<ast::UnresolvedConstantLit>(scope);
ENFORCE(lastContLit != nullptr);
while (auto constLit = ast::cast_tree<ast::UnresolvedConstantLit>(lastConstLit->scope)) {

(i try to avoid the address-of & operator whenever possible)

Copy link
Collaborator
@jez jez left a comment

Choose a reason for hiding this comment

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

Couple small suggestions, but this looks great, thanks!

@ngroman ngroman enabled auto-merge (squash) April 18, 2022 21:33
@ngroman ngroman merged commit dd645af into master Apr 18, 2022
@ngroman ngroman deleted the nr-package-toplevel branch April 18, 2022 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4638
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0