Skip to content

Commit 462b293

Browse files
committed
Apply suggestions from code review
1 parent bba0db3 commit 462b293

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,11 +1991,10 @@ trait Applications extends Compatibility {
19911991
// break that by changing implicit priority of types. On the other hand, we do
19921992
// want to exhaust all other possibilities before using owner score as a tie breaker.
19931993
// For instance, pos/scala-uri.scala depends on that.
1994-
def drawOrOwner =
1995-
if preferGeneral && !ctx.mode.is(Mode.OldImplicitResolution) then
1996-
//println(i"disambi compare($alt1, $alt2)? $ownerScore")
1997-
ownerScore
1998-
else 0
1994+
def disambiguateWithOwner(result: Int) =
1995+
if result == 0 && preferGeneral && !ctx.mode.is(Mode.OldImplicitResolution)
1996+
then ownerScore
1997+
else result
19991998

20001999
if alt1.symbol.is(ConstructorProxy) && !alt2.symbol.is(ConstructorProxy) then -1
20012000
else if alt2.symbol.is(ConstructorProxy) && !alt1.symbol.is(ConstructorProxy) then 1
@@ -2005,14 +2004,15 @@ trait Applications extends Compatibility {
20052004
val strippedType1 = stripImplicit(fullType1)
20062005
val strippedType2 = stripImplicit(fullType2)
20072006

2008-
val result = compareWithTypes(strippedType1, strippedType2)
2009-
if result != 0 then result
2010-
else if strippedType1 eq fullType1 then
2007+
var result = compareWithTypes(strippedType1, strippedType2)
2008+
if result != 0 then return result
2009+
if strippedType1 eq fullType1 then
20112010
if strippedType2 eq fullType2
2012-
then drawOrOwner // no implicits either side: its' a draw
2011+
then disambiguateWithOwner(0) // no implicits either side: its' a draw
20132012
else 1 // prefer 1st alternative with no implicits
20142013
else if strippedType2 eq fullType2 then -1 // prefer 2nd alternative with no implicits
2015-
else compareWithTypes(fullType1, fullType2) // continue by comparing implicits parameters
2014+
else disambiguateWithOwner(
2015+
compareWithTypes(fullType1, fullType2)) // continue by comparing implicit parameters
20162016
}
20172017
end compare
20182018

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -549,11 +549,10 @@ object Implicits:
549549
/** An ambiguous implicits failure */
550550
class AmbiguousImplicits(val alt1: SearchSuccess, val alt2: SearchSuccess, val expectedType: Type, val argument: Tree, val nested: Boolean = false) extends SearchFailureType:
551551

552-
private[Implicits] var priorityChangeWarning: Message | Null = null
552+
private[Implicits] var priorityChangeWarnings: List[Message] = Nil
553553

554554
def priorityChangeWarningNote(using Context): String =
555-
if priorityChangeWarning != null then s"\n\nNote: $priorityChangeWarning"
556-
else ""
555+
priorityChangeWarnings.map(msg => s"\n\nNote: $msg").mkString
557556

558557
def msg(using Context): Message =
559558
var str1 = err.refStr(alt1.ref)
@@ -1627,15 +1626,23 @@ trait Implicits:
16271626
throw ex
16281627

16291628
val result = rank(sort(eligible), NoMatchingImplicitsFailure, Nil)
1630-
for (critical, msg) <- priorityChangeWarnings do
1631-
if result.found.exists(critical.contains(_)) then
1632-
result match
1633-
case result: SearchFailure =>
1634-
result.reason match
1635-
case ambi: AmbiguousImplicits => ambi.priorityChangeWarning = msg
1636-
case _ =>
1629+
1630+
// Issue all priority change warnings that can affect the result
1631+
val shownWarnings = priorityChangeWarnings.toList.collect:
1632+
case (critical, msg) if result.found.exists(critical.contains(_)) =>
1633+
msg
1634+
result match
1635+
case result: SearchFailure =>
1636+
result.reason match
1637+
case ambi: AmbiguousImplicits =>
1638+
// Make warnings part of error message because otherwise they are suppressed when
1639+
// the error is emitted.
1640+
ambi.priorityChangeWarnings = shownWarnings
16371641
case _ =>
1638-
report.warning(msg, srcPos)
1642+
case _ =>
1643+
for msg <- shownWarnings do
1644+
report.warning(msg, srcPos)
1645+
16391646
result
16401647
end searchImplicit
16411648

0 commit comments

Comments
 (0)