Skip to content

Commit dda91bb

Browse files
committed
Make code more readable
1 parent 98bf4b2 commit dda91bb

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

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

+22-20
Original file line numberDiff line numberDiff line change
@@ -927,42 +927,44 @@ trait Implicits:
927927
if currImplicits.outerImplicits == null then currImplicits.refs
928928
else currImplicits.refs ::: allImplicits(currImplicits.outerImplicits)
929929

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)
933933
case mt: MethodType => !mt.isImplicitMethod && !mt.isContextualMethod
934934
case _ => false
935935
}
936936

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 {
941950
case Apply(_, List(arg)) => arg.tpe.isInstanceOf[ErrorType]
942951
case _ => false
943952
}
953+
!hasErrors && expected =:= tried.tpe
954+
}
944955

945956
def ignoredConvertibleImplicits = arg.tpe match
946957
case fail: SearchFailureType =>
947958
if (fail.expectedType eq pt) || isFullyDefined(fail.expectedType, ForceDegree.none) then
948959
// Get every implicit in scope and try to convert each
949960
allImplicits(ctx.implicits)
950-
.distinctBy(_.underlyingRef.denot)
951961
.view
952962
.map(_.underlyingRef)
963+
.distinctBy(_.denot)
953964
.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)
966968
}
967969
else
968970
Nil

0 commit comments

Comments
 (0)