Skip to content

Commit c0c6052

Browse files
authored
Merge pull request #20495 from theblixguy/fix/SR-9213
[Sema] [CSDiag] Fixes incorrect diagnostic when using keypath with tuples
2 parents 44e0f44 + 21f97a5 commit c0c6052

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,8 @@ ERROR(expr_string_interpolation_outside_string,none,
510510
"string interpolation can only appear inside a string literal", ())
511511
ERROR(unsupported_keypath_tuple_element_reference,none,
512512
"key path cannot reference tuple elements", ())
513+
ERROR(expr_keypath_unimplemented_tuple,none,
514+
"key path support for tuples is not implemented", ())
513515
ERROR(expr_keypath_subscript_index_not_hashable, none,
514516
"subscript index of type %0 in a key path must be Hashable", (Type))
515517
ERROR(expr_smart_keypath_application_type_mismatch,none,

lib/Sema/CSDiag.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6686,8 +6686,14 @@ static bool diagnoseKeyPathComponents(ConstraintSystem &CS, KeyPathExpr *KPE,
66866686
corrections);
66876687

66886688
if (currentType)
6689-
TC.diagnose(componentNameLoc, diag::could_not_find_type_member,
6690-
currentType, componentName);
6689+
if (isa<TupleType>(currentType.getPointer())) {
6690+
TC.diagnose(KPE->getLoc(), diag::expr_keypath_unimplemented_tuple);
6691+
isInvalid = true;
6692+
break;
6693+
}
6694+
else
6695+
TC.diagnose(componentNameLoc, diag::could_not_find_type_member,
6696+
currentType, componentName);
66916697
else
66926698
TC.diagnose(componentNameLoc, diag::use_unresolved_identifier,
66936699
componentName, false);

test/expr/unary/keypath/keypath-unimplemented.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,13 @@ class C {
1212
// rdar://problem/32209039 - Improve diagnostic when unsupported tuple element references are used in key path literals
1313
let _ = \(Int, String).0 // expected-error {{key path cannot reference tuple elements}}
1414
let _ = \(a: Int, b: String).b // expected-error {{key path cannot reference tuple elements}}
15+
16+
struct TupleKeypath {
17+
let labeled: (foo: Int, bar: String)
18+
let unlabeled: (Int, Int)
19+
}
20+
21+
let _: KeyPath<TupleKeypath, Int> = \TupleKeypath.labeled.foo // expected-error {{key path support for tuples is not implemented}}
22+
let _: KeyPath<TupleKeypath, String> = \TupleKeypath.labeled.bar // expected-error {{key path support for tuples is not implemented}}
23+
let _: KeyPath<TupleKeypath, Int> = \TupleKeypath.unlabeled.0 // expected-error {{key path support for tuples is not implemented}}
24+
let _: KeyPath<TupleKeypath, String> = \TupleKeypath.unlabeled.1 // expected-error {{key path support for tuples is not implemented}}

0 commit comments

Comments
 (0)