@@ -1748,6 +1748,17 @@ trait Applications extends Compatibility {
1748
1748
else if sym2.is(Module ) then compareOwner(sym1, cls2)
1749
1749
else 0
1750
1750
1751
+ enum CompareScheme :
1752
+ case Old // Normal specificity test for overloading resolution (where `preferGeneral` is false)
1753
+ // and in mode Scala3-migration when we compare with the old Scala 2 rules.
1754
+
1755
+ case Intermediate // Intermediate rules: better means specialize, but map all type arguments downwards
1756
+ // These are enabled for 3.0-3.4, or if OldImplicitResolution
1757
+ // is specified, and also for all comparisons between old-style implicits,
1758
+
1759
+ case New // New rules: better means generalize, givens (and extensions) always beat implicits
1760
+ end CompareScheme
1761
+
1751
1762
/** Compare two alternatives of an overloaded call or an implicit search.
1752
1763
*
1753
1764
* @param alt1, alt2 Non-overloaded references indicating the two choices
@@ -1774,6 +1785,15 @@ trait Applications extends Compatibility {
1774
1785
*/
1775
1786
def compare (alt1 : TermRef , alt2 : TermRef , preferGeneral : Boolean = false )(using Context ): Int = trace(i " compare( $alt1, $alt2) " , overload) {
1776
1787
record(" resolveOverloaded.compare" )
1788
+ val scheme =
1789
+ val oldResolution = ctx.mode.is(Mode .OldImplicitResolution )
1790
+ if ! preferGeneral || Feature .migrateTo3 && oldResolution then
1791
+ CompareScheme .Old
1792
+ else if Feature .sourceVersion.isAtMost(SourceVersion .`3.4`)
1793
+ || oldResolution
1794
+ || alt1.symbol.is(Implicit ) && alt2.symbol.is(Implicit )
1795
+ then CompareScheme .Intermediate
1796
+ else CompareScheme .New
1777
1797
1778
1798
/** Is alternative `alt1` with type `tp1` as good as alternative
1779
1799
* `alt2` with type `tp2` ?
@@ -1816,15 +1836,15 @@ trait Applications extends Compatibility {
1816
1836
isAsGood(alt1, tp1.instantiate(tparams.map(_.typeRef)), alt2, tp2)
1817
1837
}
1818
1838
case _ => // (3)
1819
- def compareValues (tp1 : Type , tp2 : Type )(using Context ) =
1820
- isAsGoodValueType(tp1, tp2, alt1.symbol.is(Implicit ), alt2.symbol.is( Implicit ) )
1839
+ def compareValues (tp2 : Type )(using Context ) =
1840
+ isAsGoodValueType(tp1, tp2, alt1.symbol.is(Implicit ))
1821
1841
tp2 match
1822
1842
case tp2 : MethodType => true // (3a)
1823
1843
case tp2 : PolyType if tp2.resultType.isInstanceOf [MethodType ] => true // (3a)
1824
1844
case tp2 : PolyType => // (3b)
1825
- explore(compareValues(tp1, instantiateWithTypeVars(tp2)))
1845
+ explore(compareValues(instantiateWithTypeVars(tp2)))
1826
1846
case _ => // 3b)
1827
- compareValues(tp1, tp2)
1847
+ compareValues(tp2)
1828
1848
}
1829
1849
1830
1850
/** Test whether value type `tp1` is as good as value type `tp2`.
@@ -1862,9 +1882,8 @@ trait Applications extends Compatibility {
1862
1882
* Also and only for given resolution: If a compared type refers to a given or its module class, use
1863
1883
* the intersection of its parent classes instead.
1864
1884
*/
1865
- def isAsGoodValueType (tp1 : Type , tp2 : Type , alt1IsImplicit : Boolean , alt2IsImplicit : Boolean )(using Context ): Boolean =
1866
- val oldResolution = ctx.mode.is(Mode .OldImplicitResolution )
1867
- if ! preferGeneral || Feature .migrateTo3 && oldResolution then
1885
+ def isAsGoodValueType (tp1 : Type , tp2 : Type , alt1IsImplicit : Boolean )(using Context ): Boolean =
1886
+ if scheme == CompareScheme .Old then
1868
1887
// Normal specificity test for overloading resolution (where `preferGeneral` is false)
1869
1888
// and in mode Scala3-migration when we compare with the old Scala 2 rules.
1870
1889
isCompatible(tp1, tp2)
@@ -1878,13 +1897,7 @@ trait Applications extends Compatibility {
1878
1897
val tp1p = prepare(tp1)
1879
1898
val tp2p = prepare(tp2)
1880
1899
1881
- if Feature .sourceVersion.isAtMost(SourceVersion .`3.4`)
1882
- || oldResolution
1883
- || alt1IsImplicit && alt2IsImplicit
1884
- then
1885
- // Intermediate rules: better means specialize, but map all type arguments downwards
1886
- // These are enabled for 3.0-3.5, and for all comparisons between old-style implicits,
1887
- // and in 3.5 and 3.6-migration when we compare with previous rules.
1900
+ if scheme == CompareScheme .Intermediate || alt1IsImplicit then
1888
1901
val flip = new TypeMap :
1889
1902
def apply (t : Type ) = t match
1890
1903
case t @ AppliedType (tycon, args) =>
@@ -1895,9 +1908,7 @@ trait Applications extends Compatibility {
1895
1908
case _ => mapOver(t)
1896
1909
(flip(tp1p) relaxed_<:< flip(tp2p)) || viewExists(tp1, tp2)
1897
1910
else
1898
- // New rules: better means generalize, givens (and extensions) always beat implicits
1899
- if alt1IsImplicit != alt2IsImplicit then alt2IsImplicit
1900
- else (tp2p relaxed_<:< tp1p) || viewExists(tp2, tp1)
1911
+ (tp2p relaxed_<:< tp1p) || viewExists(tp2, tp1)
1901
1912
end isAsGoodValueType
1902
1913
1903
1914
/** Widen the result type of synthetic given methods from the implementation class to the
@@ -1968,13 +1979,19 @@ trait Applications extends Compatibility {
1968
1979
// alternatives are the same after following ExprTypes, pick one of them
1969
1980
// (prefer the one that is not a method, but that's arbitrary).
1970
1981
if alt1.widenExpr =:= alt2 then - 1 else 1
1971
- else ownerScore match
1972
- case 1 => if winsType1 || ! winsType2 then 1 else 0
1973
- case - 1 => if winsType2 || ! winsType1 then - 1 else 0
1974
- case 0 =>
1975
- if winsType1 != winsType2 then if winsType1 then 1 else - 1
1976
- else if alt1.symbol == alt2.symbol then comparePrefixes
1977
- else 0
1982
+ else
1983
+ // For new implicit resolution, take ownerscore as more significant than type resolution
1984
+ // Reason: People use owner hierarchies to explicitly prioritize, we should not
1985
+ // break that by changing implicit priority of types.
1986
+ def drawOrOwner =
1987
+ if scheme == CompareScheme .New then ownerScore else 0
1988
+ ownerScore match
1989
+ case 1 => if winsType1 || ! winsType2 then 1 else drawOrOwner
1990
+ case - 1 => if winsType2 || ! winsType1 then - 1 else drawOrOwner
1991
+ case 0 =>
1992
+ if winsType1 != winsType2 then if winsType1 then 1 else - 1
1993
+ else if alt1.symbol == alt2.symbol then comparePrefixes
1994
+ else 0
1978
1995
end compareWithTypes
1979
1996
1980
1997
if alt1.symbol.is(ConstructorProxy ) && ! alt2.symbol.is(ConstructorProxy ) then - 1
0 commit comments