Skip to content

Commit ed7e03b

Browse files
[CSDiagnostics] Ensure fix-it inserts uses correct location in infered key path root optional unwrapped
1 parent b1db77a commit ed7e03b

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,8 @@ SourceRange MemberAccessOnOptionalBaseFailure::getSourceRange() const {
10421042
if (componentPathElt->getIndex() == 0) {
10431043
if (auto rootType = keyPathExpr->getRootType()) {
10441044
return rootType->getSourceRange();
1045+
} else {
1046+
return keyPathExpr->getComponents().front().getLoc();
10451047
}
10461048
} else {
10471049
auto componentIdx = componentPathElt->getIndex() - 1;

test/expr/unary/keypath/keypath.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,11 @@ func testMemberAccessOnOptionalKeyPathComponent() {
10121012
let _ : KeyPath<String?, Int> = \.count // expected-error {{key path root inferred as optional type 'String?' must be unwrapped to refer to member 'count' of unwrapped type 'String'}}
10131013
// expected-note@-1 {{chain the optional using '?.' to access unwrapped type member 'count'}} {{37-37=?.}}
10141014
// expected-note@-2 {{unwrap the optional using '!.' to access unwrapped type member 'count'}} {{37-37=!.}}
1015+
1016+
let _ : KeyPath<String?, Int> = \.utf8.count
1017+
// expected-error@-1 {{key path root inferred as optional type 'String?' must be unwrapped to refer to member 'utf8' of unwrapped type 'String'}}
1018+
// expected-note@-2 {{chain the optional using '?.' to access unwrapped type member 'utf8'}} {{37-37=?.}}
1019+
// expected-note@-3 {{unwrap the optional using '!.' to access unwrapped type member 'utf8'}} {{37-37=!.}}
10151020
}
10161021

10171022
func testSyntaxErrors() {

0 commit comments

Comments
 (0)