Skip to content

Commit 5e8c8f7

Browse files
committed
Don't keep Unit type ascriptions in erasure
They lead to inefficient code: (x: Unit) became (x; BoxedUnit: BoxedUnit) Now it's just `x`. Note: Such type ascriptions come up when inlining a unit-typed non-transparent method. For instance, `assert(p, msg)`.
1 parent 9c4f6f2 commit 5e8c8f7

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -588,16 +588,17 @@ object Erasure {
588588
tree.withType(erasure(tree.tpe))
589589

590590
/** This override is only needed to semi-erase type ascriptions */
591-
override def typedTyped(tree: untpd.Typed, pt: Type)(using Context): Tree = {
591+
override def typedTyped(tree: untpd.Typed, pt: Type)(using Context): Tree =
592592
val Typed(expr, tpt) = tree
593-
val tpt1 = tpt match {
594-
case Block(_, tpt) => tpt // erase type aliases (statements) from type block
595-
case tpt => tpt
596-
}
597-
val tpt2 = typedType(tpt1)
598-
val expr1 = typed(expr, tpt2.tpe)
599-
assignType(untpd.cpy.Typed(tree)(expr1, tpt2), tpt2)
600-
}
593+
if tpt.typeOpt.typeSymbol == defn.UnitClass then
594+
typed(expr, defn.UnitType)
595+
else
596+
val tpt1 = tpt match
597+
case Block(_, tpt) => tpt // erase type aliases (statements) from type block
598+
case tpt => tpt
599+
val tpt2 = typedType(tpt1)
600+
val expr1 = typed(expr, tpt2.tpe)
601+
assignType(untpd.cpy.Typed(tree)(expr1, tpt2), tpt2)
601602

602603
override def typedLiteral(tree: untpd.Literal)(using Context): Tree =
603604
if (tree.typeOpt.isRef(defn.UnitClass))

library/src/dotty/DottyPredef.scala

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@ package dotty
33
object DottyPredef {
44
import compiletime.summonFrom
55

6-
inline final def assert(inline assertion: Boolean, inline message: => Any): Unit = {
7-
if (!assertion)
8-
scala.runtime.Scala3RunTime.assertFailed(message)
9-
}
6+
inline def assert(inline assertion: Boolean, inline message: => Any): Unit =
7+
if !assertion then scala.runtime.Scala3RunTime.assertFailed(message)
108

11-
transparent inline final def assert(inline assertion: Boolean): Unit = {
12-
if (!assertion)
13-
scala.runtime.Scala3RunTime.assertFailed()
14-
}
9+
inline def assert(inline assertion: Boolean): Unit =
10+
if !assertion then scala.runtime.Scala3RunTime.assertFailed()
1511

1612
/**
1713
* Retrieve the single value of a type with a unique inhabitant.

0 commit comments

Comments
 (0)