Skip to content

[Diagnostics] Upon keypath result contextual mismatch try to match ob… #28770

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 1 commit into from
Dec 13, 2019
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
7 changes: 7 additions & 0 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2842,6 +2842,13 @@ bool ConstraintSystem::repairFailures(
if (fnType && fnType->getResult()->isEqual(rhs))
return true;

auto lastComponentType = lhs->lookThroughAllOptionalTypes();
auto keyPathResultType = rhs->lookThroughAllOptionalTypes();

// Propagate contextual information from/to keypath result type.
(void)matchTypes(lastComponentType, keyPathResultType, matchKind,
TMF_ApplyingFix, getConstraintLocator(locator));

conversionsOrFixes.push_back(IgnoreContextualType::create(
*this, lhs, rhs, getConstraintLocator(locator)));
return true;
Expand Down
13 changes: 13 additions & 0 deletions test/Constraints/keypath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,16 @@ func rdar56131416() {
// This type should be selected correctly.
Rdar56131416.takesCorrectType(result)
}

func test_mismatch_with_contextual_optional_result() {
struct A<T> {
init<U: Collection>(_ data: T, keyPath: KeyPath<T, U?>) {}
}

struct B {
var arr: [Int] = []
}

let _ = A(B(), keyPath: \.arr)
// expected-error@-1 {{key path value type '[Int]' cannot be converted to contextual type '[Int]?'}}
}