Skip to content

Commit 8b38d29

Browse files
committed
Sema: Limit error on LE importing non-LE modules to SDK modules
Limit reporting as an error imports of a non-library-evolution module from a library-evolution enabled module to sources that are part of the SDK. The error also requires having enabled `InternalImportsByDefault`. This should help prevent SDK breaking regressions while loosening the restriction elsewhere. Framework owners can enable `-library-level api` or `spi` to get this check as an error, along with more sanity checks. Other cases remain as a warning. We should look to silence it in some cases or offer a flag to do so. rdar://160414667
1 parent 3a7cf47 commit 8b38d29

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

lib/Sema/ImportResolution.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -908,21 +908,24 @@ void UnboundImport::validateResilience(NullablePtr<ModuleDecl> topLevelModule,
908908
targetName, importerName);
909909

910910
if (ctx.LangOpts.hasFeature(Feature::InternalImportsByDefault)) {
911-
// This will catch Swift 6 language mode as well where
912-
// it will be reported as an error.
913911
inFlight.fixItRemove(import.accessLevelRange);
914912
} else {
915913
SourceRange attrRange = import.accessLevelRange;
916914
if (attrRange.isValid())
917915
inFlight.fixItReplace(attrRange, "internal");
918916
else
919917
inFlight.fixItInsert(import.importLoc, "internal ");
920-
921-
// Downgrade to warning only in pre-Swift 6 mode and
922-
// when not using the experimental flag.
923-
if (!ctx.LangOpts.hasFeature(Feature::AccessLevelOnImport))
924-
inFlight.limitBehavior(DiagnosticBehavior::Warning);
925918
}
919+
920+
// Report as an error when InternalImportsByDefault is enabled or
921+
// the experimental AccessLevelOnImport (but not Swift 6), only in libraries
922+
// that are meant to be distributed.
923+
auto featureEnabled =
924+
ctx.LangOpts.hasFeature(Feature::AccessLevelOnImport) ||
925+
ctx.LangOpts.hasFeature(Feature::InternalImportsByDefault);
926+
if (!featureEnabled ||
927+
SF.getParentModule()->getLibraryLevel() < LibraryLevel::SPI)
928+
inFlight.limitBehavior(DiagnosticBehavior::Warning);
926929
}
927930

928931
void UnboundImport::diagnoseInvalidAttr(DeclAttrKind attrKind,

test/ModuleInterface/imports-swift7.swift

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,31 @@
55
// RUN: %target-swift-frontend -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -emit-module -o %t/nonResilient.swiftmodule %t/empty.swift
66
// RUN: %target-swift-frontend -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -emit-module -o %t/resilient.swiftmodule %t/empty.swift -enable-library-evolution
77

8-
/// Check errors.
9-
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %t/clientWithError.swift -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -I %t -verify -enable-upcoming-feature InternalImportsByDefault
8+
/// Warning vs error on public import of a non-LE module from LE.
9+
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) \
10+
// RUN: %t/clientWithError.swift -I %t -verify \
11+
// RUN: -disable-implicit-concurrency-module-import \
12+
// RUN: -disable-implicit-string-processing-module-import \
13+
// RUN: -enable-upcoming-feature InternalImportsByDefault \
14+
// RUN: -verify-additional-prefix library-level-private-
15+
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) \
16+
// RUN: %t/clientWithError.swift -I %t -verify \
17+
// RUN: -disable-implicit-concurrency-module-import \
18+
// RUN: -disable-implicit-string-processing-module-import \
19+
// RUN: -enable-upcoming-feature InternalImportsByDefault \
20+
// RUN: -verify-additional-prefix library-level-private- -library-level ipi
21+
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) \
22+
// RUN: %t/clientWithError.swift -I %t -verify \
23+
// RUN: -disable-implicit-concurrency-module-import \
24+
// RUN: -disable-implicit-string-processing-module-import \
25+
// RUN: -enable-upcoming-feature InternalImportsByDefault \
26+
// RUN: -verify-additional-prefix library-level-public- -library-level spi
27+
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) \
28+
// RUN: %t/clientWithError.swift -I %t -verify \
29+
// RUN: -disable-implicit-concurrency-module-import \
30+
// RUN: -disable-implicit-string-processing-module-import \
31+
// RUN: -enable-upcoming-feature InternalImportsByDefault \
32+
// RUN: -verify-additional-prefix library-level-public- -library-level api
1033

