Skip to content

Commit bfd5c89

Browse files
Merge pull request #6231 from dotty-staging/fix/i5791
Fix #5791: let t.asInstanceOf[Unit] always succeed
2 parents 40e4ff4 + 8af3d3e commit bfd5c89

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ object TypeTestsCasts {
234234
def testCls = testType.widen.classSymbol
235235
if (expr.tpe <:< testType)
236236
Typed(expr, tree.args.head)
237+
else if (testCls eq defn.BoxedUnitClass) {
238+
// as a special case, casting to Unit always successfully returns Unit
239+
Block(expr :: Nil, Literal(Constant(()))).withSpan(expr.span)
240+
}
237241
else if (foundCls.isPrimitiveValueClass) {
238242
if (testCls.isPrimitiveValueClass) primitiveConversion(expr, testCls)
239243
else derivedTree(box(expr), defn.Any_asInstanceOf, testType)

tests/run/i5791.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello!

tests/run/i5791.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
object Test {
2+
def main(args: Array[String]): Unit = {
3+
val a = { 5 }.asInstanceOf[Unit]
4+
val b = {
5+
println("hello!")
6+
5
7+
}.asInstanceOf[Unit]
8+
}
9+
}

0 commit comments

Comments
 (0)