8000 Flatten virtual package module into a series of casgns by aadi-stripe · Pull Request #5599 · sorbet/sorbet · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Flatten virtual package module into a series of casgns #5599

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 3 commits into from
Apr 18, 2022
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
40 changes: 16 additions & 24 deletions packager/packager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1722,21 +1722,7 @@ class ImportTreeBuilder final {
return;
}

// Construct a module containing an assignment for an imported name:
// For name `A::B::C::D` imported from package `A::B` construct:
// module A::B::C
// D = <Mangled A::B>::A::B::C::D
// end
const auto &sourceMangledName = isFriendImport ? privatePkgMangledName : source.packageMangledName;
auto assignRhs = prependPackageScope(ctx, parts2literal(parts, core::LocOffsets::none()), sourceMangledName);

auto assign = ast::MK::Assign(core::LocOffsets::none(), name2Expr(parts.back(), ast::MK::EmptyTree()),
std::move(assignRhs));

ast::ClassDef::RHS_store rhs;
rhs.emplace_back(std::move(assign));

// Use the loc from the import in the module name and declaration to get the
// Use the loc from the import in the assignment name and declaration to get the
// following jump to definition behavior in the case of enumerated imports:
// imported constant: `Foo::Bar::Baz` from package `Foo::Bar`
// ^^^^^^^^ jump to the import statement
Expand All @@ -1748,16 +1734,22 @@ class ImportTreeBuilder final {
// imported constant: `Foo::Bar::Baz` from package `Foo::Bar`
// ^^^^^^^^ jump to top of package file of `Foo::Bar`
// ^^^ jump to actual definition of `Baz` class
const auto &assignLoc = getAssignLoc(source, packageLoc);

// Construct an assignment for an imported name:
// For name `A::B::C::D` imported from package `A::B` construct:
// A::B::C::D = <Mangled A::B>::A::B::C::D
const auto &sourceMangledName = isFriendImport ? privatePkgMangledName : source.packageMangledName;
auto assignRhs = prependPackageScope(ctx, parts2literal(parts, core::LocOffsets::none()), sourceMangledName);

// Ensure import's do not add duplicate loc-s in the test_module
const auto &moduleLoc = getModuleLoc(source, packageLoc);
auto scope = importScopeName(parts, assignLoc, moduleType);
auto assignLhs = name2Expr(parts.back(), std::move(scope));
auto assign = ast::MK::Assign(assignLoc, std::move(assignLhs), std::move(assignRhs));

auto mod = ast::MK::Module(core::LocOffsets::none(), moduleLoc, importModuleName(parts, moduleLoc, moduleType),
{}, std::move(rhs));
modRhs.emplace_back(std::move(mod));
modRhs.emplace_back(std::move(assign));
}

