diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 554380596567..0f240f31a205 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -1099,16 +1099,20 @@ class Typer extends Namer pt match { case SAMType(sam) if !defn.isFunctionType(pt) && mt <:< sam => + // SAMs of the form C[?] where C is a class cannot be conversion targets. + // The resulting class `class $anon extends C[?] {...}` would be illegal, + // since type arguments to `C`'s super constructor cannot be constructed. + def isWildcardClassSAM = + !pt.classSymbol.is(Trait) && pt.argInfos.exists(_.isInstanceOf[TypeBounds]) val targetTpe = - if (!isFullyDefined(pt, ForceDegree.all)) - if (pt.isRef(defn.PartialFunctionClass)) - // Replace the underspecified expected type by one based on the closure method type - defn.PartialFunctionOf(mt.firstParamTypes.head, mt.resultType) - else { - ctx.error(ex"result type of lambda is an underspecified SAM type $pt", tree.sourcePos) - pt - } - else pt + if isFullyDefined(pt, ForceDegree.all) && !isWildcardClassSAM then + pt + else if pt.isRef(defn.PartialFunctionClass) then + // Replace the underspecified expected type by one based on the closure method type + defn.PartialFunctionOf(mt.firstParamTypes.head, mt.resultType) + else + ctx.error(ex"result type of lambda is an underspecified SAM type $pt", tree.sourcePos) + pt if (pt.classSymbol.isOneOf(FinalOrSealed)) { val offendingFlag = pt.classSymbol.flags & FinalOrSealed ctx.error(ex"lambda cannot implement $offendingFlag ${pt.classSymbol}", tree.sourcePos) diff --git a/tests/neg/i8012.scala b/tests/neg/i8012.scala new file mode 100644 index 000000000000..01171fd3f80c --- /dev/null +++ b/tests/neg/i8012.scala @@ -0,0 +1,13 @@ + + +@FunctionalInterface +abstract class Q[A] { + def apply(a: A): Int +} + +class C extends Q[?] // error: Type argument must be fully defined + +object O { + def m(i: Int): Int = i + val x: Q[_] = m // error: result type of lambda is an underspecified SAM type Q[?] +} \ No newline at end of file