-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make more anonymous functions static (#19251)
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 #19224
- Loading branch information
Showing
2 changed files
with
55 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// scalajs: --skip | ||
|
||
object Test extends App { | ||
val field = 1 | ||
def x(): Int => String = (i: Int) => i.toString | ||
def y(): () => String = () => field.toString | ||
|
||
locally { | ||
assert(x() == x()) // true on Scala 2, was false on Scala 3... | ||
assert(y() == y()) // also true if `y` accesses object-local fields | ||
|
||
def z(): Int => String = (i: Int) => i.toString | ||
assert(z() != z()) // lambdas in constructor are not lifted to static, so no memoization (Scala 2 lifts them, though). | ||
} | ||
|
||
val t1 = new C | ||
val t2 = new C | ||
|
||
locally { | ||
assert(t1.x() == t2.x()) // true on Scala 2, was false on Scala 3... | ||
} | ||
} | ||
class C { | ||
def x(): Int => String = (i: Int) => i.toString | ||
} |