1134
/// Check Swift 7 imports printed in swiftinterface from 2 source files.
1235
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %t/main.swift %t/main-other.swift -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -I %S/Inputs/imports-clang-modules/ -I %t -verify -enable-upcoming-feature InternalImportsByDefault
@@ -38,8 +61,11 @@ public import D // expected-warning {{public import of 'D' was not used in publi
3861
public import NotSoSecret // expected-warning {{'NotSoSecret' inconsistently imported as implementation-only}}
3962
// expected-warning @-1 {{public import of 'NotSoSecret' was not used in public declarations or inlinable code}}
4063
@_implementationOnly import NotSoSecret2 // expected-note {{imported as implementation-only here}}
64+
4165
//--- clientWithError.swift
42-
@_exported public import nonResilient // expected-error {{module 'nonResilient' was not compiled with library evolution support; using it means binary compatibility for 'clientWithError' can't be guaranteed}}
66+
@_exported public import nonResilient
67+
// expected-library-level-public-error @-1 {{module 'nonResilient' was not compiled with library evolution support; using it means binary compatibility for 'clientWithError' can't be guaranteed}}
68+
// expected-library-level-private-warning @-2 {{module 'nonResilient' was not compiled with library evolution support; using it means binary compatibility for 'clientWithError' can't be guaranteed}}
4369

4470
// CHECK-7-NOT: import
4571
// CHECK-7: {{^}}public import A{{$}}

test/Sema/access-level-and-non-resilient-import.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,25 @@
1313
// RUN: %target-swift-frontend -emit-module %t/PrivateLib.swift -o %t
1414

1515
/// A resilient client will error on public imports.
16-
/// Keep AccessLevelOnImport to get the error from Swift 6 in Swift 5.
16+
/// Use AccessLevelOnImport or InternalImportsByDefault to get the error
17+
/// from Swift 7 in Swift 5.
1718
// RUN: %target-swift-frontend -typecheck %t/Client_Swift5.swift -I %t \
1819
// RUN: -enable-library-evolution -swift-version 5 \
1920
// RUN: -enable-experimental-feature AccessLevelOnImport -verify \
20-
// RUN: -package-name pkg
21-
// RUN: %target-swift-frontend -typecheck %t/Client_Swift6.swift -I %t \
21+
// RUN: -package-name pkg -library-level api
22+
// RUN: %target-swift-frontend -typecheck %t/Client_Swift7.swift -I %t \
2223
// RUN: -enable-library-evolution \
2324
// RUN: -enable-upcoming-feature InternalImportsByDefault \
24-
// RUN: -verify -package-name pkg
25+
// RUN: -verify -package-name pkg -library-level api
2526

2627
/// A non-resilient client doesn't complain.
2728
// RUN: %target-swift-frontend -typecheck %t/Client_Swift5.swift -I %t \
2829
// RUN: -swift-version 5 \
2930
// RUN: -enable-experimental-feature AccessLevelOnImport \
30-
// RUN: -package-name pkg
31-
// RUN: %target-swift-frontend -typecheck %t/Client_Swift6.swift -I %t \
31+
// RUN: -package-name pkg -library-level api
32+
// RUN: %target-swift-frontend -typecheck %t/Client_Swift7.swift -I %t \
3233
// RUN: -enable-upcoming-feature InternalImportsByDefault \
33-
// RUN: -package-name pkg
34+
// RUN: -package-name pkg -library-level api
3435

3536
// REQUIRES: swift_feature_AccessLevelOnImport
3637
// REQUIRES: swift_feature_InternalImportsByDefault
@@ -53,10 +54,10 @@ internal import InternalLib
5354
fileprivate import FileprivateLib
5455
private import PrivateLib
5556

56-
//--- Client_Swift6.swift
57+
//--- Client_Swift7.swift
5758

5859
import DefaultLib
59-
public import PublicLib // expected-error {{module 'PublicLib' was not compiled with library evolution support; using it means binary compatibility for 'Client_Swift6' can't be guaranteed}} {{1-8=}}
60+
public import PublicLib // expected-error {{module 'PublicLib' was not compiled with library evolution support; using it means binary compatibility for 'Client_Swift7' can't be guaranteed}} {{1-8=}}
6061
// expected-warning @-1 {{public import of 'PublicLib' was not used in public declarations or inlinable code}}
6162
package import PackageLib
6263
// expected-warning @-1 {{package import of 'PackageLib' was not used in package declarations}}

0 commit comments

Comments
 (0)