Skip to content

Commit a483d4a

Browse files
committed
Fix Unit ascriptions in erasure
We should simply drop a Unit ascription, not ascribe BoxedUnit instead. This fixes a case in `nullAsInstanceOf.scala`. Now null.asInstanceOf[Unit] is `()`, whereas before it was `null` `()` is correct, or at least it is the same behavior as for all other primitive types.
1 parent fe6c10f commit a483d4a

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
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))

tests/run/nullAsInstanceOf.check

-2 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)