Skip to content

Commit daa0c00

Browse files
authored
Merge pull request #76188 from tshortli/retroactive-does-not-apply-same-package-diagnostics
Sema: Fix `@retroactive` does not apply diags for same-package conformance
2 parents 5c30b30 + 2d4b211 commit daa0c00

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,8 +2606,9 @@ ERROR(retroactive_not_in_extension_inheritance_clause,none,
26062606
"extensions", ())
26072607

26082608
ERROR(retroactive_attr_does_not_apply,none,
2609-
"'retroactive' attribute does not apply; %0 is declared in this module",
2610-
(Identifier))
2609+
"'retroactive' attribute does not apply; %0 is declared in "
2610+
"%select{the same package|this module}1",
2611+
(const ValueDecl *, bool))
26112612

26122613
WARNING(extension_retroactive_conformance,none,
26132614
"extension declares a conformance of imported type %0 to imported "

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,13 +1819,20 @@ static void diagnoseRetroactiveConformances(
18191819
if (decl == proto && entry.isRetroactive()) {
18201820
auto loc =
18211821
entry.getTypeRepr()->findAttrLoc(TypeAttrKind::Retroactive);
1822+
1823+
bool typeInSamePackage = extTypeModule->inSamePackage(module);
18221824
bool typeIsSameModule =
18231825
extTypeModule->isSameModuleLookingThroughOverlays(module);
1824-
auto incorrectTypeName = typeIsSameModule ?
1825-
extendedNominalDecl->getName() : proto->getName();
1826+
1827+
auto declForDiag = (typeIsSameModule || typeInSamePackage)
1828+
? extendedNominalDecl
1829+
: proto;
1830+
bool isSameModule = declForDiag->getParentModule()
1831+
->isSameModuleLookingThroughOverlays(module);
1832+
18261833
diags
1827-
.diagnose(loc, diag::retroactive_attr_does_not_apply,
1828-
incorrectTypeName)
1834+
.diagnose(loc, diag::retroactive_attr_does_not_apply, declForDiag,
1835+
isSameModule)
18291836
.warnUntilSwiftVersion(6)
18301837
.fixItRemove(SourceRange(loc, loc.getAdvancedLoc(1)));
18311838
return TypeWalker::Action::Stop;

test/Sema/extension_retroactive_conformances_package.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
// RUN: %target-swift-frontend -swift-version 5 %t/Library.swift -emit-module -module-name Library -o %t -package-name Library
55
// 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
6+
// RUN: %target-swift-frontend -swift-version 5 %t/ExternalLibrary.swift -emit-module -module-name ExternalLibrary -o %t
7+
// RUN: %target-swift-frontend -typecheck %t/Client.swift -module-name Client -verify -swift-version 5 -I %t -package-name Library
78

89
//--- Library.swift
910

@@ -19,10 +20,16 @@ public protocol OtherLibraryProtocol {}
1920
package class PackageOtherLibraryClass {}
2021
package protocol PackageOtherLibraryProtocol {}
2122

23+
//--- ExternalLibrary.swift
24+
25+
public class ExternalLibraryClass {}
26+
public protocol ExternalLibraryProtocol {}
27+
2228
//--- Client.swift
2329

2430
public import Library
2531
public import OtherLibrary
32+
public import ExternalLibrary
2633

2734
// These are all fine because all 3 of these modules are in the same package.
2835

@@ -40,4 +47,13 @@ extension LibraryClass: PackageOtherLibraryProtocol {}
4047

4148
extension PackageLibraryClass: PackageLibraryProtocol {}
4249
extension PackageOtherLibraryClass: PackageLibraryProtocol {}
43-
extension PackageLibraryClass: PackageOtherLibraryProtocol {}
50+
extension PackageLibraryClass: PackageOtherLibraryProtocol {}
51+
52+
extension ExternalLibraryClass: ExternalLibraryProtocol {} // expected-warning {{extension declares a conformance of imported type 'ExternalLibraryClass' to imported protocol 'ExternalLibraryProtocol'; this will not behave correctly if the owners of 'ExternalLibrary' introduce this conformance in the future}}
53+
// expected-note @-1 {{add '@retroactive' to silence this warning}} {{33-56=@retroactive ExternalLibraryProtocol}}
54+
55+
extension ExternalLibraryClass: LibraryProtocol {}
56+
extension LibraryClass: ExternalLibraryProtocol {}
57+
58+
extension ExternalLibraryClass: @retroactive OtherLibraryProtocol {} // expected-warning {{'retroactive' attribute does not apply; 'OtherLibraryProtocol' is declared in the same package; this is an error in the Swift 6 language mode}}
59+
extension OtherLibraryClass: @retroactive ExternalLibraryProtocol {} // expected-warning {{'retroactive' attribute does not apply; 'OtherLibraryClass' is declared in the same package; this is an error in the Swift 6 language mode}}

0 commit comments

Comments
 (0)