Skip to content

Commit 0a9e960

Browse files
committed
[CSDiagnostics] Don't suggest unwrapping a key path root without an explicit type
A key path without an explicit root cannot be unwrapped since the optionality is inferred from context and we cannot suggest removing that, adding explicit type is not going to change anything and `!` and `?` after the leading dot is not supported.
1 parent 5a6c562 commit 0a9e960

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,10 +1493,10 @@ bool MemberAccessOnOptionalBaseFailure::diagnoseAsError() {
14931493
} else {
14941494
emitDiagnostic(diag::invalid_optional_inferred_keypath_root, baseType,
14951495
Member, unwrappedBaseType);
1496-
emitDiagnostic(diag::optional_key_path_root_base_chain, Member)
1497-
.fixItInsert(sourceRange.End, "?.");
1498-
emitDiagnostic(diag::optional_key_path_root_base_unwrap, Member)
1499-
.fixItInsert(sourceRange.End, "!.");
1496+
// Note that unwrapping fix-its cannot be suggested in this case
1497+
// because neither `.?` nor `.!` can be used to start a key path
1498+
// literal. Suggesting an explicit type here won't work either
1499+
// because contextual root is going to be optional still.
15001500
}
15011501
} else {
15021502
// Check whether or not the base of this optional unwrap is implicit self

test/expr/unary/keypath/keypath.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,16 +1010,10 @@ func testMemberAccessOnOptionalKeyPathComponent() {
10101010
func kp(_: KeyPath<String?, Int>) {}
10111011

10121012
kp(\.count) // expected-error {{key path root inferred as optional type 'String?' must be unwrapped to refer to member 'count' of unwrapped type 'String'}}
1013-
// expected-note@-1 {{chain the optional using '?.' to access unwrapped type member 'count'}} {{8-8=?.}}
1014-
// expected-note@-2 {{unwrap the optional using '!.' to access unwrapped type member 'count'}} {{8-8=!.}}
10151013
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'}}
1016-
// expected-note@-1 {{chain the optional using '?.' to access unwrapped type member 'count'}} {{37-37=?.}}
1017-
// expected-note@-2 {{unwrap the optional using '!.' to access unwrapped type member 'count'}} {{37-37=!.}}
10181014

10191015
let _ : KeyPath<String?, Int> = \.utf8.count
10201016
// expected-error@-1 {{key path root inferred as optional type 'String?' must be unwrapped to refer to member 'utf8' of unwrapped type 'String'}}
1021-
// expected-note@-2 {{chain the optional using '?.' to access unwrapped type member 'utf8'}} {{37-37=?.}}
1022-
// expected-note@-3 {{unwrap the optional using '!.' to access unwrapped type member 'utf8'}} {{37-37=!.}}
10231017
}
10241018

10251019
func testSyntaxErrors() {
@@ -1070,6 +1064,7 @@ func f_56996() {
10701064
// Key-path missing optional crashes compiler: Inactive constraints left over?
10711065
func f_55805() {
10721066
let _: KeyPath<String?, Int?> = \.utf8.count
1067+
// expected-error@-1 {{key path root inferred as optional type 'String?' must be unwrapped to refer to member 'utf8' of unwrapped type 'String'}}
10731068
// expected-error@-2 {{value of optional type 'String.UTF8View?' must be unwrapped to refer to member 'count' of wrapped base type 'String.UTF8View'}}
10741069
// expected-note@-3 {{chain the optional using '?' to access member 'count' only for non-'nil' base values}}
10751070
}

0 commit comments

Comments
 (0)