Skip to content

Commit 6e4c3b7

Browse files
committed
Make eqAny a member of Eq
Was Predef, but that does not work because that way we cannot always determine whether a class has an Eq instance by an implicit search. The eqAny in Predef was found by a contextual search, so we never looked for another Eq by a search of the implicit scope. Making eqAny a member of Eq avoids this problem.
1 parent 8b0aca2 commit 6e4c3b7

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,6 @@ class Definitions {
347347

348348
def DottyPredefModule(implicit ctx: Context) = DottyPredefModuleRef.symbol
349349

350-
def Predef_eqAny(implicit ctx: Context) = DottyPredefModule.requiredMethod(nme.eqAny)
351350
lazy val Predef_ImplicitConverterR = DottyPredefModule.requiredClass("ImplicitConverter").typeRef
352351
def Predef_ImplicitConverter(implicit ctx: Context) = Predef_ImplicitConverterR.symbol
353352

@@ -547,6 +546,9 @@ class Definitions {
547546

548547
lazy val EqType = ctx.requiredClassRef("scala.Eq")
549548
def EqClass(implicit ctx: Context) = EqType.symbol.asClass
549+
def EqModule(implicit ctx: Context) = EqClass.companionModule
550+
551+
def Eq_eqAny(implicit ctx: Context) = EqModule.requiredMethod(nme.eqAny)
550552

551553
lazy val XMLTopScopeModuleRef = ctx.requiredModuleRef("scala.xml.TopScope")
552554

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ trait Implicits { self: Typer =>
762762
// Does there exist an implicit value of type `Eq[tp, tp]`?
763763
def hasEq(tp: Type): Boolean =
764764
new ImplicitSearch(defn.EqType.appliedTo(tp, tp), EmptyTree, pos).bestImplicit match {
765-
case result: SearchSuccess => result.ref.symbol != defn.Predef_eqAny
765+
case result: SearchSuccess => result.ref.symbol != defn.Eq_eqAny
766766
case result: AmbiguousImplicits => true
767767
case _ => false
768768
}
@@ -781,7 +781,7 @@ trait Implicits { self: Typer =>
781781
}
782782
else generated1 match {
783783
case TypeApply(fn, targs @ (arg1 :: arg2 :: Nil))
784-
if fn.symbol == defn.Predef_eqAny && !validEqAnyArgs(arg1.tpe, arg2.tpe) =>
784+
if fn.symbol == defn.Eq_eqAny && !validEqAnyArgs(arg1.tpe, arg2.tpe) =>
785785
nonMatchingImplicit(ref, Nil)
786786
case _ =>
787787
SearchSuccess(generated1, ref, cand.level, ctx.typerState)

library/src/dotty/DottyPredef.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ object DottyPredef {
1212
* Eq-free. A type `S` is Eq-free if there is no implicit instance of `Eq[S, S]`.
1313
* An implicit search will fail instead of returning an invalid `eqAny` instance.
1414
*/
15-
implicit def eqAny[L, R]: Eq[L, R] = Eq
16-
1715
implicit def eqNumber : Eq[Number, Number] = Eq
1816
implicit def eqString : Eq[String, String] = Eq
1917

library/src/scala/Eq.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ sealed trait Eq[-L, -R]
1010
* can also be used as a value that's compatible with
1111
* any instance of `Eq`.
1212
*/
13-
object Eq extends Eq[Any, Any]
14-
13+
object Eq extends Eq[Any, Any] {
14+
implicit def eqAny[L, R]: Eq[L, R] = this
15+
}

0 commit comments

Comments
 (0)