Skip to content

Commit c2a87aa

Browse files
author
Harlan Haskins
authored
Relax @retroactive check to allow same-package declarations (#73512)
* 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 c2fd130 commit c2a87aa

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
@@ -185,16 +185,22 @@ usesDefaultDefinition(AssociatedTypeDecl *requirement) const {
185185
bool ProtocolConformance::isRetroactive() const {
186186
auto extensionModule = getDeclContext()->getParentModule();
187187
auto protocolModule = getProtocol()->getParentModule();
188-
if (extensionModule->isSameModuleLookingThroughOverlays(protocolModule)) {
188+
189+
auto isSameRetroactiveContext =
190+
[](ModuleDecl *moduleA, ModuleDecl *moduleB) -> bool {
191+
return moduleA->isSameModuleLookingThroughOverlays(moduleB) ||
192+
moduleA->inSamePackage(moduleB);
193+
};
194+
195+
if (isSameRetroactiveContext(extensionModule, protocolModule)) {
189196
return false;
190197
}
191198

192199
auto conformingTypeDecl =
193200
ConformingType->getNominalOrBoundGenericNominal();
194201
if (conformingTypeDecl) {
195202
auto conformingTypeModule = conformingTypeDecl->getParentModule();
196-
if (extensionModule->
197-
isSameModuleLookingThroughOverlays(conformingTypeModule)) {
203+
if (isSameRetroactiveContext(extensionModule, conformingTypeModule)) {
198204
return false;
199205
}
200206
}
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)