diff --git a/compiler/src/dotty/tools/dotc/transform/ExpandSAMs.scala b/compiler/src/dotty/tools/dotc/transform/ExpandSAMs.scala index 8dba16951c6f..e7466ecc6fcf 100644 --- a/compiler/src/dotty/tools/dotc/transform/ExpandSAMs.scala +++ b/compiler/src/dotty/tools/dotc/transform/ExpandSAMs.scala @@ -53,7 +53,7 @@ class ExpandSAMs extends MiniPhase: case tpe @ SAMType(_) if tpe.isRef(defn.PartialFunctionClass) => val tpe1 = checkRefinements(tpe, fn) toPartialFunction(tree, tpe1) - case tpe @ SAMType(_) if ExpandSAMs.isPlatformSam(tpe.classSymbol.asClass) => + case tpe @ SAMType(_) if ExpandSAMs.isPlatformSam(tpe.classSymbol.asClass) && !definesNarrowedOverrides(tpe) => checkRefinements(tpe, fn) tree case tpe => @@ -66,6 +66,12 @@ class ExpandSAMs extends MiniPhase: tree } + private def definesNarrowedOverrides(tpe: Type)(using Context): Boolean = + tpe.decls.exists { sym => + val resultType = sym.info.resultType + sym.allOverriddenSymbols.exists(resultType <:< _.info.resultType) + } + /** A partial function literal: * * ``` diff --git a/tests/run/i15402.scala b/tests/run/i15402.scala new file mode 100644 index 000000000000..ed747b9e7442 --- /dev/null +++ b/tests/run/i15402.scala @@ -0,0 +1,15 @@ +trait Named: + def me: Named + +trait Foo extends Named: + def me: Foo = this + def foo(x: String): String + +class Names(xs: List[Named]): + def mkString = xs.map(_.me).mkString(",") + +object Names: + def single[T <: Named](t: T): Names = Names(List(t)) + +@main def Test() = + Names.single[Foo](x => x).mkString \ No newline at end of file