Skip to content

Commit db1b09a

Browse files
committed
Simplify control flow in rank method
1 parent 0da21e1 commit db1b09a

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

compiler/src/dotty/tools/dotc/typer/Implicits.scala

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -916,49 +916,49 @@ trait Implicits { self: Typer =>
916916
* treated as a simple failure, with a warning that semantics will change.
917917
* - otherwise add the failure to `rfailures` and continue testing the other candidates.
918918
*/
919-
def rank(pending: List[Candidate], found: SearchResult, rfailures: List[SearchFailure]): SearchResult = {
920-
def recur(result: SearchResult, remaining: List[Candidate], isNot: Boolean): SearchResult = result match {
921-
case fail: SearchFailure =>
922-
if (isNot)
923-
recur(
924-
SearchSuccess(ref(defn.Not_value), defn.Not_value.termRef, 0)(
925-
ctx.typerState.fresh().setCommittable(true)),
926-
remaining, false)
927-
else if (fail.isAmbiguous)
928-
if (ctx.scala2Mode) {
929-
val result = rank(remaining, found, NoMatchingImplicitsFailure :: rfailures)
930-
if (result.isSuccess)
931-
warnAmbiguousNegation(fail.reason.asInstanceOf[AmbiguousImplicits])
932-
result
933-
}
934-
else
935-
healAmbiguous(remaining, fail)
936-
else
937-
rank(remaining, found, fail :: rfailures)
938-
case best: SearchSuccess =>
939-
if (isNot)
940-
recur(NoMatchingImplicitsFailure, remaining, false)
941-
else if (ctx.mode.is(Mode.ImplicitExploration) || isCoherent)
942-
best
943-
else disambiguate(found, best) match {
944-
case retained: SearchSuccess =>
945-
val newPending =
946-
if (retained eq found) remaining
947-
else remaining.filter(cand =>
948-
compareCandidate(retained, cand.ref, cand.level) <= 0)
949-
rank(newPending, retained, rfailures)
919+
def rank(pending: List[Candidate], found: SearchResult, rfailures: List[SearchFailure]): SearchResult =
920+
pending match {
921+
case cand :: remaining =>
922+
negateIfNot(tryImplicit(cand)) match {
950923
case fail: SearchFailure =>
951-
healAmbiguous(remaining, fail)
924+
if (fail.isAmbiguous)
925+
if (ctx.scala2Mode) {
926+
val result = rank(remaining, found, NoMatchingImplicitsFailure :: rfailures)
927+
if (result.isSuccess)
928+
warnAmbiguousNegation(fail.reason.asInstanceOf[AmbiguousImplicits])
929+
result
930+
}
931+
else healAmbiguous(remaining, fail)
932+
else rank(remaining, found, fail :: rfailures)
933+
case best: SearchSuccess =>
934+
if (ctx.mode.is(Mode.ImplicitExploration) || isCoherent)
935+
best
936+
else disambiguate(found, best) match {
937+
case retained: SearchSuccess =>
938+
val newPending =
939+
if (retained eq found) remaining
940+
else remaining.filter(cand =>
941+
compareCandidate(retained, cand.ref, cand.level) <= 0)
942+
rank(newPending, retained, rfailures)
943+
case fail: SearchFailure =>
944+
healAmbiguous(remaining, fail)
945+
}
952946
}
953-
}
954-
pending match {
955-
case cand :: pending1 =>
956-
recur(tryImplicit(cand), pending1, this.isNot)
957947
case nil =>
958948
if (rfailures.isEmpty) found
959949
else found.recoverWith(_ => rfailures.reverse.maxBy(_.tree.treeSize))
960950
}
961-
}
951+
952+
def negateIfNot(result: SearchResult) =
953+
if (isNot)
954+
result match {
955+
case _: SearchFailure =>
956+
SearchSuccess(ref(defn.Not_value), defn.Not_value.termRef, 0)(
957+
ctx.typerState.fresh().setCommittable(true))
958+
case _: SearchSuccess =>
959+
NoMatchingImplicitsFailure
960+
}
961+
else result
962962

963963
def warnAmbiguousNegation(ambi: AmbiguousImplicits) =
964964
ctx.migrationWarning(

0 commit comments

Comments
 (0)