Skip to content

[Sema] [CSDiag] Fix incorrect diagnostic when using keypath with tuples #20495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,8 @@ ERROR(expr_string_interpolation_outside_string,none,
"string interpolation can only appear inside a string literal", ())
ERROR(unsupported_keypath_tuple_element_reference,none,
"key path cannot reference tuple elements", ())
ERROR(expr_keypath_unimplemented_tuple,none,
"key path support for tuples is not implemented", ())
ERROR(expr_keypath_subscript_index_not_hashable, none,
"subscript index of type %0 in a key path must be Hashable", (Type))
ERROR(expr_smart_keypath_application_type_mismatch,none,
Expand Down
10 changes: 8 additions & 2 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6686,8 +6686,14 @@ static bool diagnoseKeyPathComponents(ConstraintSystem &CS, KeyPathExpr *KPE,
corrections);

if (currentType)
TC.diagnose(componentNameLoc, diag::could_not_find_type_member,
currentType, componentName);
if (isa<TupleType>(currentType.getPointer())) {
TC.diagnose(KPE->getLoc(), diag::expr_keypath_unimplemented_tuple);
isInvalid = true;
break;
}
else
TC.diagnose(componentNameLoc, diag::could_not_find_type_member,
currentType, componentName);
else
TC.diagnose(componentNameLoc, diag::use_unresolved_identifier,
componentName, false);
Expand Down
10 changes: 10 additions & 0 deletions test/expr/unary/keypath/keypath-unimplemented.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,13 @@ class C {
// rdar://problem/32209039 - Improve diagnostic when unsupported tuple element references are used in key path literals
let _ = \(Int, String).0 // expected-error {{key path cannot reference tuple elements}}
let _ = \(a: Int, b: String).b // expected-error {{key path cannot reference tuple elements}}

struct TupleKeypath {
let labeled: (foo: Int, bar: String)
let unlabeled: (Int, Int)
}

let _: KeyPath<TupleKeypath, Int> = \TupleKeypath.labeled.foo // expected-error {{key path support for tuples is not implemented}}
let _: KeyPath<TupleKeypath, String> = \TupleKeypath.labeled.bar // expected-error {{key path support for tuples is not implemented}}
let _: KeyPath<TupleKeypath, Int> = \TupleKeypath.unlabeled.0 // expected-error {{key path support for tuples is not implemented}}
let _: KeyPath<TupleKeypath, String> = \TupleKeypath.unlabeled.1 // expected-error {{key path support for tuples is not implemented}}