Skip to content

Commit fd7ba2b

Browse files
committed
Fix #3396: Abort implicit search if result does not match
Don't proceed with implicit search if result type cannot match - the search will likely by under-constrained, which means that an unbounded number of alternatives is tried. See strawman-contrib MapDecoratorTest.scala for an example where this happens.
1 parent 6398448 commit fd7ba2b

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,13 +2150,19 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
21502150

21512151
def adaptNoArgs(wtp: Type): Tree = {
21522152
val ptNorm = underlyingApplied(pt)
2153-
val functionExpected = defn.isFunctionType(ptNorm)
2153+
lazy val functionExpected = defn.isFunctionType(ptNorm)
2154+
lazy val resultMatch = constrainResult(wtp, followAlias(pt))
21542155
wtp match {
21552156
case wtp: ExprType =>
21562157
adaptInterpolated(tree.withType(wtp.resultType), pt)
2157-
case wtp: MethodType
2158-
if wtp.isImplicitMethod && (constrainResult(wtp, followAlias(pt)) || !functionExpected) =>
2159-
adaptNoArgsImplicitMethod(wtp)
2158+
case wtp: MethodType if wtp.isImplicitMethod && (resultMatch || !functionExpected) =>
2159+
if (resultMatch || ctx.mode.is(Mode.ImplicitsEnabled)) adaptNoArgsImplicitMethod(wtp)
2160+
else {
2161+
// Don't proceed with implicit search if result type cannot match - the search
2162+
// will likely by under-constrained, which means that an unbounded number of alternatives
2163+
// is tried. See strawman-contrib MapDecoratorTest.scala for an example where this happens.
2164+
err.typeMismatch(tree, pt)
2165+
}
21602166
case wtp: MethodType if !pt.isInstanceOf[SingletonType] =>
21612167
val arity =
21622168
if (functionExpected)

0 commit comments

Comments
 (0)