@@ -927,42 +927,44 @@ trait Implicits:
927
927
if currImplicits.outerImplicits == null then currImplicits.refs
928
928
else currImplicits.refs ::: allImplicits(currImplicits.outerImplicits)
929
929
930
- /** Ensure an implicit is not a Scala 2-style implicit conversion, based on its type */
931
- def isScala2Conv (typ : Type ): Boolean = typ match {
932
- case PolyType (_, resType) => isScala2Conv (resType)
930
+ /** Whether the given type is for an implicit def that's a Scala 2 implicit conversion */
931
+ def isImplicitDefConversion (typ : Type ): Boolean = typ match {
932
+ case PolyType (_, resType) => isImplicitDefConversion (resType)
933
933
case mt : MethodType => ! mt.isImplicitMethod && ! mt.isContextualMethod
934
934
case _ => false
935
935
}
936
936
937
- def hasErrors (tree : Tree ): Boolean =
938
- if tree.tpe.isInstanceOf [ErrorType ] then true
939
- else
940
- tree match {
937
+ /** Whether a found implicit be converted to the desired type */
938
+ def canBeConverted (ref : TermRef , expected : Type ): Boolean = {
939
+ // Using Mode.Printing will stop it from printing errors
940
+ val tried = Contexts .withMode(Mode .Printing ) {
941
+ typed(
942
+ tpd.ref(ref).withSpan(arg.span),
943
+ expected,
944
+ ctx.typerState.ownedVars
945
+ )
946
+ }
947
+ val hasErrors =
948
+ if tried.tpe.isInstanceOf [ErrorType ] then true
949
+ else tried match {
941
950
case Apply (_, List (arg)) => arg.tpe.isInstanceOf [ErrorType ]
942
951
case _ => false
943
952
}
953
+ ! hasErrors && expected =:= tried.tpe
954
+ }
944
955
945
956
def ignoredConvertibleImplicits = arg.tpe match
946
957
case fail : SearchFailureType =>
947
958
if (fail.expectedType eq pt) || isFullyDefined(fail.expectedType, ForceDegree .none) then
948
959
// Get every implicit in scope and try to convert each
949
960
allImplicits(ctx.implicits)
950
- .distinctBy(_.underlyingRef.denot)
951
961
.view
952
962
.map(_.underlyingRef)
963
+ .distinctBy(_.denot)
953
964
.filter { imp =>
954
- if isScala2Conv(imp.underlying) || imp.symbol == defn.Predef_conforms then
955
- false
956
- else
957
- // Using Mode.Printing will stop it from printing errors
958
- val tried = Contexts .withMode(Mode .Printing ) {
959
- typed(
960
- tpd.ref(imp).withSpan(arg.span),
961
- fail.expectedType,
962
- ctx.typerState.ownedVars
963
- )
964
- }
965
- ! hasErrors(tried) && fail.expectedType =:= tried.tpe
965
+ ! isImplicitDefConversion(imp.underlying)
966
+ && imp.symbol != defn.Predef_conforms
967
+ && canBeConverted(imp, fail.expectedType)
966
968
}
967
969
else
968
970
Nil
0 commit comments