Skip to content

Commit 01ca212

Browse files
authored
Merge pull request #14080 from rudkx/sr6744-swift-5.0-branch
[5.0] Need to check that the keypath decl is an AbstractStorageDecl instead…
2 parents f5945a6 + 3c0e910 commit 01ca212

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
@@ -3927,8 +3927,11 @@ ConstraintSystem::simplifyKeyPathConstraint(Type keyPathTy,
39273927
if (!choices[i].isDecl()) {
39283928
return SolutionKind::Error;
39293929
}
3930+
auto storage = dyn_cast<AbstractStorageDecl>(choices[i].getDecl());
3931+
if (!storage) {
3932+
return SolutionKind::Error;
3933+
}
39303934

3931-
auto storage = cast<AbstractStorageDecl>(choices[i].getDecl());
39323935
if (!storage->isSettable(DC)) {
39333936
// A non-settable component makes the key path read-only, unless
39343937
// 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)