Skip to content

[6.2] AST: Add a warning group for @_implementationOnly deprecation diagnostics #82081

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
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
1 change: 1 addition & 0 deletions include/swift/AST/DiagnosticGroups.def
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ GROUP(DynamicCallable, "dynamic-callable-requirements")
GROUP(ErrorInFutureSwiftVersion, "error-in-future-swift-version")
GROUP(ExistentialAny, "existential-any")
GROUP(ExistentialMemberAccess, "existential-member-access-limitations")
GROUP(ImplementationOnlyDeprecated, "implementation-only-deprecated")
GROUP(IsolatedConformances, "isolated-conformances")
GROUP(MemberImportVisibility, "member-import-visibility")
GROUP(MissingModuleOnKnownPaths, "missing-module-on-known-paths")
Expand Down
6 changes: 4 additions & 2 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1220,11 +1220,13 @@ ERROR(module_not_compiled_with_library_evolution,none,
"module %0 was not compiled with library evolution support; "
"using it means binary compatibility for %1 can't be guaranteed",
(Identifier, Identifier))
WARNING(implementation_only_requires_library_evolution,none,
GROUPED_WARNING(implementation_only_requires_library_evolution,
ImplementationOnlyDeprecated,none,
"using '@_implementationOnly' without enabling library evolution "
"for %0 may lead to instability during execution",
(Identifier))
WARNING(implementation_only_deprecated,none,
GROUPED_WARNING(implementation_only_deprecated,
ImplementationOnlyDeprecated,none,
"'@_implementationOnly' is deprecated, use 'internal import' instead",
())

Expand Down
6 changes: 6 additions & 0 deletions test/Sema/implementation-only-deprecated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
// RUN: -enable-library-evolution -swift-version 5 \
// RUN: -enable-upcoming-feature InternalImportsByDefault \
// RUN: -verify
// RUN: %target-swift-frontend -typecheck %t/Client.swift -I %t \
// RUN: -enable-library-evolution -swift-version 5 \
// RUN: -enable-upcoming-feature InternalImportsByDefault \
// RUN: -warnings-as-errors -Wwarning ImplementationOnlyDeprecated \
// RUN: -verify


// REQUIRES: swift_feature_InternalImportsByDefault

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

/// Build a client with and without library-evolution.
// RUN: %target-swift-frontend -typecheck %t/client-non-resilient.swift -I %t -verify
// RUN: %target-swift-frontend -typecheck %t/client-non-resilient.swift -I %t -verify \
// RUN: -warnings-as-errors -Wwarning ImplementationOnlyDeprecated
// RUN: %target-swift-frontend -typecheck %t/client-resilient.swift -I %t -verify \
// RUN: -enable-library-evolution -swift-version 5

Expand Down
23 changes: 23 additions & 0 deletions userdocs/diagnostics/implementation-only-deprecated.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Deprecated implementation-only imports (ImplementationOnlyDeprecated)

Warnings that identify `import` declarations with the `@_implementationOnly` attribute.

## Overview

When applied to `import` declarations, the compiler-internal attribute `@_implementationOnly` attempts prevents declarations from the imported module from being exposed in the ABI or public interface of the dependent module. This attribute became deprecated when support for access levels on `import` declarations was introduced with [SE-0409].

One reason `@_implementationOnly import` is deprecated is that it is unsafe when used in modules that are built _without_ [library evolution] enabled. For example, suppose the following code were part of a library named `Foo`:

```swift
// Library `Foo`
@_implementationOnly import ImplementationDetail

public struct Bar {
internal var x: Baz // defined in ImplementationDetail
}
```

If `Foo` is not compiled with library evolution, then the memory layout of values of `Bar` must be known at compile time in clients of `Foo`. However, the `@_implementationOnly import` of `ImplementationDetail` prevents clients from being able to look up `Baz` which is a type that contributes to the layout of `Foo`. As a result, the layout of `Foo` will be miscalculated resulting in undefined behavior.

[SE-0409]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0409-access-level-on-imports.md
[library evolution]: https://www.swift.org/blog/library-evolution/