@@ -706,7 +706,7 @@ trait Implicits { self: Typer =>
706
706
}
707
707
708
708
/** An implicit search; parameters as in `inferImplicit` */
709
- class ImplicitSearch (protected val pt : Type , protected val argument : Tree , pos : Position )(implicit ctx : Context ) {
709
+ class ImplicitSearch (protected val pt : Type , protected val argument : Tree , pos : Position , contextual : Boolean = true )(implicit ctx : Context ) {
710
710
711
711
private def nestedContext = ctx.fresh.setMode(ctx.mode &~ Mode .ImplicitsEnabled )
712
712
@@ -743,7 +743,7 @@ trait Implicits { self: Typer =>
743
743
def typedImplicit (cand : Candidate )(implicit ctx : Context ): SearchResult = track(" typedImplicit" ) { ctx.traceIndented(i " typed implicit ${cand.ref}, pt = $pt, implicitsEnabled == ${ctx.mode is ImplicitsEnabled }" , implicits, show = true ) {
744
744
assert(constr eq ctx.typerState.constraint)
745
745
val ref = cand.ref
746
- var generated : Tree = tpd.ref(ref).withPos(pos.startPos )
746
+ var generated : Tree = tpd.ref(ref).withPos(pos)
747
747
if (! argument.isEmpty)
748
748
generated = typedUnadapted(
749
749
untpd.Apply (untpd.TypedSplice (generated), untpd.TypedSplice (argument) :: Nil ),
@@ -759,13 +759,19 @@ trait Implicits { self: Typer =>
759
759
case _ => false
760
760
}
761
761
}
762
- // Does there exist an implicit value of type `Eq[tp, tp]`?
763
- def hasEq (tp : Type ): Boolean =
764
- new ImplicitSearch (defn.EqType .appliedTo(tp, tp), EmptyTree , pos).bestImplicit match {
765
- case result : SearchSuccess => result.ref.symbol != defn.Predef_eqAny
766
- case result : AmbiguousImplicits => true
767
- case _ => false
768
- }
762
+ // Does there exist an implicit value of type `Eq[tp, tp]`
763
+ // which is different from `eqAny`?
764
+ def hasEq (tp : Type ): Boolean = {
765
+ def search (contextual : Boolean ): Boolean =
766
+ new ImplicitSearch (defn.EqType .appliedTo(tp, tp), EmptyTree , pos, contextual).bestImplicit match {
767
+ case result : SearchSuccess =>
768
+ result.ref.symbol != defn.Predef_eqAny ||
769
+ contextual && search(contextual = false )
770
+ case result : AmbiguousImplicits => true
771
+ case _ => false
772
+ }
773
+ search(contextual = true )
774
+ }
769
775
770
776
def validEqAnyArgs (tp1 : Type , tp2 : Type ) = {
771
777
List (tp1, tp2).foreach(fullyDefinedType(_, " eqAny argument" , pos))
@@ -873,11 +879,13 @@ trait Implicits { self: Typer =>
873
879
874
880
/** Find a unique best implicit reference */
875
881
def bestImplicit : SearchResult = {
876
- searchImplicits(ctx.implicits.eligible(wildProto), contextual = true ) match {
882
+ searchImplicits(ctx.implicits.eligible(wildProto), contextual) match {
877
883
case result : SearchSuccess => result
878
884
case result : AmbiguousImplicits => result
879
885
case result : SearchFailure =>
880
- searchImplicits(implicitScope(wildProto).eligible, contextual = false )
886
+ if (contextual)
887
+ searchImplicits(implicitScope(wildProto).eligible, contextual = false )
888
+ else result
881
889
}
882
890
}
883
891
0 commit comments