Skip to content

Commit 7685c18

Browse files
authored
Merge pull request #37076 from rintaro/5.5-ide-rdar76714968
[5.5][CodeCompletion] Peform complete `filterSolutions` in code completion
2 parents bebc8b8 + 172358f commit 7685c18

File tree

3 files changed

+8
-47
lines changed

3 files changed

+8
-47
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4874,11 +4874,9 @@ class ConstraintSystem {
48744874
/// \param diff The differences among the solutions.
48754875
/// \param idx1 The index of the first solution.
48764876
/// \param idx2 The index of the second solution.
4877-
/// \param isForCodeCompletion Whether solving for code completion.
48784877
static SolutionCompareResult
48794878
compareSolutions(ConstraintSystem &cs, ArrayRef<Solution> solutions,
4880-
const SolutionDiff &diff, unsigned idx1, unsigned idx2,
4881-
bool isForCodeCompletion);
4879+
const SolutionDiff &diff, unsigned idx1, unsigned idx2);
48824880

48834881
public:
48844882
/// Increase the score of the given kind for the current (partial) solution

lib/Sema/CSRanking.cpp

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -763,48 +763,14 @@ static void addKeyPathDynamicMemberOverloads(
763763
}
764764
}
765765

766-
SolutionCompareResult compareSolutionsForCodeCompletion(
767-
ConstraintSystem &cs, ArrayRef<Solution> solutions, unsigned idx1,
768-
unsigned idx2) {
769-
770-
// When solving for code completion we can't consider one solution worse than
771-
// another according to the same rules as regular compilation. For example,
772-
// with the code below:
773-
//
774-
// func foo(_ x: Int) -> Int {}
775-
// func foo<T>(_ x: T) -> String {}
776-
// foo(3).<complete here> // Still want solutions with for both foo
777-
// // overloads - String and Int members are both
778-
// // valid here.
779-
//
780-
// the comparison for regular compilation considers the solution with the more
781-
// specialized `foo` overload `foo(_: Int)` to be better than the solution
782-
// with the generic overload `foo(_: T)` even though both are otherwise
783-
// viable. For code completion purposes offering members of 'String' based
784-
// on the solution with the generic overload is equally as import as offering
785-
// members of 'Int' as choosing one of those completions will then result in
786-
// regular compilation resolving the call to the generic overload instead.
787-
788-
if (solutions[idx1].getFixedScore() == solutions[idx2].getFixedScore())
789-
return SolutionCompareResult::Incomparable;
790-
return solutions[idx1].getFixedScore() < solutions[idx2].getFixedScore()
791-
? SolutionCompareResult::Better
792-
: SolutionCompareResult::Worse;
793-
}
794-
795-
796766
SolutionCompareResult ConstraintSystem::compareSolutions(
797767
ConstraintSystem &cs, ArrayRef<Solution> solutions,
798-
const SolutionDiff &diff, unsigned idx1, unsigned idx2,
799-
bool isForCodeCompletion) {
768+
const SolutionDiff &diff, unsigned idx1, unsigned idx2) {
800769
if (cs.isDebugMode()) {
801770
llvm::errs().indent(cs.solverState->depth * 2)
802771
<< "comparing solutions " << idx1 << " and " << idx2 <<"\n";
803772
}
804773

805-
if (isForCodeCompletion)
806-
return compareSolutionsForCodeCompletion(cs, solutions, idx1, idx2);
807-
808774
// Whether the solutions are identical.
809775
bool identical = true;
810776

@@ -1340,8 +1306,7 @@ ConstraintSystem::findBestSolution(SmallVectorImpl<Solution> &viable,
13401306
SmallVector<bool, 16> losers(viable.size(), false);
13411307
unsigned bestIdx = 0;
13421308
for (unsigned i = 1, n = viable.size(); i != n; ++i) {
1343-
switch (compareSolutions(*this, viable, diff, i, bestIdx,
1344-
isForCodeCompletion())) {
1309+
switch (compareSolutions(*this, viable, diff, i, bestIdx)) {
13451310
case SolutionCompareResult::Identical:
13461311
// FIXME: Might want to warn about this in debug builds, so we can
13471312
// find a way to eliminate the redundancy in the search space.
@@ -1365,8 +1330,7 @@ ConstraintSystem::findBestSolution(SmallVectorImpl<Solution> &viable,
13651330
if (i == bestIdx)
13661331
continue;
13671332

1368-
switch (compareSolutions(*this, viable, diff, bestIdx, i,
1369-
isForCodeCompletion())) {
1333+
switch (compareSolutions(*this, viable, diff, bestIdx, i)) {
13701334
case SolutionCompareResult::Identical:
13711335
// FIXME: Might want to warn about this in debug builds, so we can
13721336
// find a way to eliminate the redundancy in the search space.
@@ -1418,8 +1382,7 @@ ConstraintSystem::findBestSolution(SmallVectorImpl<Solution> &viable,
14181382
if (losers[j])
14191383
continue;
14201384

1421-
switch (compareSolutions(*this, viable, diff, i, j,
1422-
isForCodeCompletion())) {
1385+
switch (compareSolutions(*this, viable, diff, i, j)) {
14231386
case SolutionCompareResult::Identical:
14241387
// FIXME: Dub one of these the loser arbitrarily?
14251388
break;

test/IDE/complete_ambiguous.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,8 @@ struct Struct123: Equatable {
448448
}
449449
func testBestSolutionFilter() {
450450
let a = Struct123();
451-
let b = [Struct123]().first(where: { $0 == a && 1 + 90 * 5 / 8 == 45 * -10 })?.structMem != .#^BEST_SOLUTION_FILTER^#
452-
let c = min(10.3, 10 / 10.4) < 6 / 7 ? true : Optional(a)?.structMem != .#^BEST_SOLUTION_FILTER2?check=BEST_SOLUTION_FILTER^#
451+
let b = [Struct123]().first(where: { $0 == a && 1 + 90 * 5 / 8 == 45 * -10 })?.structMem != .#^BEST_SOLUTION_FILTER?xfail=rdar73282163^#
452+
let c = min(10.3, 10 / 10.4) < 6 / 7 ? true : Optional(a)?.structMem != .#^BEST_SOLUTION_FILTER2?check=BEST_SOLUTION_FILTER;xfail=rdar73282163^#
453453
}
454454

455455
// BEST_SOLUTION_FILTER: Begin completions
@@ -465,7 +465,7 @@ func testBestSolutionGeneric() {
465465
func genAndInt(_ x: Int) -> Int { return 1 }
466466
func genAndInt<T>(_ x: T) -> Test1 { return Test1() }
467467

468-
genAndInt(2).#^BEST_SOLUTION_FILTER_GEN^#
468+
genAndInt(2).#^BEST_SOLUTION_FILTER_GEN?xfail=rdar73282163^#
469469
}
470470

471471
// BEST_SOLUTION_FILTER_GEN: Begin completions

0 commit comments

Comments
 (0)