Skip to content

Commit 5ac817e

Browse files
authored
Merge pull request swiftlang#76386 from xymus/public-import-of-private-fixit
Sema: Use `internal import` in fixits on public imports of private modules
2 parents b35a2a1 + 20b4187 commit 5ac817e

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
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 renamed to test/Sema/report-public-import-of-private-module.swift

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// RUN: %empty-directory(%t)
22
// RUN: split-file %s %t
33
// REQUIRES: VENDOR=apple
4-
// REQUIRES: asserts
54

65
/// Prepare the SDK.
76
// RUN: cp -r %S/Inputs/public-private-sdk %t/sdk
@@ -42,12 +41,12 @@
4241
// RUN: -library-level ipi -module-name MainLib
4342
//--- PublicImports.swift
4443
import PublicSwift
45-
import PrivateSwift // expected-error{{private module 'PrivateSwift' is imported publicly from the public module 'MainLib'}}
44+
import PrivateSwift // expected-error{{private module 'PrivateSwift' is imported publicly from the public module 'MainLib'}}{{1-1=internal }}
4645

4746
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'}}
47+
import PublicClang_Private // expected-error{{private module 'PublicClang_Private' is imported publicly from the public module 'MainLib'}}{{1-1=internal }}
48+
import FullyPrivateClang // expected-error{{private module 'FullyPrivateClang' is imported publicly from the public module 'MainLib'}}{{1-1=internal }}
49+
import LocalClang // expected-error{{private module 'LocalClang' is imported publicly from the public module 'MainLib'}}{{1-1=internal }}
5150
@_exported import MainLib // expected-warning{{private module 'MainLib' is imported publicly from the public module 'MainLib'}}
5251

5352
/// Expect no errors with implementation-only imports.
@@ -146,16 +145,27 @@ private import LocalClang
146145
// RUN: -library-level api -verify
147146
//--- ExplicitlyPublicImports.swift
148147
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}}
148+
// expected-warning @-1 {{public import of 'PublicSwift' was not used in public declarations or inlinable code}}{{1-7=internal}}
149+
public import PrivateSwift // expected-error{{private module 'PrivateSwift' is imported publicly from the public module 'MainLib'}}{{1-7=internal}}
150+
// expected-warning @-1 {{public import of 'PrivateSwift' was not used in public declarations or inlinable code}}{{1-7=internal}}
152151

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

0 commit comments

Comments
 (0)