bug icon indicating copy to clipboard operation
bug copied to clipboard

Incorrect LambdaMetaFactory call for SAM type with parent requiring bridge

Open lrytz opened this issue 1 month ago • 2 comments

From https://github.com/scala/scala3/issues/24573#issuecomment-3601905869

trait F1[-T, +R] {
  def apply(t: T): R
}

trait SF[-T] extends F1[T, Unit] { def apply(t: T): Unit }

object T {
  def f: SF[String] = (x: String) => println(x)
  def main(): Unit = f("hai")
}

crashes at run-time with

Exception in thread "main" java.lang.BootstrapMethodError: bootstrap method initialization exception
	at java.base/java.lang.invoke.BootstrapMethodInvoker.invoke(BootstrapMethodInvoker.java:187)
	at java.base/java.lang.invoke.CallSite.makeSite(CallSite.java:310)
	at java.base/java.lang.invoke.MethodHandleNatives.linkCallSiteImpl(MethodHandleNatives.java:250)
	at java.base/java.lang.invoke.MethodHandleNatives.linkCallSite(MethodHandleNatives.java:240)
	at T$.f(T.scala:8)
	at T$.main(T.scala:9)
	at T.main(T.scala)
Caused by: java.lang.invoke.LambdaConversionException: Type mismatch for lambda expected return: void is not convertible to class java.lang.Object
	at java.base/java.lang.invoke.AbstractValidatingLambdaMetafactory.checkDescriptor(AbstractValidatingLambdaMetafactory.java:333)
	at java.base/java.lang.invoke.AbstractValidatingLambdaMetafactory.validateMetafactoryArgs(AbstractValidatingLambdaMetafactory.java:312)
	at java.base/java.lang.invoke.LambdaMetafactory.altMetafactory(LambdaMetafactory.java:545)
	at java.base/java.lang.invoke.BootstrapMethodInvoker.invoke(BootstrapMethodInvoker.java:143)
	... 6 more

lrytz avatar Dec 02 '25 14:12 lrytz

Hi @lrytz , can you point out some places in Scala 2 where we expand SAM and generate possible bridges (and other related code)? I want to compare and double-check the behaviour with Scala 3.

Thanks!

noti0na1 avatar Dec 03 '25 12:12 noti0na1

https://github.com/scala/scala/blob/41f6cfcd4b05298c59fc375c27b21eaea8e09653/src/compiler/scala/tools/nsc/transform/Delambdafy.scala#L115-L122

(also, feel free to reach out on the LAMP slack)

lrytz avatar Dec 03 '25 13:12 lrytz