Skip to content

Commit a76d74b

Browse files
committed
When subscripting with a keypath, check for the path being an IUO.
1 parent 7073eb1 commit a76d74b

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

lib/Sema/CSApply.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,9 +1316,16 @@ namespace {
13161316
if (choice.getKind() == OverloadChoiceKind::KeyPathApplication) {
13171317
index = cs.coerceToRValue(index);
13181318
// The index argument should be (keyPath: KeyPath<Root, Value>).
1319-
auto keyPathTTy = cs.getType(index)->castTo<TupleType>()
1320-
->getElementType(0);
1321-
1319+
// Dig the key path expression out of the argument tuple.
1320+
auto indexKP = cast<TupleExpr>(index)->getElement(0);
1321+
auto keyPathTTy = cs.getType(indexKP);
1322+
1323+
// Check for the KeyPath being an IUO
1324+
if (auto pathTy = cs.lookThroughImplicitlyUnwrappedOptionalType(keyPathTTy)) {
1325+
keyPathTTy = pathTy;
1326+
indexKP = coerceImplicitlyUnwrappedOptionalToValue(indexKP, keyPathTTy, locator);
1327+
}
1328+
13221329
Type valueTy;
13231330
Type baseTy;
13241331
bool resultIsLValue;
@@ -1376,9 +1383,6 @@ namespace {
13761383
if (resultIsLValue)
13771384
valueTy = LValueType::get(valueTy);
13781385

1379-
// Dig the key path expression out of the argument tuple.
1380-
auto indexKP = cast<TupleExpr>(index)->getElement(0);
1381-
13821386
auto keyPathAp = new (cs.getASTContext())
13831387
KeyPathApplicationExpr(base, index->getStartLoc(), indexKP,
13841388
index->getEndLoc(), valueTy,

test/expr/unary/keypath/keypath.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,11 @@ class CC {
376376
func testKeyPathOptional() {
377377
_ = \AA.c?.i
378378
_ = \AA.c!.i
379+
380+
// SR-6198
381+
let path: KeyPath<CC,Int>! = \CC.i
382+
let cc = CC()
383+
_ = cc[keyPath: path]
379384
}
380385

381386
func testLiteralInAnyContext() {

0 commit comments

Comments
 (0)