Skip to content

[Serialization] Package imports #64047

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 7 commits into from
Mar 3, 2023
Merged

Conversation

xymus
Copy link
Contributor

@xymus xymus commented Mar 2, 2023

This PR completes the support for package-only imports, aka package imports as it's spelled in source code with package import SomePackageDependency. Package imports allow modules from a same package to have a shared dependency on a package-internal module, which enables package decls to refer to types from that module.

Previous PRs have added exportability checks for package imported decls (#64014 & #64033) and #63974 ensured that package imports are not printed in swiftinterfaces. This PR adds support for package imported dependencies in swiftmodule files.

The new behavior applies when loading a swiftmodule, let's say for module A, the compiler reads its package membership information to tell if the current client should load A's dependencies imported by a package import. Only clients belonging to the same package as A should load those dependencies, clients outside of the package likely don't have access to those dependencies. This applies only if A enabled library-evolution, without it all clients load the package dependencies.

See the following test for a typical use case of package import: test/Sema/package-only-references.swift

rdar://106164813

@xymus xymus requested review from elsh, artemcm and tshortli March 2, 2023 23:40
@xymus
Copy link
Contributor Author

xymus commented Mar 3, 2023

@swift-ci Please smoke test

1 similar comment
@xymus
Copy link
Contributor Author

xymus commented Mar 3, 2023

@swift-ci Please smoke test

Default = 1 << 1,
/// Include imports declared with `@_implementationOnly` or with an
/// access-level of `package` or less.
ImplementationOnly = 1 << 2,
/// Include imports of SPIs declared with `@_spi`.
SPIAccessControl = 1 << 3,
Copy link
Contributor

@elsh elsh Mar 3, 2023

Choose a reason for hiding this comment

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

Shouldn't @_spi import still be allowed? Otherwise might be too source breaking no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We still support @_spi(X) import but we didn't really need that information in the filter. That filter is mostly used to call getImportedModules which allows mostly to get all public imports, all implementation-only imports, etc. In comparison, the logics specific to @_spi use some other filter and then checks for the spiGroups field in the returned AttributedImport.

Copy link
Contributor

Choose a reason for hiding this comment

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

gotcha sounds good

xymus added 7 commits March 3, 2023 11:42
That filter wasn't needed in practice, we can remove it.
This general test applies to all access-level. Let's precise that it
tests the scenario where a client is outside of the package to preserve
the current behavior. A new tests will check the behavior of a package
dependency for clients in package.
…ackage

When loading a swiftmodule A, read its package information to tell if
the current client should load A's dependencies imports by a package
import. Only clients belonging to the same package as A should load
those dependencies, clients outside of the package likely don't have
access to those dependencies.

This is specific to swiftmodules as swiftinterfaces never display a
package-only import. Clients are unaware of package dependencies when
building against a swiftinterface.

rdar://106164813
@xymus xymus force-pushed the package-only-imports branch from 23fd0a7 to 4b14a8a Compare March 3, 2023 19:43
@xymus
Copy link
Contributor Author

xymus commented Mar 3, 2023

Adding the main module loading test that I forgot to commit before.

@swift-ci Please smoke test

@xymus xymus merged commit 644c943 into swiftlang:main Mar 3, 2023
@xymus xymus deleted the package-only-imports branch March 3, 2023 23:14
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.

2 participants