Skip to content

Commit 7b1bc00

Browse files
committed
[Sema] Update diagnostic about public imports of non-resilient modules
The compiler reports public imports of non-resilient modules from a resilient module. Make sure that when imports default to internal, which also implies Swift 6, this is treated as an error and the fixit simply deletes the `public` keyword. We keep using the AccessLevelOnImport flag only to report these as errors in Swift 5 for early adopters.
1 parent cbe5c38 commit 7b1bc00

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

lib/Sema/ImportResolution.cpp

+10-3
Original file line numberDiff line numberDiff line change
@@ -821,14 +821,21 @@ void UnboundImport::validateResilience(NullablePtr<ModuleDecl> topLevelModule,
821821
topLevelModule.get()->getName(),
822822
SF.getParentModule()->getName());
823823

824-
if (ctx.LangOpts.hasFeature(Feature::AccessLevelOnImport)) {
824+
if (ctx.LangOpts.hasFeature(Feature::InternalImportsByDefault)) {
825+
// This will catch Swift 6 language mode as well where
826+
// it will be reported as an error.
827+
inFlight.fixItRemove(import.accessLevelRange);
828+
} else {
825829
SourceRange attrRange = import.accessLevelRange;
826830
if (attrRange.isValid())
827831
inFlight.fixItReplace(attrRange, "internal");
828832
else
829833
inFlight.fixItInsert(import.importLoc, "internal ");
830-
} else {
831-
inFlight.limitBehavior(DiagnosticBehavior::Warning);
834+
835+
// Downgrade to warning only in pre-Swift 6 mode and
836+
// when not using the experimental flag.
837+
if (!ctx.LangOpts.hasFeature(Feature::AccessLevelOnImport))
838+
inFlight.limitBehavior(DiagnosticBehavior::Warning);
832839
}
833840
}
834841

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

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/// a non-resilient module.
33

44
// RUN: %empty-directory(%t)
5-
// RUN: split-file %s %t
5+
// RUN: split-file --leading-lines %s %t
66

77
// REQUIRES: asserts
88

@@ -15,14 +15,18 @@
1515
// RUN: %target-swift-frontend -emit-module %t/PrivateLib.swift -o %t
1616

1717
/// A resilient client will error on public imports.
18+
/// Keep AccessLevelOnImport to get the error from Swift 6 in Swift 5.
1819
// RUN: %target-swift-frontend -typecheck %t/Client_Swift5.swift -I %t \
1920
// RUN: -enable-library-evolution -swift-version 5 \
2021
// RUN: -enable-experimental-feature AccessLevelOnImport -verify \
2122
// RUN: -package-name pkg
2223
// RUN: %target-swift-frontend -typecheck %t/Client_Swift6.swift -I %t \
23-
// RUN: -enable-library-evolution -swift-version 6 \
24-
// RUN: -enable-experimental-feature AccessLevelOnImport -verify \
24+
// RUN: -enable-library-evolution -swift-version 6 -verify \
2525
// RUN: -package-name pkg
26+
// RUN: %target-swift-frontend -typecheck %t/Client_Swift6.swift -I %t \
27+
// RUN: -enable-library-evolution \
28+
// RUN: -enable-upcoming-feature InternalImportsByDefault \
29+
// RUN: -verify -package-name pkg
2630

2731
/// A non-resilient client doesn't complain.
2832
// RUN: %target-swift-frontend -typecheck %t/Client_Swift5.swift -I %t \
@@ -31,7 +35,9 @@
3135
// RUN: -package-name pkg
3236
// RUN: %target-swift-frontend -typecheck %t/Client_Swift6.swift -I %t \
3337
// RUN: -swift-version 6 \
34-
// RUN: -enable-experimental-feature AccessLevelOnImport \
38+
// RUN: -package-name pkg
39+
// RUN: %target-swift-frontend -typecheck %t/Client_Swift6.swift -I %t \
40+
// RUN: -enable-upcoming-feature InternalImportsByDefault \
3541
// RUN: -package-name pkg
3642

3743
//--- DefaultLib.swift
@@ -55,7 +61,7 @@ private import PrivateLib
5561
//--- Client_Swift6.swift
5662

5763
import DefaultLib
58-
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-7=internal}}
64+
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=}}
5965
// expected-warning @-1 {{public import of 'PublicLib' was not used in public declarations or inlinable code}}
6066
package import PackageLib
6167
// expected-warning @-1 {{package import of 'PackageLib' was not used in package declarations}}

0 commit comments

Comments
 (0)