diff --git a/compiler/src/dotty/tools/dotc/typer/Applications.scala b/compiler/src/dotty/tools/dotc/typer/Applications.scala index 48a5738b4074..7961f74035e8 100644 --- a/compiler/src/dotty/tools/dotc/typer/Applications.scala +++ b/compiler/src/dotty/tools/dotc/typer/Applications.scala @@ -635,15 +635,16 @@ trait Applications extends Compatibility { // matches expected type false case argtpe => - def SAMargOK = formal match { - case SAMType(sam) => argtpe <:< sam.toFunctionType(isJava = formal.classSymbol.is(JavaDefined)) - case _ => false - } + def SAMargOK(onlyFunctionalInterface: Boolean) = + (!onlyFunctionalInterface || formal.classSymbol.hasAnnotation(defn.FunctionalInterfaceAnnot)) + && formal.match + case SAMType(sam) => argtpe <:< sam.toFunctionType(isJava = formal.classSymbol.is(JavaDefined)) + case _ => false if argMatch == ArgMatch.SubType then - argtpe relaxed_<:< formal.widenExpr + (argtpe relaxed_<:< formal.widenExpr) || SAMargOK(onlyFunctionalInterface = true) else isCompatible(argtpe, formal) - || ctx.mode.is(Mode.ImplicitsEnabled) && SAMargOK + || ctx.mode.is(Mode.ImplicitsEnabled) && SAMargOK(onlyFunctionalInterface = false) || argMatch == ArgMatch.CompatibleCAP && { val argtpe1 = argtpe.widen @@ -1877,12 +1878,12 @@ trait Applications extends Compatibility { record("resolveOverloaded.FunProto", alts.length) val alts1 = narrowBySize(alts) - //report.log(i"narrowed by size: ${alts1.map(_.symbol.showDcl)}%, %") + overload.println(i"narrowed by size: ${alts1.map(_.symbol.showDcl)}%, %") if isDetermined(alts1) then alts1 else record("resolveOverloaded.narrowedBySize", alts1.length) val alts2 = narrowByShapes(alts1) - //report.log(i"narrowed by shape: ${alts2.map(_.symbol.showDcl)}%, %") + overload.println(i"narrowed by shape: ${alts2.map(_.symbol.showDcl)}%, %") if isDetermined(alts2) then alts2 else record("resolveOverloaded.narrowedByShape", alts2.length) diff --git a/tests/neg/i11899.scala b/tests/neg/i11899.scala new file mode 100644 index 000000000000..3ae8fc496bfa --- /dev/null +++ b/tests/neg/i11899.scala @@ -0,0 +1,10 @@ +import java.util.function.Function + +class Future[T](val initial: T) { + def map[V](v: V): Unit = println(v) + def map[U](fn: Function[T, U]): Unit = println(fn(initial)) +} + +val f = new Future(42) +val fn = (i: Int) => i.toString +def test = f.map(fn) // error diff --git a/tests/run/i11938.scala b/tests/run/i11938.scala new file mode 100644 index 000000000000..1f8acd7fc2fc --- /dev/null +++ b/tests/run/i11938.scala @@ -0,0 +1,15 @@ +import java.util.function.Function + +class Future[T](val initial: T) { + def map[V](v: V): Unit = println(v) + //def map(v: String): Unit = println(v) + def map[U](fn: Function[T, U]): Unit = println(fn(initial)) +} + +object Test { + val f = new Future(42) + def fn(i: Int) = i.toString + def main(args: Array[String]): Unit = + f.map((i: Int) => i.toString) + f.map(fn) +} \ No newline at end of file