Skip to content

Commit ccdbb0e

Browse files
Merge pull request swiftlang#33772 from LucianoPAlmeida/correcting-source-location-keypath-fix
[CSDiagnostics] Ensure fix-it inserts uses correct location in infered key path root optional unwrapped
2 parents 923b1fb + ed7e03b commit ccdbb0e

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
@@ -1047,6 +1047,8 @@ SourceRange MemberAccessOnOptionalBaseFailure::getSourceRange() const {
10471047
if (componentPathElt->getIndex() == 0) {
10481048
if (auto rootType = keyPathExpr->getRootType()) {
10491049
return rootType->getSourceRange();
1050+
} else {
1051+
return keyPathExpr->getComponents().front().getLoc();
10501052
}
10511053
} else {
10521054
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)