Skip to content

Commit 517d185

Browse files
authored
Merge pull request #71483 from xymus/superfluously-public-import-fixes
Sema: Don't warn on @_exported public imports that are not used in API
2 parents a425853 + 422e4cd commit 517d185

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
lines changed

lib/Sema/TypeCheckAccess.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,6 +2472,10 @@ void swift::diagnoseUnnecessaryPublicImports(SourceFile &SF) {
24722472
if (importedModule->getTopLevelModule() != importedModule)
24732473
continue;
24742474

2475+
// Ignore @_exported as by themselves the import is meaningful.
2476+
if (import.options.contains(ImportFlags::Exported))
2477+
continue;
2478+
24752479
AccessLevel levelUsed = SF.getMaxAccessLevelUsingImport(importedModule);
24762480
if (import.accessLevel <= levelUsed)
24772481
continue;

test/ModuleInterface/imports-swift6.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//--- empty.swift
1919

2020
//--- main.swift
21-
@_exported public import resilient // expected-warning {{public import of 'resilient' was not used in public declarations or inlinable code}}
21+
@_exported public import resilient
2222
public import B.B2 // expected-warning {{public import of 'B' was not used in public declarations or inlinable code}}
2323

2424
public import func C.c // expected-warning {{public import of 'C' was not used in public declarations or inlinable code}}
@@ -40,7 +40,6 @@ public import NotSoSecret // expected-warning {{'NotSoSecret' inconsistently imp
4040
@_implementationOnly import NotSoSecret2 // expected-note {{imported as implementation-only here}}
4141
//--- clientWithError.swift
4242
@_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}}
43-
// expected-warning @-1 {{public import of 'nonResilient' was not used in public declarations or inlinable code}}
4443

4544
// CHECK-6-NOT: import
4645
// CHECK-6: {{^}}public import A{{$}}

test/Sema/conflicting-import-restrictions.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,10 @@
4949
// RUN: -experimental-spi-only-imports -verify
5050

5151
//--- Public_Exported.swift
52-
@_exported public import Lib // expected-warning {{public import of 'Lib' was not used in public declarations or inlinable code}}
52+
@_exported public import Lib
5353

5454
//--- Package_Exported.swift
5555
@_exported package import Lib // expected-error {{'@_exported' is incompatible with 'package'; it can only be applied to public imports}}
56-
// expected-warning @-1 {{package import of 'Lib' was not used in package declarations}}
5756

5857
//--- Internal_Exported.swift
5958
@_exported internal import Lib // expected-error {{'@_exported' is incompatible with 'internal'; it can only be applied to public imports}}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,3 @@ public import FullyPrivateClang // expected-error{{private module 'FullyPrivateC
159159
public import LocalClang // expected-error{{private module 'LocalClang' is imported publicly from the public module 'MainLib'}}
160160
// expected-warning @-1 {{public import of 'LocalClang' was not used in public declarations or inlinable code}}
161161
@_exported public import MainLib // expected-warning{{private module 'MainLib' is imported publicly from the public module 'MainLib'}}
162-
// expected-warning @-1 {{public import of 'MainLib' was not used in public declarations or inlinable code}}

test/Sema/superfluously-public-imports.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020
// RUN: %target-swift-frontend -emit-module %t/UnusedPackageImport.swift -o %t -I %t
2121
// RUN: %target-swift-frontend -emit-module %t/ImportNotUseFromAPI.swift -o %t -I %t
2222
// RUN: %target-swift-frontend -emit-module %t/ImportUsedInPackage.swift -o %t -I %t
23+
// RUN: %target-swift-frontend -emit-module %t/ExportedUnused.swift -o %t -I %t
24+
// RUN: %target-swift-frontend -emit-module %t/SPIOnlyUsedInSPI.swift -o %t -I %t
2325

2426
/// Check diagnostics.
2527
// RUN: %target-swift-frontend -typecheck %t/Client.swift -I %t \
26-
// RUN: -package-name pkg -Rmodule-api-import -swift-version 6 -verify
28+
// RUN: -package-name pkg -Rmodule-api-import -swift-version 6 -verify \
29+
// RUN: -experimental-spi-only-imports
2730
// RUN: %target-swift-frontend -typecheck %t/ClientOfClangModules.swift -I %t \
2831
// RUN: -package-name pkg -Rmodule-api-import -swift-version 6 -verify
2932
// RUN: %target-swift-frontend -typecheck %t/Client_Swift5.swift -I %t \
@@ -102,6 +105,11 @@ public func notAnAPIFunc() -> NotAnAPIType { return NotAnAPIType() }
102105
public struct PackageType {}
103106
public func packageFunc() -> PackageType { return PackageType() }
104107

108+
//--- ExportedUnused.swift
109+
110+
//--- SPIOnlyUsedInSPI.swift
111+
public struct ToUseFromSPI {}
112+
105113
//--- Client_Swift5.swift
106114
/// No diagnostics should be raised on the implicit access level.
107115
import UnusedImport // expected-error {{ambiguous implicit access level for import of 'UnusedImport'; it is imported as 'public' elsewhere}}
@@ -133,6 +141,9 @@ package import UnusedPackageImport // expected-warning {{package import of 'Unus
133141
public import ImportNotUseFromAPI // expected-warning {{public import of 'ImportNotUseFromAPI' was not used in public declarations or inlinable code}} {{1-8=}}
134142
public import ImportUsedInPackage // expected-warning {{public import of 'ImportUsedInPackage' was not used in public declarations or inlinable code}} {{1-7=package}}
135143

144+
@_exported public import ExportedUnused
145+
@_spiOnly public import SPIOnlyUsedInSPI
146+
136147
public func useInSignature(_ a: TypeUsedInSignature) {} // expected-remark {{struct 'TypeUsedInSignature' is imported via 'DepUsedInSignature'}}
137148
public func exportedTypeUseInSignature(_ a: ExportedType) {} // expected-remark {{struct 'ExportedType' is imported via 'Exporter', which reexports definition from 'Exportee'}}
138149

@@ -214,6 +225,9 @@ func implicitlyInternalFunc(a: NotAnAPIType = notAnAPIFunc()) {}
214225
// For package decls we only remark on types used in signatures, not for inlinable code.
215226
package func packageFunc(a: PackageType = packageFunc()) {} // expected-remark {{struct 'PackageType' is imported via 'ImportUsedInPackage'}}
216227

228+
@_spi(X)
229+
public func spiFunc(a: ToUseFromSPI) {} // expected-remark {{struct 'ToUseFromSPI' is imported via 'SPIOnlyUsedInSPI'}}
230+
217231
/// Tests for imports of clang modules.
218232
//--- module.modulemap
219233
module ClangSimpleUnused {

0 commit comments

Comments
 (0)