Skip to content

Commit 3ab95d1

Browse files
authored
Merge pull request #14067 from jckarter/sr6744-4.1
Need to check that the keypath decl is an AbstractStorageDecl instead of assuming it.
2 parents 71293a8 + b412f69 commit 3ab95d1

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3938,8 +3938,11 @@ ConstraintSystem::simplifyKeyPathConstraint(Type keyPathTy,
39383938
if (!choices[i].isDecl()) {
39393939
return SolutionKind::Error;
39403940
}
3941+
auto storage = dyn_cast<AbstractStorageDecl>(choices[i].getDecl());
3942+
if (!storage) {
3943+
return SolutionKind::Error;
3944+
}
39413945

3942-
auto storage = cast<AbstractStorageDecl>(choices[i].getDecl());
39433946
if (!storage->isSettable(DC)) {
39443947
// A non-settable component makes the key path read-only, unless
39453948
// a reference-writable component shows up later.

test/expr/unary/keypath/keypath.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,20 @@ func sr6106() {
485485
}
486486
}
487487

488+
// SR-6744
489+
func sr6744() {
490+
struct ABC {
491+
let value: Int
492+
func value(adding i: Int) -> Int { return value + i }
493+
}
494+
495+
let abc = ABC(value: 0)
496+
func get<T>(for kp: KeyPath<ABC, T>) -> T {
497+
return abc[keyPath: kp]
498+
}
499+
_ = get(for: \.value)
500+
}
501+
488502
func testSyntaxErrors() { // expected-note{{}}
489503
_ = \. ; // expected-error{{expected member name following '.'}}
490504
_ = \.a ;

0 commit comments

Comments
 (0)