Skip to content

Commit

Permalink
Make more anonymous functions static
Browse files Browse the repository at this point in the history
An anonymous function in a static object was previously mapped to a member
of that object. We now map it to a static member of the toplevel class instead.
This causes the backend to memoize the function, which fixes #19224. On the
other hand, we don't do that for anonymous functions nested in the object
constructor, since that can cause deadlocks (see run/deadlock.scala).

Scala 2's behavior is different: it does lift lambdas in constructors
to be static, too, which can cause deadlocks.

Fixes #19924
  • Loading branch information
odersky committed Dec 12, 2023
1 parent 4e6de64 commit 01a12df
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions tests/run/i19224.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ object Test extends App:
def x(): Int => String = (i: Int) => i.toString
def y(): () => String = () => field.toString
locally:
assert(x() == x()) // was true on Scala 2, false on Scala 3...
assert(x() == x()) // true on Scala 2, was false on Scala 3...
assert(y() == y()) // also true if `y` accesses object-local fields


//@main def Test = App()

0 comments on commit 01a12df

Please sign in to comment.