Skip to content

AST: Make the versioned variants of #if canImport() more reliable and consistent #60981

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

Conversation

tshortli
Copy link
Contributor

@tshortli tshortli commented Sep 7, 2022

Previously, when evaluating a #if canImport(Module, _version: 42) directive the compiler could diagnose and ignore the directive under the following conditions:

  • The associated binary module is corrupt/bogus.
  • The .tbd for an underlying Clang module is missing a current-version field.

This behavior is surprising when there is a valid .swiftinterface available and it only becomes apparent when building against an SDK with an old enough version of the module that the version in the .swiftinterface is too low, making this failure easy to miss. Some modules have different versioning systems for their Swift and Clang modules and it can also be intentional for a distributed binary .swiftmodule to contain bogus data (to force the compiler to recompile the .swiftinterface) so we need to handle both of these cases gracefully and predictably.

Now the compiler will enumerate all module loaders, ask each of them to attempt to parse the module version and then consistently use the parsed version from a single source. The .swiftinterface is preferred if present, then the binary module if present, and then finally the .tbd. The .tbd is still always used exclusively for the _underlyingVersion variant of canImport().

Resolves rdar://88723492

…ants of `#if canImport()`.

The ClangImporter tests for `#if canImport(..., _underlyingVersion: ...)` do not really depend on a Swift overlay being present for Simple.framework, so remove the overlay related `RUN` steps.
@tshortli
Copy link
Contributor Author

tshortli commented Sep 7, 2022

@swift-ci please test

Copy link
Contributor

@nkcsgexi nkcsgexi left a comment

Choose a reason for hiding this comment

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

Thank you!

…nd consistent.

Previously, when evaluating a `#if canImport(Module, _version: 42)` directive the compiler could diagnose and ignore the directive under the following conditions:

- The associated binary module is corrupt/bogus.
- The .tbd for an underlying Clang module is missing a current-version field.

This behavior is surprising when there is a valid `.swiftinterface` available and it only becomes apparent when building against an SDK with an old enough version of the module that the version in the `.swiftinterface` is too low, making this failure easy to miss. Some modules have different versioning systems for their Swift and Clang modules and it can also be intentional for a distributed binary `.swiftmodule` to contain bogus data (to force the compiler to recompile the `.swiftinterface`) so we need to handle both of these cases gracefully and predictably.

Now the compiler will enumerate all module loaders, ask each of them to attempt to parse the module version and then consistently use the parsed version from a single source. The `.swiftinterface` is preferred if present, then the binary module if present, and then finally the `.tbd`. The `.tbd` is still always used exclusively for the `_underlyingVersion` variant of `canImport()`.

Resolves rdar://88723492
@tshortli tshortli force-pushed the can-import-version-prefer-swiftinterface branch from c34784c to bbf189c Compare September 7, 2022 22:58
@tshortli
Copy link
Contributor Author

tshortli commented Sep 7, 2022

@swift-ci please smoke test and merge

Copy link
Contributor

@artemcm artemcm left a comment

Choose a reason for hiding this comment

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

Very nice!

@swift-ci swift-ci merged commit bdb58a3 into swiftlang:main Sep 8, 2022
@tshortli tshortli deleted the can-import-version-prefer-swiftinterface branch September 8, 2022 04:27
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.

4 participants