Skip to content

Commit 713ca60

Browse files
Merge pull request swiftlang#33519 from LucianoPAlmeida/assing-through-partial-keypath-diagnostic
[CSDiagnostics] Also consider PartialKeyPath a non-writable keypath on AssignmentFailure diagnostics
2 parents 0bd7bbf + 53cdd77 commit 713ca60

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1860,7 +1860,9 @@ AssignmentFailure::resolveImmutableBase(Expr *expr) const {
18601860
// In Swift versions lower than 5, this check will fail as read only
18611861
// key paths can masquerade as writable for compatibilty reasons.
18621862
// This is fine as in this case we just fall back on old diagnostics.
1863-
if (bgt->getDecl() == getASTContext().getKeyPathDecl()) {
1863+
auto &ctx = getASTContext();
1864+
if (bgt->getDecl() == ctx.getKeyPathDecl() ||
1865+
bgt->getDecl() == ctx.getPartialKeyPathDecl()) {
18641866
return {expr, member};
18651867
}
18661868
}

test/expr/unary/keypath/keypath.swift

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@ func testKeyPathSubscript(readonly: Z, writable: inout Z,
324324
var anySink2 = writable[keyPath: pkp]
325325
expect(&anySink2, toHaveType: Exactly<Any>.self)
326326

327-
readonly[keyPath: pkp] = anySink1 // expected-error{{cannot assign through subscript: 'readonly' is a 'let' constant}}
328-
writable[keyPath: pkp] = anySink2 // expected-error{{cannot assign through subscript: 'writable' is immutable}}
327+
readonly[keyPath: pkp] = anySink1 // expected-error{{cannot assign through subscript: 'pkp' is a read-only key path}}
328+
writable[keyPath: pkp] = anySink2 // expected-error{{cannot assign through subscript: 'pkp' is a read-only key path}}
329329

330330
let akp: AnyKeyPath = pkp
331331

@@ -443,12 +443,12 @@ func testKeyPathSubscriptLValue(base: Z, kp: inout KeyPath<Z, Z>) {
443443
}
444444

445445
func testKeyPathSubscriptExistentialBase(concreteBase: inout B,
446-
existentialBase: inout P,
447-
kp: KeyPath<P, String>,
448-
wkp: WritableKeyPath<P, String>,
449-
rkp: ReferenceWritableKeyPath<P, String>,
450-
pkp: PartialKeyPath<P>,
451-
s: String) {
446+
existentialBase: inout P,
447+
kp: KeyPath<P, String>,
448+
wkp: WritableKeyPath<P, String>,
449+
rkp: ReferenceWritableKeyPath<P, String>,
450+
pkp: PartialKeyPath<P>,
451+
s: String) {
452452
_ = concreteBase[keyPath: kp]
453453
_ = concreteBase[keyPath: wkp]
454454
_ = concreteBase[keyPath: rkp]
@@ -457,9 +457,7 @@ func testKeyPathSubscriptExistentialBase(concreteBase: inout B,
457457
concreteBase[keyPath: kp] = s // expected-error {{cannot assign through subscript: 'kp' is a read-only key path}}
458458
concreteBase[keyPath: wkp] = s // expected-error {{key path with root type 'P' cannot be applied to a base of type 'B'}}
459459
concreteBase[keyPath: rkp] = s
460-
// TODO(diagnostics): Improve this diagnostic message because concreteBase is mutable, the problem is related to assign
461-
// through PartialKeyPath.
462-
concreteBase[keyPath: pkp] = s // expected-error {{cannot assign through subscript: 'concreteBase' is immutable}}
460+
concreteBase[keyPath: pkp] = s // expected-error {{cannot assign through subscript: 'pkp' is a read-only key path}}
463461

464462
_ = existentialBase[keyPath: kp]
465463
_ = existentialBase[keyPath: wkp]
@@ -469,9 +467,7 @@ func testKeyPathSubscriptExistentialBase(concreteBase: inout B,
469467
existentialBase[keyPath: kp] = s // expected-error {{cannot assign through subscript: 'kp' is a read-only key path}}
470468
existentialBase[keyPath: wkp] = s
471469
existentialBase[keyPath: rkp] = s
472-
// TODO(diagnostics): Improve this diagnostic message because existentialBase is mutable, the problem is related to assign
473-
// through PartialKeyPath.
474-
existentialBase[keyPath: pkp] = s // expected-error {{cannot assign through subscript: 'existentialBase' is immutable}}
470+
existentialBase[keyPath: pkp] = s // expected-error {{cannot assign through subscript: 'pkp' is a read-only key path}}
475471
}
476472

477473
struct AA {

0 commit comments

Comments
 (0)