Skip to content

Removes spurious js.AsInstanceOf around the js.LinkTimeIf #5210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion compiler/src/main/scala/org/scalajs/nscplugin/GenJSCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3207,7 +3207,30 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
case Object_isInstanceOf =>
genIsAsInstanceOf(obj, targs, cast = false)
case Object_asInstanceOf =>
genIsAsInstanceOf(obj, targs, cast = true)
val targetTpe = targs.head.tpe
obj match {
/* This is an optimization for `linkTimeIf(cond)(thenp)(elsep).asInstanceOf[T]`.
* If both `thenp` and `elsep` are subtypes of `T`, the `asInstanceOf`
* is redundant and can be removed. The optimizer already routinely performs
* this optimization. However, that comes too late for the module instance
* field alias analysis performed by `IncOptimizer`. In that case, while the
* desugarer removes the `LinkTimeIf`, the extra `AsInstanceOf` prevents
* aliasing the field. Removing the cast ahead of time in the compiler allows
* field aliases to be recognized in the presence of `LinkTimeIf`s.
*/
case Apply(fun, List(cond, thenp, elsep))
if fun.symbol == jsDefinitions.LinkingInfo_linkTimeIf &&
thenp.tpe <:< targetTpe && elsep.tpe <:< targetTpe =>
val genObj = genExpr(obj) match {
case t: js.LinkTimeIf => t
case t =>
abort("Unexpected tree " + t +
" is generated for " + fun + " at: " + tree.pos)
}
js.LinkTimeIf(genObj.cond, genObj.thenp, genObj.elsep)(toIRType(targetTpe))(genObj.pos)
case _ =>
genIsAsInstanceOf(obj, targs, cast = true)
}
case Object_synchronized =>
genSynchronized(obj, args.head, isStat)
case _ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,50 @@ class OptimizationTest extends JSASTTest {
}
}
}

@Test
def linkTimeIf: Unit = {
/* Make sure the cast in
* `linkTimeIf(cond)(thenp)(elsep).asInstanceOf[T]``
* is optimized away if `thenp` and `elsep` are subtypes of `T`.
*/
"""
import scala.scalajs.js
import scala.scalajs.LinkingInfo._

class LinkTimeIfTest {
import LinkTimeIfTest._
private val impl = linkTimeIf[ArrayImpl](productionMode) {
JSArrayImpl
} {
ScalaArrayImpl
}
def implPattern(): Int = {
impl.length(impl.empty())
}
}
object LinkTimeIfTest {
sealed private abstract class ArrayImpl {
type Repr
def empty(): Repr
def length(v: Repr): Int
}
private object JSArrayImpl extends ArrayImpl {
type Repr = js.Array[AnyRef]
def empty(): Repr = js.Array[AnyRef]()
def length(v: Repr): Int = v.length
}
private object ScalaArrayImpl extends ArrayImpl {
type Repr = Array[AnyRef]
def empty(): Repr = new Array[AnyRef](0)
def length(v: Repr): Int = v.length
}
}
""".
hasNot("linkTimeIf[A](...).asInstanceOf[A]") {
case js.AsInstanceOf(_:js.LinkTimeIf, _) =>
}
}
}

object OptimizationTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,51 @@ class LinkTimeIfTest {
}
assertEquals(pow(2.0, 8.0), 256.0, 0)
}

// This test verifies that the compiler can safely compile surrounding
// implicit asInstanceOf casts to a supertype.
@Test def subtyping(): Unit = {
trait A { def value: Int }
class B(val value: Int) extends A
class C(val value: Int) extends A

val b = new B(1)
val c = new C(2)

val result1 = linkTimeIf[A](productionMode) { b } { c }
assertEquals(if (Platform.isInProductionMode) b.value else c.value, result1.value)

val result2 = linkTimeIf[A](productionMode) { c } { b }
assertEquals(if (Platform.isInProductionMode) c.value else b.value, result2.value)
}

@Test def implPattern(): Unit = {
import LinkTimeIfTest._
val impl = linkTimeIf[ArrayImpl](productionMode) {
JSArrayImpl
} {
ScalaArrayImpl
}
assertEquals(0, impl.length(impl.empty()))
}
}

object LinkTimeIfTest {
sealed private abstract class ArrayImpl {
type Repr
def empty(): Repr
def length(v: Repr): Int
}

private object JSArrayImpl extends ArrayImpl {
type Repr = js.Array[AnyRef]
def empty(): Repr = js.Array[AnyRef]()
def length(v: Repr): Int = v.length
}

private object ScalaArrayImpl extends ArrayImpl {
type Repr = Array[AnyRef]
def empty(): Repr = new Array[AnyRef](0)
def length(v: Repr): Int = v.length
}
}