Skip to content

Commit 3122440

Browse files
authored
Merge pull request #66766 from amritpan/key-path-backward-diag
[ConstraintSystem] Fix keyPath diagnostic error for value convert to contextual types
2 parents 22d34d9 + d6486e2 commit 3122440

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12202,7 +12202,7 @@ ConstraintSystem::simplifyKeyPathConstraint(
1220212202
(definitelyKeyPathType && capability == ReadOnly)) {
1220312203
auto resolvedKPTy =
1220412204
BoundGenericType::get(kpDecl, nullptr, {rootTy, valueTy});
12205-
return matchTypes(keyPathTy, resolvedKPTy, ConstraintKind::Bind, subflags,
12205+
return matchTypes(resolvedKPTy, keyPathTy, ConstraintKind::Bind, subflags,
1220612206
loc);
1220712207
} else {
1220812208
addUnsolvedConstraint(Constraint::create(*this, ConstraintKind::KeyPath,

test/Constraints/keypath.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,19 @@ func rdar85458997() {
206206
_ = S(\.name)
207207
// expected-error@-1 {{cannot infer key path type from context; consider explicitly specifying a root type}} {{10-10=<#Root#>}}
208208
}
209+
210+
// https://github.com/apple/swift/issues/65965 - failed to produce correct types for key path capability mismatch
211+
func issue_65965() {
212+
struct S {
213+
var s: String
214+
let v: String
215+
}
216+
217+
let refKP: ReferenceWritableKeyPath<S, String>
218+
refKP = \.s
219+
// expected-error@-1 {{key path value type 'WritableKeyPath<S, String>' cannot be converted to contextual type 'ReferenceWritableKeyPath<S, String>'}}
220+
221+
let writeKP: WritableKeyPath<S, String>
222+
writeKP = \.v
223+
// expected-error@-1 {{key path value type 'KeyPath<S, String>' cannot be converted to contextual type 'WritableKeyPath<S, String>'}}
224+
}

0 commit comments

Comments
 (0)