@@ -1762,6 +1762,17 @@ trait Applications extends Compatibility {
1762
1762
else if sym2.is(Module ) then compareOwner(sym1, cls2)
1763
1763
else 0
1764
1764
1765
+ enum CompareScheme :
1766
+ case Old // Normal specificity test for overloading resolution (where `preferGeneral` is false)
1767
+ // and in mode Scala3-migration when we compare with the old Scala 2 rules.
1768
+
1769
+ case Intermediate // Intermediate rules: better means specialize, but map all type arguments downwards
1770
+ // These are enabled for 3.0-3.4, or if OldImplicitResolution
1771
+ // is specified, and also for all comparisons between old-style implicits,
1772
+
1773
+ case New // New rules: better means generalize, givens (and extensions) always beat implicits
1774
+ end CompareScheme
1775
+
1765
1776
/** Compare two alternatives of an overloaded call or an implicit search.
1766
1777
*
1767
1778
* @param alt1, alt2 Non-overloaded references indicating the two choices
@@ -1788,6 +1799,15 @@ trait Applications extends Compatibility {
1788
1799
*/
1789
1800
def compare (alt1 : TermRef , alt2 : TermRef , preferGeneral : Boolean = false )(using Context ): Int = trace(i " compare( $alt1, $alt2) " , overload) {
1790
1801
record(" resolveOverloaded.compare" )
1802
+ val scheme =
1803
+ val oldResolution = ctx.mode.is(Mode .OldImplicitResolution )
1804
+ if ! preferGeneral || Feature .migrateTo3 && oldResolution then
1805
+ CompareScheme .Old
1806
+ else if Feature .sourceVersion.isAtMost(SourceVersion .`3.4`)
1807
+ || oldResolution
1808
+ || alt1.symbol.is(Implicit ) && alt2.symbol.is(Implicit )
1809
+ then CompareScheme .Intermediate
1810
+ else CompareScheme .New
1791
1811
1792
1812
/** Is alternative `alt1` with type `tp1` as good as alternative
1793
1813
* `alt2` with type `tp2` ?
@@ -1877,8 +1897,7 @@ trait Applications extends Compatibility {
1877
1897
* the intersection of its parent classes instead.
1878
1898
*/
1879
1899
def isAsGoodValueType (tp1 : Type , tp2 : Type , alt1IsImplicit : Boolean , alt2IsImplicit : Boolean )(using Context ): Boolean =
1880
- val oldResolution = ctx.mode.is(Mode .OldImplicitResolution )
1881
- if ! preferGeneral || Feature .migrateTo3 && oldResolution then
1900
+ if scheme == CompareScheme .Old then
1882
1901
// Normal specificity test for overloading resolution (where `preferGeneral` is false)
1883
1902
// and in mode Scala3-migration when we compare with the old Scala 2 rules.
1884
1903
isCompatible(tp1, tp2)
@@ -1892,13 +1911,7 @@ trait Applications extends Compatibility {
1892
1911
val tp1p = prepare(tp1)
1893
1912
val tp2p = prepare(tp2)
1894
1913
1895
- if Feature .sourceVersion.isAtMost(SourceVersion .`3.4`)
1896
- || oldResolution
1897
- || alt1IsImplicit && alt2IsImplicit
1898
- then
1899
- // Intermediate rules: better means specialize, but map all type arguments downwards
1900
- // These are enabled for 3.0-3.5, and for all comparisons between old-style implicits,
1901
- // and in 3.5 and 3.6-migration when we compare with previous rules.
1914
+ if scheme == CompareScheme .Intermediate then
1902
1915
val flip = new TypeMap :
1903
1916
def apply (t : Type ) = t match
1904
1917
case t @ AppliedType (tycon, args) =>
@@ -1909,7 +1922,6 @@ trait Applications extends Compatibility {
1909
1922
case _ => mapOver(t)
1910
1923
(flip(tp1p) relaxed_<:< flip(tp2p)) || viewExists(tp1, tp2)
1911
1924
else
1912
- // New rules: better means generalize, givens (and extensions) always beat implicits
1913
1925
if alt1IsImplicit != alt2IsImplicit then alt2IsImplicit
1914
1926
else (tp2p relaxed_<:< tp1p) || viewExists(tp2, tp1)
1915
1927
end isAsGoodValueType
@@ -1982,13 +1994,19 @@ trait Applications extends Compatibility {
1982
1994
// alternatives are the same after following ExprTypes, pick one of them
1983
1995
// (prefer the one that is not a method, but that's arbitrary).
1984
1996
if alt1.widenExpr =:= alt2 then - 1 else 1
1985
- else ownerScore match
1986
- case 1 => if winsType1 || ! winsType2 then 1 else 0
1987
- case - 1 => if winsType2 || ! winsType1 then - 1 else 0
1988
- case 0 =>
1989
- if winsType1 != winsType2 then if winsType1 then 1 else - 1
1990
- else if alt1.symbol == alt2.symbol then comparePrefixes
1991
- else 0
1997
+ else
1998
+ // For new implicit resolution, take ownerscore as more significant than type resolution
1999
+ // Reason: People use owner hierarchies to explicitly prioritize, we should not
2000
+ // break that by changing implicit priority of types.
2001
+ def drawOrOwner =
2002
+ if scheme == CompareScheme .New then ownerScore else 0
2003
+ ownerScore match
2004
+ case 1 => if winsType1 || ! winsType2 then 1 else drawOrOwner
2005
+ case - 1 => if winsType2 || ! winsType1 then - 1 else drawOrOwner
2006
+ case 0 =>
2007
+ if winsType1 != winsType2 then if winsType1 then 1 else - 1
2008
+ else if alt1.symbol == alt2.symbol then comparePrefixes
2009
+ else 0
1992
2010
end compareWithTypes
1993
2011
1994
2012
if alt1.symbol.is(ConstructorProxy ) && ! alt2.symbol.is(ConstructorProxy ) then - 1
0 commit comments