Skip to content

Commit 33a0e0f

Browse files
committed
Sema: Fix capture lifetime issue in protocol conformance sendable mismatch
The closure passed in to diagnoseOrDefer() outlives the ConformanceChecker instance, so instead of capturing 'this', pull out the 'DC' instance variable directly.
1 parent e805339 commit 33a0e0f

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/Sema/TypeCheckProtocol.cpp

+3-1
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) {
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)