Skip to content

Commit 867a370

Browse files
committed
Weigh ownerScore more than type score for implicit search
1 parent 6688751 commit 867a370

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

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

+23-17
Original file line numberDiff line numberDiff line change
@@ -1970,23 +1970,29 @@ trait Applications extends Compatibility {
19701970

19711971
def compareWithTypes(tp1: Type, tp2: Type) =
19721972
val ownerScore = compareOwner(alt1.symbol.maybeOwner, alt2.symbol.maybeOwner)
1973-
val winsType1 = isAsGood(alt1, tp1, alt2, tp2)
1974-
val winsType2 = isAsGood(alt2, tp2, alt1, tp1)
1975-
1976-
overload.println(i"compare($alt1, $alt2)? $tp1 $tp2 $ownerScore $winsType1 $winsType2")
1977-
if winsType1 && winsType2
1978-
&& alt1.widenExpr.isStable && (alt1.widenExpr frozen_=:= alt2.widenExpr)
1979-
then
1980-
// alternatives are the same after following ExprTypes, pick one of them
1981-
// (prefer the one that is not a method, but that's arbitrary).
1982-
if alt1.widenExpr =:= alt2 then -1 else 1
1983-
else ownerScore match
1984-
case 1 => if winsType1 || !winsType2 then 1 else 0
1985-
case -1 => if winsType2 || !winsType1 then -1 else 0
1986-
case 0 =>
1987-
if winsType1 != winsType2 then if winsType1 then 1 else -1
1988-
else if alt1.symbol == alt2.symbol then comparePrefixes
1989-
else 0
1973+
if preferGeneral && ownerScore != 0 then
1974+
// For implicit resolution, take ownerscre as more significat than type resoltion
1975+
// Reason: People use owner hierarchies to explicitly prioritize, we should not
1976+
// break that by changing implicit priority of types.
1977+
ownerScore
1978+
else
1979+
val winsType1 = isAsGood(alt1, tp1, alt2, tp2)
1980+
val winsType2 = isAsGood(alt2, tp2, alt1, tp1)
1981+
1982+
overload.println(i"compare($alt1, $alt2)? $tp1 $tp2 $ownerScore $winsType1 $winsType2")
1983+
if winsType1 && winsType2
1984+
&& alt1.widenExpr.isStable && (alt1.widenExpr frozen_=:= alt2.widenExpr)
1985+
then
1986+
// alternatives are the same after following ExprTypes, pick one of them
1987+
// (prefer the one that is not a method, but that's arbitrary).
1988+
if alt1.widenExpr =:= alt2 then -1 else 1
1989+
else ownerScore match
1990+
case 1 => if winsType1 || !winsType2 then 1 else 0
1991+
case -1 => if winsType2 || !winsType1 then -1 else 0
1992+
case 0 =>
1993+
if winsType1 != winsType2 then if winsType1 then 1 else -1
1994+
else if alt1.symbol == alt2.symbol then comparePrefixes
1995+
else 0
19901996
end compareWithTypes
19911997

19921998
if alt1.symbol.is(ConstructorProxy) && !alt2.symbol.is(ConstructorProxy) then -1

0 commit comments

Comments
 (0)