Skip to content

Commit 8e63cb4

Browse files
authored
Merge pull request #15501 from dotty-staging/backport-15498
Backport #15498: Fix #15494: Handle non-specialized functions in EtaReduce.
2 parents 16f5c25 + 2f60265 commit 8e63cb4

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

compiler/src/dotty/tools/dotc/transform/EtaReduce.scala

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ class EtaReduce extends MiniPhase:
4848
fn
4949
case TypeApply(Select(qual, _), _) if rhs.symbol.isTypeCast && rhs.span.isSynthetic =>
5050
tryReduce(mdef, qual)
51+
case Apply(_, arg :: Nil) if Erasure.Boxing.isUnbox(rhs.symbol) && rhs.span.isSynthetic =>
52+
tryReduce(mdef, arg)
53+
case Block(call :: Nil, unit @ Literal(Constants.Constant(()))) if unit.span.isSynthetic =>
54+
tryReduce(mdef, call)
5155
case _ =>
5256
tree
5357

tests/sjs-junit/test/org/scalajs/testsuite/compiler/RegressionTestScala3.scala

+19
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,25 @@ class RegressionTestScala3 {
5959
assertEquals(0L, Foo.bar().x)
6060
assertEquals(5L, Foo.bar(5L).x)
6161
}
62+
63+
@Test def etaReduceIssue15494(): Unit = {
64+
// Also known as tests/run/i14623.scala for Scala/JVM
65+
66+
object Thunk {
67+
private[this] val impl =
68+
((x: Any) => x).asInstanceOf[(=> Any) => Function0[Any]]
69+
70+
def asFunction0[A](thunk: => A): Function0[A] = impl(thunk).asInstanceOf[Function0[A]]
71+
}
72+
73+
var i = 0
74+
val f1 = { () => i += 1; "" }
75+
assertSame(f1, Thunk.asFunction0(f1()))
76+
val f2 = { () => i += 1; i }
77+
assertSame(f2, Thunk.asFunction0(f2()))
78+
val f3 = { () => i += 1 }
79+
assertSame(f3, Thunk.asFunction0(f3()))
80+
}
6281
}
6382

6483
object RegressionTestScala3 {

0 commit comments

Comments
 (0)