@@ -2532,11 +2532,24 @@ class JSCodeGen()(using genCtx: Context) {
2532
2532
* asInstanceOf to a raw JS type is completely erased.
2533
2533
*/
2534
2534
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())
2535
2543
} else {
2536
2544
js.AsInstanceOf (value, toIRType(to))
2537
2545
}
2538
2546
}
2539
2547
2548
+ private def genThrowClassCastException ()(implicit pos : Position ): js.Tree = {
2549
+ js.Throw (js.New (jsNames.ClassCastExceptionClass ,
2550
+ js.MethodIdent (jsNames.NoArgConstructorName ), Nil ))
2551
+ }
2552
+
2540
2553
/** Gen JS code for an isInstanceOf test (for reference types only) */
2541
2554
private def genIsInstanceOf (value : js.Tree , to : Type )(
2542
2555
implicit pos : SourcePosition ): js.Tree = {
@@ -2556,6 +2569,9 @@ class JSCodeGen()(using genCtx: Context) {
2556
2569
jstpe.BooleanType )
2557
2570
}
2558
2571
} 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" )
2559
2575
js.IsInstanceOf (value, toIRType(to))
2560
2576
}
2561
2577
}
0 commit comments