@@ -2532,11 +2532,24 @@ class JSCodeGen()(using genCtx: Context) {
25322532 * asInstanceOf to a raw JS type is completely erased.
25332533 */
25342534 value
2535+ } else if (sym == defn.NullClass ) {
2536+ js.If (
2537+ js.BinaryOp (js.BinaryOp .=== , value, js.Null ()),
2538+ js.Null (),
2539+ genThrowClassCastException())(
2540+ jstpe.NullType )
2541+ } else if (sym == defn.NothingClass ) {
2542+ js.Block (value, genThrowClassCastException())
25352543 } else {
25362544 js.AsInstanceOf (value, toIRType(to))
25372545 }
25382546 }
25392547
2548+ private def genThrowClassCastException ()(implicit pos : Position ): js.Tree = {
2549+ js.Throw (js.New (jsNames.ClassCastExceptionClass ,
2550+ js.MethodIdent (jsNames.NoArgConstructorName ), Nil ))
2551+ }
2552+
25402553 /** Gen JS code for an isInstanceOf test (for reference types only) */
25412554 private def genIsInstanceOf (value : js.Tree , to : Type )(
25422555 implicit pos : SourcePosition ): js.Tree = {
@@ -2556,6 +2569,9 @@ class JSCodeGen()(using genCtx: Context) {
25562569 jstpe.BooleanType )
25572570 }
25582571 } else {
2572+ // The Scala type system prevents x.isInstanceOf[Null] and ...[Nothing]
2573+ assert(sym != defn.NullClass && sym != defn.NothingClass ,
2574+ s " Found a .isInstanceOf[ $sym] at $pos" )
25592575 js.IsInstanceOf (value, toIRType(to))
25602576 }
25612577 }
0 commit comments