Skip to content

Commit 50504c3

Browse files
authored
Merge pull request #59559 from slavapestov/sendable-mismatch-capture-lifetime-fix-5.7
Sema: Fix capture lifetime issue in protocol conformance sendable mismatch [5.7]
2 parents 50a2c57 + 3a9049a commit 50504c3

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
@@ -4198,8 +4198,10 @@ ConformanceChecker::resolveWitnessViaLookup(ValueDecl *requirement) {
41984198
if (behavior != DiagnosticBehavior::Ignore) {
41994199
bool isError = behavior < DiagnosticBehavior::Warning;
42004200

4201+
// Avoid relying on the lifetime of 'this'.
4202+
const DeclContext *DC = this->DC;
42014203
diagnoseOrDefer(requirement, isError,
4202-
[this, requirement, witness, sendFrom](
4204+
[DC, requirement, witness, sendFrom](
42034205
NormalProtocolConformance *conformance) {
42044206
diagnoseSendabilityErrorBasedOn(conformance->getProtocol(), sendFrom,
42054207
[&](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)