Skip to content

Expand lambdas to anonymous classes when LMF cannot implement them#11205

Merged
lrytz merged 2 commits intoscala:2.13.xfrom
lrytz:t13142
Feb 11, 2026
Merged

Expand lambdas to anonymous classes when LMF cannot implement them#11205
lrytz merged 2 commits intoscala:2.13.xfrom
lrytz:t13142

Conversation

@lrytz
Copy link
Copy Markdown
Member

@lrytz lrytz commented Jan 28, 2026

LambdaMetaFactory can adapt / bridge when the parameter / result types of a lambda don't exactly match up with the SAM, for example by introducing a cast or boxing.

We use that functionality, but not in all cases; unboxing null in Scala needs to produce zero, not an NPE. For that reason we manually create an anonfun$adapted bridge in Delambdafy when necessary.

LMF can also generate a bridge methods if needed, for example:

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

LMF does the same adaptations when generating bridges. Here, in case the adaptation by LMF doesn't match the semantics we need for Scala, we have to bail out and emit an anonymous class. This is added in this PR.

Backport of scala/scala3#24624

Fixes scala/bug#13142

lrytz added 2 commits January 28, 2026 21:22
LambdaMetaFactory can adapt / bridge when the parameter / result types
of a lambda don't exactly match up with the SAM, for example by
introducing a cast or boxing.

We use that functionality, but not in all cases; unboxing `null` in
Scala needs to produce zero, not an NPE. For that reason we manually
create an `anonfun$adapted` bridge in Delambdafy when necessary.

LMF can also generate a bridge methods if needed, for example:

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

LMF does the same adaptations when generating bridges. Here, in case
the adaptation by LMF doesn't match the semantics we need for Scala,
we have to bail out and emit an anonymous class. This is added in this
PR.
... written by an agent, of course (copilot / claude-sonnet-4.5),
edited by me.
@scala-jenkins scala-jenkins added this to the 2.13.19 milestone Jan 28, 2026
@lrytz lrytz requested a review from som-snytt January 28, 2026 20:41
@lrytz
Copy link
Copy Markdown
Member Author

lrytz commented Jan 28, 2026

@SethTisue review of AGENTS.md?

@SethTisue
Copy link
Copy Markdown
Member

@SethTisue review of AGENTS.md?

Seems pretty thorough to me, and might be useful to humans as well. We can always add things later.

@lrytz
Copy link
Copy Markdown
Member Author

lrytz commented Feb 11, 2026

@noti0na1 could you review this?

@noti0na1
Copy link
Copy Markdown
Member

Sure!

Copy link
Copy Markdown
Member

@noti0na1 noti0na1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look reasonable to me. Do we have issue about auto-adopting Array with Object in Scala 2?

@noti0na1
Copy link
Copy Markdown
Member

BTW, does the AGENTS.md help? 🤓

@lrytz
Copy link
Copy Markdown
Member Author

lrytz commented Feb 11, 2026

Do we have issue about auto-adopting Array with Object in Scala 2

Your test case I backported passes, so it seems OK. I think it's handled by implParamType <:< samParamType.

BTW, does the AGENTS.md help? 🤓

I just started using the robot recently, but yeah. At least it then knows how to build the project and run a test, since it's not just sbt compile and sbt test. I expect AGENTS.md to evolve. Join the #ai channel on the LAMP Slack!

@lrytz lrytz merged commit 4d415e3 into scala:2.13.x Feb 11, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect LambdaMetaFactory call for SAM type with parent requiring bridge

5 participants