const core::LocOffsets getModuleLoc(ImportTree::Source &source, const core::Loc *packageLoc) {
const core::LocOffsets getAssignLoc(ImportTree::Source &source, const core::Loc *packageLoc) {
// normal or test import
if (source.isTestImport() || source.isNormalImport()) {
return source.isEnumeratedImport ? source.importLoc : core::LocOffsets::none();
Expand Down Expand Up @@ -1794,9 +1786,9 @@ class ImportTreeBuilder final {
}
}

// Name of the wrapper module for a given import, prefixed with the mangled name of the package.
ast::ExpressionPtr importModuleName(vector<core::NameRef> &parts, core::LocOffsets importLoc,
ModuleType moduleType) const {
// Name of the wrapper scope for a given import, prefixed with the mangled name of the package.
ast::ExpressionPtr importScopeName(vector<core::NameRef> &parts, core::LocOffsets importLoc,
ModuleType moduleType) const {
// Export mapping modules are built in the public (_Package) namespace, whereas other modules are built in
// the private (_Package_Private) namespace.
ast::ExpressionPtr name =
Expand Down
6 changes: 6 additions & 0 deletions test/cli/packager_suggest_nested/consumer_auth/__package.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true
# typed: strict

class Project::ConsumerAuth < PackageSpec
import Project::ConsumerAuth::Data
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true
# typed: strict

class Project::ConsumerAuth::Data < PackageSpec
export Project::ConsumerAuth::Data::IdentifierStruct
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true
# typed: strict

class Project::ConsumerAuth::Data
class IdentifierStruct
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true
# typed: strict

class Project::ConsumerAuth
class IdentifierType
end
end
1 change: 1 addition & 0 deletions test/cli/packager_suggest_nested/test.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No errors! Great job.
3 changes: 3 additions & 0 deletions test/cli/packager_suggest_nested/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

main/sorbet --silence-dev-message --stripe-packages test/cli/packager_suggest_nested/ 2>&1
Original file line number Diff line number Diff line change
Expand Up @ F438 @ -9,33 +9,21 @@ class <emptyTree><<C <root>>> < (::<todo sym>)
end

module <emptyTree>::<C <PackageRegistry>><<C <todo sym>>> < ()
module <emptyTree>::<C Package_Package_Private$1>::<C Package><<C <todo sym>>> < ()
<emptyTree>::<C Subpackage> = <emptyTree>::<C <PackageRegistry>>::<C Package_Subpackage_Package$1>::<C Package>::<C Subpackage>
end
<emptyTree>::<C Package_Package_Private$1>::<C Package>::<C Subpackage> = <emptyTree>::<C <PackageRegistry>>::<C Package_Subpackage_Package$1>::<C Package>::<C Subpackage>
end

module <emptyTree>::<C <PackageTests>><<C <todo sym>>> < ()
module <emptyTree>::<C Package_Package_Private$1>::<C Package><<C <todo sym>>> < ()
<emptyTree>::<C InnerClass> = <emptyTree>::<C <PackageRegistry>>::<C Package_Package_Private$1>::<C Package>::<C InnerClass>
end
<emptyTree>::<C Package_Package_Private$1>::<C Package>::<C InnerClass> = <emptyTree>::<C <PackageRegistry>>::<C Package_Package_Private$1>::<C Package>::<C InnerClass>

module <emptyTree>::<C Package_Package_Private$1>::<C Package><<C <todo sym>>> < ()
<emptyTree>::<C PackageClass> = <emptyTree>::<C <PackageRegistry>>::<C Package_Package_Private$1>::<C Package>::<C PackageClass>
end
<emptyTree>::<C Package_Package_Private$1>::<C Package>::<C PackageClass> = <emptyTree>::<C <PackageRegistry>>::<C Package_Package_Private$1>::<C Package>::<C PackageClass>

module <emptyTree>::<C Package_Package_Private$1>::<C Package><<C <todo sym>>> < ()
<emptyTree>::<C Subpackage> = <emptyTree>::<C <PackageRegistry>>::<C Package_Subpackage_Package$1>::<C Package>::<C Subpackage>
end
<emptyTree>::<C Package_Package_Private$1>::<C Package>::<C Subpackage> = <emptyTree>::<C <PackageRegistry>>::<C Package_Subpackage_Package$1>::<C Package>::<C Subpackage>
end

module <emptyTree>::<C <PackageRegistry>><<C <todo sym>>> < ()
module <emptyTree>::<C Package_Package$1>::<C Package><<C <todo sym>>> < ()
<emptyTree>::<C InnerClass> = <emptyTree>::<C <PackageRegistry>>::<C Package_Package_Private$1>::<C Package>::<C InnerClass>
end
<emptyTree>::<C Package_Package$1>::<C Package>::<C InnerClass> = <emptyTree>::<C <PackageRegistry>>::<C Package_Package_Private$1>::<C Package>::<C InnerClass>

module <emptyTree>::<C Package_Package$1>::<C Package><<C <todo sym>>> < ()
<emptyTree>::<C PackageClass> = <emptyTree>::<C <PackageRegistry>>::<C Package_Package_Private$1>::<C Package>::<C PackageClass>
end
<emptyTree>::<C Package_Package$1>::<C Package>::<C PackageClass> = <emptyTree>::<C <PackageRegistry>>::<C Package_Package_Private$1>::<C Package>::<C PackageClass>
end

module <emptyTree>::<C <PackageTests>><<C <todo sym>>> < ()
Expand Down Expand Up @@ -75,33 +63,21 @@ class <emptyTree><<C <root>>> < (::<todo sym>)
end

module <emptyTree>::<C <PackageRegistry>><<C <todo sym>>> < ()
module <emptyTree>::<C Package_Subpackage_Package_Private$1>::<C Package><<C <todo sym>>> < ()
<emptyTree>::<C InnerClass> = <emptyTree>::<C <PackageRegistry>>::<C Package_Package$1>::<C Package>::<C InnerClass>
end
<emptyTree>::<C Package_Subpackage_Package_Private$1>::<C Package>::<C InnerClass> = <emptyTree>::<C <PackageRegistry>>::<C Package_Package$1>::<C Package>::<C InnerClass>

module <emptyTree>::<C Package_Subpackage_Package_Private$1>::<C Package><<C <todo sym>>> < ()
<emptyTree>::<C PackageClass> = <emptyTree>::<C <PackageRegistry>>::<C Package_Package$1>::<C Package>::<C PackageClass>
end
<emptyTree>::<C Package_Subpackage_Package_Private$1>::<C Package>::<C PackageClass> = <emptyTree>::<C <PackageRegistry>>::<C Package_Package$1>::<C Package>::<C PackageClass>
end

module <emptyTree>::<C <PackageTests>><<C <todo sym>>> < ()
module <emptyTree>::<C Package_Subpackage_Package_Private$1>::<C Package><<C <todo sym>>> < ()
<emptyTree>::<C InnerClass> = <emptyTree>::<C <PackageRegistry>>::<C Package_Package$1>::<C Package>::<C InnerClass>
end
<emptyTree>::<C Package_Subpackage_Package_Private$1>::<C Package>::<C InnerClass> = <emptyTree>::<C <PackageRegistry>>::<C Package_Package$1>::<C Package>::<C InnerClass>

module <emptyTree>::<C Package_Subpackage_Package_Private$1>::<C Package><<C <todo sym>>> < ()
<emptyTree>::<C PackageClass> = <emptyTree>::<C <PackageRegistry>>::<C Package_Package$1>::<C Package>::<C PackageClass>
end
<emptyTree>::<C Package_Subpackage_Package_Private$1>::<C Package>::<C PackageClass> = <emptyTree>::<C <PackageRegistry>>::<C Package_Package$1>::<C Package>::<C PackageClass>

module <emptyTree>::<C Package_Subpackage_Package_Private$1>::<C Package>::<C Subpackage><<C <todo sym>>> < ()
<emptyTree>::<C SubpackageClass> = <emptyTree>::<C <PackageRegistry>>::<C Package_Subpackage_Package_Private$1>::<C Package>::<C Subpackage>::<C SubpackageClass>
end
<emptyTree>::<C Package_Subpackage_Package_Private$1>::<C Package>::<C Subpackage>::<C SubpackageClass> = <emptyTree>::<C <PackageRegistry>>::<C Package_Subpackage_Package_Private$1>::<C Package>::<C Subpackage>::<C SubpackageClass>
end

module <emptyTree>::<C <PackageRegistry>><<C <todo sym>>> < ()
module <emptyTree>::<C Package_Subpackage_Package$1>::<C Package>::<C Subpackage><<C <todo sym>>> < ()
<emptyTree>::<C SubpackageClass> = <emptyTree>::<C <PackageRegistry>>::<C Package_Subpackage_Package_Private$1>::<C Package>::<C Subpackage>::<C SubpackageClass>
end
<emptyTree>::<C Package_Subpackage_Package$1>::<C Package>::<C Subpackage>::<C SubpackageClass> = <emptyTree>::<C <PackageRegistry>>::<C Package_Subpackage_Package_Private$1>::<C Package>::<C Subpackage>::<C SubpackageClass>
end

module <emptyTree>::<C <PackageTests>><<C <todo sym>>> < ()
Expand Down
Loading
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
0