@@ -30,7 +30,7 @@ import dotty.tools.dotc.util.Spans.Span
3030import dotty .tools .dotc .report
3131
3232import 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 }
3434import org .scalajs .ir .Names .{ClassName , MethodName , SimpleMethodName }
3535import org .scalajs .ir .OriginalName
3636import org .scalajs .ir .OriginalName .NoOriginalName
@@ -2413,11 +2413,24 @@ class JSCodeGen()(using genCtx: Context) {
24132413 * asInstanceOf to a raw JS type is completely erased.
24142414 */
24152415 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())
24162424 } else {
24172425 js.AsInstanceOf (value, toIRType(to))
24182426 }
24192427 }
24202428
2429+ private def genThrowClassCastException ()(implicit pos : Position ): js.Tree = {
2430+ js.Throw (js.New (jsNames.ClassCastExceptionClass ,
2431+ js.MethodIdent (jsNames.NoArgConstructorName ), Nil ))
2432+ }
2433+
24212434 /** Gen JS code for an isInstanceOf test (for reference types only) */
24222435 private def genIsInstanceOf (value : js.Tree , to : Type )(
24232436 implicit pos : SourcePosition ): js.Tree = {
@@ -2437,6 +2450,9 @@ class JSCodeGen()(using genCtx: Context) {
24372450 jstpe.BooleanType )
24382451 }
24392452 } 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" )
24402456 js.IsInstanceOf (value, toIRType(to))
24412457 }
24422458 }
0 commit comments