Skip to content

Commit 784655f

Browse files
author
Harlan Haskins
authored
Relax @retroactive check to allow same-package declarations (#73512) (#75079)
* Relax @retroactive check to allow same-package declarations * Add package decls to the test too * Fix tests * Import test libraries publicly so I can use their conformances in public retroactive extensions
1 parent f80a182 commit 784655f

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

lib/AST/ProtocolConformance.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,22 @@ usesDefaultDefinition(AssociatedTypeDecl *requirement) const {
184184
bool ProtocolConformance::isRetroactive() const {
185185
auto extensionModule = getDeclContext()->getParentModule();
186186
auto protocolModule = getProtocol()->getParentModule();
187-
if (extensionModule->isSameModuleLookingThroughOverlays(protocolModule)) {
187+
188+
auto isSameRetroactiveContext =
189+
[](ModuleDecl *moduleA, ModuleDecl *moduleB) -> bool {
190+
return moduleA->isSameModuleLookingThroughOverlays(moduleB) ||
191+
moduleA->inSamePackage(moduleB);
192+
};
193+
194+
if (isSameRetroactiveContext(extensionModule, protocolModule)) {
188195
return false;
189196
}
190197

191198
auto conformingTypeDecl =
192199
ConformingType->getNominalOrBoundGenericNominal();
193200
if (conformingTypeDecl) {
194201
auto conformingTypeModule = conformingTypeDecl->getParentModule();
195-
if (extensionModule->
196-
isSameModuleLookingThroughOverlays(conformingTypeModule)) {
202+
if (isSameRetroactiveContext(extensionModule, conformingTypeModule)) {
197203
return false;
198204
}
199205
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
// RUN: %target-swift-frontend -swift-version 5 %t/Library.swift -emit-module -module-name Library -o %t -package-name Library
5+
// RUN: %target-swift-frontend -swift-version 5 %t/OtherLibrary.swift -emit-module -module-name OtherLibrary -o %t -package-name Library
6+
// RUN: %target-swift-frontend -typecheck %t/Client.swift -module-name Client -verify -swift-version 5 -I %t -package-name Library
7+
8+
//--- Library.swift
9+
10+
public class LibraryClass {}
11+
public protocol LibraryProtocol {}
12+
package class PackageLibraryClass {}
13+
package protocol PackageLibraryProtocol {}
14+
15+
//--- OtherLibrary.swift
16+
17+
public class OtherLibraryClass {}
18+
public protocol OtherLibraryProtocol {}
19+
package class PackageOtherLibraryClass {}
20+
package protocol PackageOtherLibraryProtocol {}
21+
22+
//--- Client.swift
23+
24+
public import Library
25+
public import OtherLibrary
26+
27+
// These are all fine because all 3 of these modules are in the same package.
28+
29+
extension LibraryClass: LibraryProtocol {}
30+
extension OtherLibraryClass: LibraryProtocol {}
31+
extension LibraryClass: OtherLibraryProtocol {}
32+
33+
extension PackageLibraryClass: LibraryProtocol {}
34+
extension PackageOtherLibraryClass: LibraryProtocol {}
35+
extension PackageLibraryClass: OtherLibraryProtocol {}
36+
37+
extension LibraryClass: PackageLibraryProtocol {}
38+
extension OtherLibraryClass: PackageLibraryProtocol {}
39+
extension LibraryClass: PackageOtherLibraryProtocol {}
40+
41+
extension PackageLibraryClass: PackageLibraryProtocol {}
42+
extension PackageOtherLibraryClass: PackageLibraryProtocol {}
43+
extension PackageLibraryClass: PackageOtherLibraryProtocol {}

0 commit comments

Comments
 (0)