Skip to content

Commit 12486b1

Browse files
authored
Merge pull request swiftlang#59558 from slavapestov/sendable-mismatch-capture-lifetime-fix
Sema: Fix capture lifetime issue in protocol conformance sendable mismatch
2 parents af8ae59 + 33a0e0f commit 12486b1

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4219,8 +4219,10 @@ ConformanceChecker::resolveWitnessViaLookup(ValueDecl *requirement) {
42194219
if (behavior != DiagnosticBehavior::Ignore) {
42204220
bool isError = behavior < DiagnosticBehavior::Warning;
42214221

4222+
// Avoid relying on the lifetime of 'this'.
4223+
const DeclContext *DC = this->DC;
42224224
diagnoseOrDefer(requirement, isError,
4223-
[this, requirement, witness, sendFrom](
4225+
[DC, requirement, witness, sendFrom](
42244226
NormalProtocolConformance *conformance) {
42254227
diagnoseSendabilityErrorBasedOn(conformance->getProtocol(), sendFrom,
42264228
[&](DiagnosticBehavior limit) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
// This triggers a conformance check with SuppressDiagnostics=true.
4+
let x = S().f {}
5+
6+
protocol P {
7+
associatedtype A
8+
9+
func f(_: A) -> Int // expected-note {{expected sendability to match requirement here}}
10+
}
11+
12+
struct S : P {
13+
typealias A = () -> ()
14+
func f(_: @Sendable () -> ()) -> Int { return 0 }
15+
// expected-warning@-1 {{sendability of function types in instance method 'f' does not match requirement in protocol 'P'}}
16+
}

0 commit comments

Comments
 (0)