Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/EtaReduce.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class EtaReduce extends MiniPhase:
fn
case TypeApply(Select(qual, _), _) if rhs.symbol.isTypeCast && rhs.span.isSynthetic =>
tryReduce(mdef, qual)
case Apply(_, arg :: Nil) if Erasure.Boxing.isUnbox(rhs.symbol) && rhs.span.isSynthetic =>
tryReduce(mdef, arg)
case Block(call :: Nil, unit @ Literal(Constants.Constant(()))) if unit.span.isSynthetic =>
tryReduce(mdef, call)
case _ =>
tree

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ class RegressionTestScala3 {
assertEquals(0L, Foo.bar().x)
assertEquals(5L, Foo.bar(5L).x)
}

@Test def etaReduceIssue15494(): Unit = {
// Also known as tests/run/i14623.scala for Scala/JVM

object Thunk {
private[this] val impl =
((x: Any) => x).asInstanceOf[(=> Any) => Function0[Any]]

def asFunction0[A](thunk: => A): Function0[A] = impl(thunk).asInstanceOf[Function0[A]]
}

var i = 0
val f1 = { () => i += 1; "" }
assertSame(f1, Thunk.asFunction0(f1()))
val f2 = { () => i += 1; i }
assertSame(f2, Thunk.asFunction0(f2()))
val f3 = { () => i += 1 }
assertSame(f3, Thunk.asFunction0(f3()))
}
}

object RegressionTestScala3 {
Expand Down