Skip to content

Commit eca1cf9

Browse files
committed
Sema: Don't warn on @_export public imports not used in API
An `@_export public import` is meaningful on it's own as it declares a relationship between two modules and how clients see them. As such that import doesn't have to be referenced from API to be appropriate. Let's not warn on any public import with an `@_export` attribute. rdar://122032960
1 parent 8bb6846 commit eca1cf9

5 files changed

+11
-5
lines changed

lib/Sema/TypeCheckAccess.cpp

+4
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

+1-2
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

+1-2
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

-1
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

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
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
2324

2425
/// Check diagnostics.
2526
// RUN: %target-swift-frontend -typecheck %t/Client.swift -I %t \
@@ -102,6 +103,8 @@ public func notAnAPIFunc() -> NotAnAPIType { return NotAnAPIType() }
102103
public struct PackageType {}
103104
public func packageFunc() -> PackageType { return PackageType() }
104105

106+
//--- ExportedUnused.swift
107+
105108
//--- Client_Swift5.swift
106109
/// No diagnostics should be raised on the implicit access level.
107110
import UnusedImport // expected-error {{ambiguous implicit access level for import of 'UnusedImport'; it is imported as 'public' elsewhere}}
@@ -133,6 +136,8 @@ package import UnusedPackageImport // expected-warning {{package import of 'Unus
133136
public import ImportNotUseFromAPI // expected-warning {{public import of 'ImportNotUseFromAPI' was not used in public declarations or inlinable code}} {{1-8=}}
134137
public import ImportUsedInPackage // expected-warning {{public import of 'ImportUsedInPackage' was not used in public declarations or inlinable code}} {{1-7=package}}
135138

139+
@_exported public import ExportedUnused
140+
136141
public func useInSignature(_ a: TypeUsedInSignature) {} // expected-remark {{struct 'TypeUsedInSignature' is imported via 'DepUsedInSignature'}}
137142
public func exportedTypeUseInSignature(_ a: ExportedType) {} // expected-remark {{struct 'ExportedType' is imported via 'Exporter', which reexports definition from 'Exportee'}}
138143

0 commit comments

Comments
 (0)