Skip to content

Commit 251305e

Browse files
authored
Merge pull request #11541 from jckarter/keypath-reference-assignment-diagnostic
Sema: Track ReferenceWritableKeyPath applications as reads of their base.
2 parents e9900a2 + 9037719 commit 251305e

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2561,6 +2561,9 @@ void VarDeclUsageChecker::markStoredOrInOutExpr(Expr *E, unsigned Flags) {
25612561
if (KPA->getKeyPath()->getType()->getAnyNominal()
25622562
== C.getWritableKeyPathDecl())
25632563
markStoredOrInOutExpr(KPA->getBase(), RK_Written|RK_Read);
2564+
if (KPA->getKeyPath()->getType()->getAnyNominal()
2565+
== C.getReferenceWritableKeyPathDecl())
2566+
markStoredOrInOutExpr(KPA->getBase(), RK_Read);
25642567
return;
25652568
}
25662569

test/expr/unary/keypath/keypath-mutation.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,18 @@ func referenceSetting<Root, Value>(_ kp: ReferenceWritableKeyPath<Root, Value>,
1818
// expected-warning@+1 {{was never mutated}}
1919
var copy = root
2020
copy[keyPath: kp] = value
21+
22+
// Should not warn about lack of use of `immCopy`
23+
let immCopy = root
24+
immCopy[keyPath: kp] = value
25+
return copy
26+
}
27+
28+
func referenceUsage<Root, Value>(_ kp: ReferenceWritableKeyPath<Root, Value>, _ root: Root, _ value: Value) -> Root {
29+
// Should warn about lack of mutation, since a RefKeyPath doesn't modify its
30+
// base.
31+
// expected-warning@+1 {{was never mutated}}
32+
var copy = root
33+
copy[keyPath: kp] = value
2134
return copy
2235
}

0 commit comments

Comments
 (0)