Skip to content

Commit 837ed71

Browse files
authored
Merge pull request #81883 from DougGregor/unsafe-implied-conformance-6.2
[Strict memory safety] Improve Fix-Its for implied conformances
2 parents e511c07 + c70699c commit 837ed71

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2695,10 +2695,28 @@ checkIndividualConformance(NormalProtocolConformance *conformance) {
26952695
}
26962696

26972697
if (!unsafeUses.empty()) {
2698-
Context.Diags.diagnose(
2698+
// Primary diagnostic along with a Fix-It to add @unsafe in the appropriate
2699+
// place.
2700+
{
2701+
auto diag = Context.Diags.diagnose(
26992702
conformance->getLoc(), diag::conformance_involves_unsafe,
2700-
conformance->getType(), Proto)
2701-
.fixItInsert(conformance->getProtocolNameLoc(), "@unsafe ");
2703+
conformance->getType(), Proto);
2704+
2705+
// Find the original explicit conformance, where we can add the Fix-It.
2706+
auto explicitConformance = conformance;
2707+
while (explicitConformance->getSourceKind() ==
2708+
ConformanceEntryKind::Implied) {
2709+
explicitConformance =
2710+
explicitConformance->ProtocolConformance::getImplyingConformance();
2711+
}
2712+
2713+
if (explicitConformance->getSourceKind() ==
2714+
ConformanceEntryKind::Explicit) {
2715+
diag.fixItInsert(explicitConformance->getProtocolNameLoc(),
2716+
"@unsafe ");
2717+
}
2718+
}
2719+
27022720
for (const auto& unsafeUse : unsafeUses)
27032721
diagnoseUnsafeUse(unsafeUse);
27042722
}

test/Unsafe/safe.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,17 @@ func testSwitch(se: SomeEnum) {
352352
default: break
353353
}
354354
}
355+
356+
@unsafe class SomeClass {}
357+
@unsafe class SomeClassWrapper { }
358+
359+
protocol Associated {
360+
associatedtype Associated
361+
}
362+
363+
protocol CustomAssociated: Associated { }
364+
365+
// expected-warning@+1{{conformance of 'SomeClass' to protocol 'Associated' involves unsafe code}}{{22-22=@unsafe }}
366+
extension SomeClass: CustomAssociated {
367+
typealias Associated = SomeClassWrapper // expected-note{{unsafe type 'SomeClass.Associated' (aka 'SomeClassWrapper') cannot satisfy safe associated type 'Associated'}}
368+
}

0 commit comments

Comments
 (0)