Skip to content

Commit 4fe88c2

Browse files
committed
Sema: Use internal import instead of @_implementationOnly on public imports of a private module
The error about a public import of a private module is raised when a library-level API module imports a library-level SPI module without a non-public access-level, `@_implementationOnly` attribute or `@_spiOnly` attribute. Update the fixit to insert an `internal` instead of a `@_implementationOnly`.
1 parent e5abfcb commit 4fe88c2

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2404,10 +2404,13 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
24042404
InFlightDiagnostic inFlight =
24052405
Ctx.Diags.diagnose(ID, diag::error_public_import_of_private_module,
24062406
target->getName(), importer->getName());
2407-
if (ID->getAttrs().isEmpty()) {
2408-
inFlight.fixItInsert(ID->getStartLoc(),
2409-
"@_implementationOnly ");
2410-
}
2407+
if (auto attr = ID->getAttrs().getAttribute<AccessControlAttr>()) {
2408+
if (Ctx.LangOpts.hasFeature(Feature::InternalImportsByDefault))
2409+
inFlight.fixItRemove(attr->getLocation());
2410+
else
2411+
inFlight.fixItReplace(attr->getLocation(), "internal");
2412+
} else
2413+
inFlight.fixItInsert(ID->getStartLoc(), "internal ");
24112414

24122415
#ifndef NDEBUG
24132416
static bool enableTreatAsError = true;

test/Sema/implementation-only-import-suggestion.swift

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@
4242
// RUN: -library-level ipi -module-name MainLib
4343
//--- PublicImports.swift
4444
import PublicSwift
45-
import PrivateSwift // expected-error{{private module 'PrivateSwift' is imported publicly from the public module 'MainLib'}}
45+
import PrivateSwift // expected-error{{private module 'PrivateSwift' is imported publicly from the public module 'MainLib'}}{{1-1=internal }}
4646

4747
import PublicClang
48-
import PublicClang_Private // expected-error{{private module 'PublicClang_Private' is imported publicly from the public module 'MainLib'}}
49-
import FullyPrivateClang // expected-error{{private module 'FullyPrivateClang' is imported publicly from the public module 'MainLib'}}
50-
import LocalClang // expected-error{{private module 'LocalClang' is imported publicly from the public module 'MainLib'}}
48+
import PublicClang_Private // expected-error{{private module 'PublicClang_Private' is imported publicly from the public module 'MainLib'}}{{1-1=internal }}
49+
import FullyPrivateClang // expected-error{{private module 'FullyPrivateClang' is imported publicly from the public module 'MainLib'}}{{1-1=internal }}
50+
import LocalClang // expected-error{{private module 'LocalClang' is imported publicly from the public module 'MainLib'}}{{1-1=internal }}
5151
@_exported import MainLib // expected-warning{{private module 'MainLib' is imported publicly from the public module 'MainLib'}}
5252

5353
/// Expect no errors with implementation-only imports.
@@ -146,16 +146,27 @@ private import LocalClang
146146
// RUN: -library-level api -verify
147147
//--- ExplicitlyPublicImports.swift
148148
public import PublicSwift
149-
// expected-warning @-1 {{public import of 'PublicSwift' was not used in public declarations or inlinable code}}
150-
public import PrivateSwift // expected-error{{private module 'PrivateSwift' is imported publicly from the public module 'MainLib'}}
151-
// expected-warning @-1 {{public import of 'PrivateSwift' was not used in public declarations or inlinable code}}
149+
// expected-warning @-1 {{public import of 'PublicSwift' was not used in public declarations or inlinable code}}{{1-7=internal}}
150+
public import PrivateSwift // expected-error{{private module 'PrivateSwift' is imported publicly from the public module 'MainLib'}}{{1-7=internal}}
151+
// expected-warning @-1 {{public import of 'PrivateSwift' was not used in public declarations or inlinable code}}{{1-7=internal}}
152152

153153
public import PublicClang
154-
// expected-warning @-1 {{public import of 'PublicClang' was not used in public declarations or inlinable code}}
155-
public import PublicClang_Private // expected-error{{private module 'PublicClang_Private' is imported publicly from the public module 'MainLib'}}
156-
// expected-warning @-1 {{public import of 'PublicClang_Private' was not used in public declarations or inlinable code}}
157-
public import FullyPrivateClang // expected-error{{private module 'FullyPrivateClang' is imported publicly from the public module 'MainLib'}}
158-
// expected-warning @-1 {{public import of 'FullyPrivateClang' was not used in public declarations or inlinable code}}
159-
public import LocalClang // expected-error{{private module 'LocalClang' is imported publicly from the public module 'MainLib'}}
160-
// expected-warning @-1 {{public import of 'LocalClang' was not used in public declarations or inlinable code}}
161-
@_exported public import MainLib // expected-warning{{private module 'MainLib' is imported publicly from the public module 'MainLib'}}
154+
// expected-warning @-1 {{public import of 'PublicClang' was not used in public declarations or inlinable code}}{{1-7=internal}}
155+
public import PublicClang_Private // expected-error{{private module 'PublicClang_Private' is imported publicly from the public module 'MainLib'}}{{1-7=internal}}
156+
// expected-warning @-1 {{public import of 'PublicClang_Private' was not used in public declarations or inlinable code}}{{1-7=internal}}
157+
public import FullyPrivateClang // expected-error{{private module 'FullyPrivateClang' is imported publicly from the public module 'MainLib'}}{{1-7=internal}}
158+
// expected-warning @-1 {{public import of 'FullyPrivateClang' was not used in public declarations or inlinable code}}{{1-7=internal}}
159+
public import LocalClang // expected-error{{private module 'LocalClang' is imported publicly from the public module 'MainLib'}}{{1-7=internal}}
160+
// expected-warning @-1 {{public import of 'LocalClang' was not used in public declarations or inlinable code}}{{1-7=internal}}
161+
@_exported public import MainLib // expected-warning{{private module 'MainLib' is imported publicly from the public module 'MainLib'}}{{12-18=internal}}
162+
163+
// RUN: %target-swift-frontend -typecheck -sdk %t/sdk %t/ImplictlyInternalImports.swift \
164+
// RUN: -module-name MainLib -module-cache-path %t \
165+
// RUN: -F %t/sdk/System/Library/PrivateFrameworks/ \
166+
// RUN: -enable-upcoming-feature InternalImportsByDefault \
167+
// RUN: -library-level api -verify
168+
//--- ImplictlyInternalImports.swift
169+
public import PublicSwift
170+
// expected-warning @-1 {{public import of 'PublicSwift' was not used in public declarations or inlinable code}}{{1-8=}}
171+
public import PrivateSwift // expected-error{{private module 'PrivateSwift' is imported publicly from the public module 'MainLib'}}{{1-8=}}
172+
// expected-warning @-1 {{public import of 'PrivateSwift' was not used in public declarations or inlinable code}}{{1-8=}}

0 commit comments

Comments
 (0)