Skip to content

Commit bbdada4

Browse files
committed
[Parse] Try to preserve the invariant that anything that contains KeyPathDotExpr must be wrapped in KeyPathExpr
Even in the presence of invalid code. Fixes typechecker crash in rdar://50666427
1 parent 3d5cb90 commit bbdada4

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

lib/Parse/ParseExpr.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -600,10 +600,11 @@ ParserResult<Expr> Parser::parseExprKeyPath() {
600600
pathResult = parseExprPostfixSuffix(inner, /*isExprBasic=*/true,
601601
/*periodHasKeyPathBehavior=*/false,
602602
unusedHasBindOptional);
603-
if (pathResult.isParseError())
604-
return pathResult;
605603
}
606604

605+
if (!rootResult.getPtrOrNull() && !pathResult.getPtrOrNull())
606+
return pathResult;
607+
607608
auto keypath = new (Context) KeyPathExpr(
608609
backslashLoc, rootResult.getPtrOrNull(), pathResult.getPtrOrNull());
609610

@@ -618,7 +619,8 @@ ParserResult<Expr> Parser::parseExprKeyPath() {
618619
return makeParserCodeCompletionResult(keypath);
619620
}
620621

621-
return makeParserResult(keypath);
622+
ParserStatus parseStatus = ParserStatus(rootResult) | ParserStatus(pathResult);
623+
return makeParserResult(parseStatus, keypath);
622624
}
623625

624626
/// expr-keypath-objc:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %target-swift-ide-test -code-completion -code-completion-token=COMPLETE -source-filename=%s
2+
3+
struct SD {
4+
var array: [Int] = []
5+
}
6+
7+
func test(sd: SD) {
8+
_ = sd[\.array[#^COMPLETE^#]]
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: not %target-swift-frontend %s -typecheck
2+
3+
struct SD {
4+
var array: [Int] = []
5+
}
6+
7+
func test(sd: SD) {
8+
_ = sd[\.array[@]]
9+
}

0 commit comments

Comments
 (0)