Skip to content

Commit b1971b4

Browse files
Add safety checks to samMethod
`sam` in `samMethod` stands for "single abstract method". However, presently, no check is performed to verify that the abstract method it finds is indeed single. This commit adds such a check to this method.
1 parent 574fd0c commit b1971b4

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

compiler/src/dotty/tools/backend/jvm/DottyBackendInterface.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -853,8 +853,14 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
853853

854854
def addRemoteRemoteExceptionAnnotation: Unit = ()
855855

856-
def samMethod(): Symbol =
857-
toDenot(sym).info.abstractTermMembers.headOption.getOrElse(toDenot(sym).info.member(nme.apply)).symbol
856+
def samMethod(): Symbol = ctx.atPhase(ctx.erasurePhase) { implicit ctx =>
857+
toDenot(sym).info.abstractTermMembers.toList match {
858+
case x :: Nil => x.symbol
859+
case Nil => abort(s"${sym.show} is not a functional interface. It doesn't have abstract methods")
860+
case xs => abort(s"${sym.show} is not a functional interface. " +
861+
s"It has the following abstract methods: ${xs.map(_.name).mkString(", ")}")
862+
}
863+
}
858864

859865
def isFunctionClass: Boolean =
860866
defn.isFunctionClass(sym)

0 commit comments

Comments
 (0)