Skip to content

Commit a431604

Browse files
committed
Generate anonymous class for SAMs defining narrowed overrides
1 parent 004a2fd commit a431604

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

compiler/src/dotty/tools/dotc/transform/ExpandSAMs.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ExpandSAMs extends MiniPhase:
5353
case tpe @ SAMType(_) if tpe.isRef(defn.PartialFunctionClass) =>
5454
val tpe1 = checkRefinements(tpe, fn)
5555
toPartialFunction(tree, tpe1)
56-
case tpe @ SAMType(_) if ExpandSAMs.isPlatformSam(tpe.classSymbol.asClass) =>
56+
case tpe @ SAMType(_) if ExpandSAMs.isPlatformSam(tpe.classSymbol.asClass) && !definesNarrowedOverrides(tpe) =>
5757
checkRefinements(tpe, fn)
5858
tree
5959
case tpe =>
@@ -66,6 +66,12 @@ class ExpandSAMs extends MiniPhase:
6666
tree
6767
}
6868

69+
private def definesNarrowedOverrides(tpe: Type)(using Context): Boolean =
70+
tpe.decls.exists { sym =>
71+
val resultType = sym.info.resultType
72+
sym.allOverriddenSymbols.exists(resultType <:< _.info.resultType)
73+
}
74+
6975
/** A partial function literal:
7076
*
7177
* ```

tests/run/i15402.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
trait Named:
2+
def me: Named
3+
4+
trait Foo extends Named:
5+
def me: Foo = this
6+
def foo(x: String): String
7+
8+
class Names(xs: List[Named]):
9+
def mkString = xs.map(_.me).mkString(",")
10+
11+
object Names:
12+
def single[T <: Named](t: T): Names = Names(List(t))
13+
14+
@main def Test() =
15+
Names.single[Foo](x => x).mkString

0 commit comments

Comments
 (0)