Skip to content

Commit e221532

Browse files
committed
Sema: track use in API of declaring and bystanders for cross-import overlay
Cross-import overlays are imported automatically if the declaring module and the bystanders modules are also imported. In theory, one could import the declaring module and bystanders without using them and use only the overlay in API. Let’s make sure we track these indirect uses to avoid superfluous warning. rdar://129779460
1 parent e431216 commit e221532

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

lib/AST/Module.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2716,6 +2716,26 @@ void SourceFile::registerAccessLevelUsingImport(
27162716
ImportsUseAccessLevel[mod] = accessLevel;
27172717
else
27182718
ImportsUseAccessLevel[mod] = std::max(accessLevel, known->second);
2719+
2720+
// Also register modules triggering the import of cross-import overlays.
2721+
auto declaringMod = mod->getDeclaringModuleIfCrossImportOverlay();
2722+
if (!declaringMod)
2723+
return;
2724+
2725+
SmallVector<Identifier, 1> bystanders;
2726+
auto bystandersValid =
2727+
mod->getRequiredBystandersIfCrossImportOverlay(declaringMod, bystanders);
2728+
if (!bystandersValid)
2729+
return;
2730+
2731+
for (auto &otherImport : *Imports) {
2732+
auto otherImportMod = otherImport.module.importedModule;
2733+
auto otherImportModName = otherImportMod->getName();
2734+
if (otherImportMod == declaringMod ||
2735+
llvm::find(bystanders, otherImportModName) != bystanders.end()) {
2736+
registerAccessLevelUsingImport(otherImport, accessLevel);
2737+
}
2738+
}
27192739
}
27202740

27212741
bool HasImportsMatchingFlagRequest::evaluate(Evaluator &evaluator,

test/CrossImport/access-level-imports-errors.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
// RUN: cp -r %S/Inputs/lib-templates/* %t/
55
// RUN: split-file --leading-lines %s %t
66

7+
//--- SomeUnrelatedModule.swift
8+
// RUN: %target-swift-emit-module-interface(%t/lib/swift/SomeUnrelatedModule.swiftinterface) %t/SomeUnrelatedModule.swift -module-name SomeUnrelatedModule
9+
710
//--- BothPublic.swift
8-
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %t/BothPublic.swift -enable-cross-import-overlays -I %t/lib/swift -module-name ClientLibrary
11+
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %t/BothPublic.swift -enable-cross-import-overlays -I %t/lib/swift -module-name ClientLibrary -verify
912

1013
public import DeclaringLibrary
1114
public import BystandingLibrary
15+
public import SomeUnrelatedModule // expected-warning {{public import of 'SomeUnrelatedModule' was not used in public declarations or inlinable code}}
1216

1317
public func fn(_: OverlayLibraryTy) {}
1418

@@ -28,7 +32,7 @@ public func fn(_: OverlayLibraryTy) {}
2832
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %t/FirstHidden.swift -enable-cross-import-overlays -I %t/lib/swift -module-name ClientLibrary -verify
2933

3034
internal import DeclaringLibrary
31-
public import BystandingLibrary // expected-warning {{public import of 'BystandingLibrary' was not used in public declarations or inlinable code}}
35+
public import BystandingLibrary
3236

3337
public func fn(_: OverlayLibraryTy) {}
3438
// expected-error @-1 {{function cannot be declared public because its parameter uses an internal type}}
@@ -38,7 +42,7 @@ public func fn(_: OverlayLibraryTy) {}
3842
//--- SecondHidden.swift
3943
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %t/SecondHidden.swift -enable-cross-import-overlays -I %t/lib/swift -module-name ClientLibrary -verify
4044

41-
public import DeclaringLibrary // expected-warning {{public import of 'DeclaringLibrary' was not used in public declarations or inlinable code}}
45+
public import DeclaringLibrary
4246
internal import BystandingLibrary
4347

4448
public func fn(_: OverlayLibraryTy) {}
@@ -66,3 +70,11 @@ private import BystandingLibrary
6670
internal func fn(_: OverlayLibraryTy) {}
6771
// expected-error @-1 {{function cannot be declared internal because its parameter uses a private type}}
6872
// expected-note @-2 {{struct 'OverlayLibraryTy' is imported by this file as 'private' from '_OverlayLibrary'}}
73+
74+
75+
//--- UnusedOverlay.swift
76+
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %t/UnusedOverlay.swift -enable-cross-import-overlays -I %t/lib/swift -module-name ClientLibrary -verify
77+
78+
public import DeclaringLibrary // expected-warning {{public import of 'DeclaringLibrary' was not used in public declarations or inlinable code}}
79+
public import BystandingLibrary // expected-warning {{public import of 'BystandingLibrary' was not used in public declarations or inlinable code}}
80+
public import SomeUnrelatedModule // expected-warning {{public import of 'SomeUnrelatedModule' was not used in public declarations or inlinable code}}

0 commit comments

Comments
 (0)