@@ -30,7 +30,7 @@ import dotty.tools.dotc.util.Spans.Span
30
30
import dotty .tools .dotc .report
31
31
32
32
import org .scalajs .ir
33
- import org .scalajs .ir .{ClassKind , Position , Trees => js , Types => jstpe }
33
+ import org .scalajs .ir .{ClassKind , Position , Names => jsNames , Trees => js , Types => jstpe }
34
34
import org .scalajs .ir .Names .{ClassName , MethodName , SimpleMethodName }
35
35
import org .scalajs .ir .OriginalName
36
36
import org .scalajs .ir .OriginalName .NoOriginalName
@@ -2413,11 +2413,24 @@ class JSCodeGen()(using genCtx: Context) {
2413
2413
* asInstanceOf to a raw JS type is completely erased.
2414
2414
*/
2415
2415
value
2416
+ } else if (sym == defn.NullClass ) {
2417
+ js.If (
2418
+ js.BinaryOp (js.BinaryOp .=== , value, js.Null ()),
2419
+ js.Null (),
2420
+ genThrowClassCastException())(
2421
+ jstpe.NullType )
2422
+ } else if (sym == defn.NothingClass ) {
2423
+ js.Block (value, genThrowClassCastException())
2416
2424
} else {
2417
2425
js.AsInstanceOf (value, toIRType(to))
2418
2426
}
2419
2427
}
2420
2428
2429
+ private def genThrowClassCastException ()(implicit pos : Position ): js.Tree = {
2430
+ js.Throw (js.New (jsNames.ClassCastExceptionClass ,
2431
+ js.MethodIdent (jsNames.NoArgConstructorName ), Nil ))
2432
+ }
2433
+
2421
2434
/** Gen JS code for an isInstanceOf test (for reference types only) */
2422
2435
private def genIsInstanceOf (value : js.Tree , to : Type )(
2423
2436
implicit pos : SourcePosition ): js.Tree = {
@@ -2437,6 +2450,9 @@ class JSCodeGen()(using genCtx: Context) {
2437
2450
jstpe.BooleanType )
2438
2451
}
2439
2452
} else {
2453
+ // The Scala type system prevents x.isInstanceOf[Null] and ...[Nothing]
2454
+ assert(sym != defn.NullClass && sym != defn.NothingClass ,
2455
+ s " Found a .isInstanceOf[ $sym] at $pos" )
2440
2456
js.IsInstanceOf (value, toIRType(to))
2441
2457
}
2442
2458
}
0 commit comments