Skip to content

Commit cfad698

Browse files
committed
[CSDiagnostics] Tailor diagnostic for Escapable conformance mismatch in key path context
1 parent db2b9f0 commit cfad698

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,8 @@ ERROR(expr_keypath_generic_type,none,
667667
"key path cannot refer to generic type %0", (Identifier))
668668
ERROR(expr_keypath_noncopyable_type,none,
669669
"key path cannot refer to noncopyable type %0", (Type))
670+
ERROR(expr_keypath_nonescapable_type,none,
671+
"key path cannot refer to nonescapable type %0", (Type))
670672
ERROR(expr_keypath_not_property,none,
671673
"%select{key path|dynamic key path member lookup}1 cannot refer to %kind0",
672674
(const ValueDecl *, bool))

lib/Sema/CSDiagnostics.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,11 @@ bool MissingConformanceFailure::diagnoseAsError() {
689689
emitDiagnostic(diag::expr_keypath_noncopyable_type, nonConformingType);
690690
return true;
691691
}
692+
693+
if (P->isSpecificProtocol(KnownProtocolKind::Escapable)) {
694+
emitDiagnostic(diag::expr_keypath_nonescapable_type, nonConformingType);
695+
return true;
696+
}
692697
}
693698
}
694699

test/Sema/lifetime_attr.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,18 @@ func invalidTarget(_ result: inout NE, _ source: consuming NE) { // expected-err
4444
func immortalConflict(_ immortal: Int) -> NE { // expected-error{{conflict between the parameter name and 'immortal' contextual keyword}}
4545
NE()
4646
}
47+
48+
do {
49+
struct Test: ~Escapable {
50+
var v1: Int
51+
var v2: NE
52+
}
53+
54+
_ = \Test.v1 // expected-error {{key path cannot refer to nonescapable type 'Test'}}
55+
_ = \Test.v2 // expected-error {{key path cannot refer to nonescapable type 'Test'}} expected-error {{key path cannot refer to nonescapable type 'NE'}}
56+
57+
func use(t: Test) {
58+
t[keyPath: \.v1] // expected-error {{key path cannot refer to nonescapable type 'Test'}}
59+
t[keyPath: \.v2] // expected-error {{key path cannot refer to nonescapable type 'Test'}} expected-error {{key path cannot refer to nonescapable type 'NE'}}
60+
}
61+
}

0 commit comments

Comments
 (0)