Skip to content

Commit 542b15c

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 76ce64f commit 542b15c

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
@@ -841,8 +841,14 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
841841

842842
def addRemoteRemoteExceptionAnnotation: Unit = ()
843843

844-
def samMethod(): Symbol =
845-
toDenot(sym).info.abstractTermMembers.headOption.getOrElse(toDenot(sym).info.member(nme.apply)).symbol
844+
def samMethod(): Symbol = ctx.atPhase(ctx.erasurePhase) { implicit ctx =>
845+
toDenot(sym).info.abstractTermMembers.toList match {
846+
case x :: Nil => x.symbol
847+
case Nil => abort(s"${sym.show} is not a functional interface. It doesn't have abstract methods")
848+
case xs => abort(s"${sym.show} is not a functional interface. " +
849+
s"It has the following abstract methods: ${xs.map(_.name).mkString(", ")}")
850+
}
851+
}
846852

847853
def isFunctionClass: Boolean =
848854
defn.isFunctionClass(sym)

0 commit comments

Comments
 (0)