Skip to content

[CSRanking] Detect cases where overload choices are incomparable #29936

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
Feb 20, 2020
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
15 changes: 15 additions & 0 deletions lib/Sema/CSRanking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,21 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
if (sameOverloadChoice(choice1, choice2))
continue;

// If constraint system is underconstrained e.g. because there are
// editor placeholders, it's possible to end up with multiple solutions
// where each ambiguous declaration is going to have its own overload kind:
//
// func foo(_: Int) -> [Int] { ... }
// func foo(_: Double) -> (result: String, count: Int) { ... }
//
// _ = foo(<#arg#>).count
//
// In this case solver would produce 2 solutions: one where `count`
// is a property reference on `[Int]` and another one is tuple access
// for a `count:` element.
if (choice1.isDecl() != choice2.isDecl())
return SolutionCompareResult::Incomparable;

auto decl1 = choice1.getDecl();
auto dc1 = decl1->getDeclContext();
auto decl2 = choice2.getDecl();
Expand Down
7 changes: 7 additions & 0 deletions test/Sema/editor_placeholders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ f(<#T##String#>) // expected-error{{editor placeholder in source file}} expected
for x in <#T#> { // expected-error{{editor placeholder in source file}} expected-error{{for-in loop requires '()' to conform to 'Sequence'}}

}

// rdar://problem/49712598 - crash while trying to rank solutions with different kinds of overloads
func test_ambiguity_with_placeholders(pairs: [(rank: Int, count: Int)]) -> Bool {
return pairs[<#^ARG^#>].count == 2
// expected-error@-1 {{editor placeholder in source file}}
// expected-error@-2 {{ambiguous use of 'subscript(_:)'}}
}