Skip to content

Commit

Permalink
Elide unit binding when beta-reducing
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Apr 4, 2024
1 parent ece87c3 commit 9b2c23f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/BetaReduce.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import MegaPhase.*
import Symbols.*, Contexts.*, Types.*, Decorators.*
import StdNames.nme
import ast.TreeTypeMap
import Constants.Constant

import scala.collection.mutable.ListBuffer

Expand Down Expand Up @@ -131,6 +132,7 @@ object BetaReduce:
val tpe =
if arg.tpe.isBottomType then param.tpe.widenTermRefExpr
else if arg.tpe.dealias.isInstanceOf[ConstantType] then arg.tpe.dealias
else if arg.tpe.dealias =:= defn.UnitType then ConstantType(Constant(()))
else arg.tpe.widen
val binding = ValDef(newSymbol(ctx.owner, param.name, flags, tpe, coord = arg.span), arg).withSpan(arg.span)
if !(tpe.isInstanceOf[ConstantType] && isPureExpr(arg)) then
Expand Down
20 changes: 20 additions & 0 deletions compiler/test/dotty/tools/backend/jvm/InlineBytecodeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -765,4 +765,24 @@ class InlineBytecodeTests extends DottyBytecodeTest {
diffInstructions(instructions1, instructions2))
}
}

@Test def beta_reduce_elide_unit_binding = {
val source = """class Test:
| def test = ((u: Unit) => u).apply(())
""".stripMargin

checkBCode(source) { dir =>
val clsIn = dir.lookupName("Test.class", directory = false).input
val clsNode = loadClassNode(clsIn)

val fun = getMethod(clsNode, "test")
val instructions = instructionsFromMethod(fun)
val expected = List(Op(RETURN))

assert(instructions == expected,
"`i was not properly beta-reduced in `test`\n" + diffInstructions(instructions, expected))

}
}

}

0 comments on commit 9b2c23f

Please sign in to comment.