Skip to content

Fix crash boxing Scala.js (+ Scala2) value class to Object - #27082

Merged
tanishiking merged 4 commits into
scala:mainfrom
dotty-staging:i27066-scalajs-sig
Sep 18, 2026
Merged

tanishiking merged 4 commits into
scala:mainfrom
dotty-staging:i27066-scalajs-sig

Conversation

@tanishiking

@tanishiking tanishiking commented Sep 16, 2026 •

Copy link
Copy Markdown
Member

Fixes #27066

background
Scala.js js.UndefOr pseudo-unions in Scala 2 are compiled as scala.scalajs.js.| ($bar), and Scala2Unpickler reads that type as a real OrType in Scala3.

TypeErasure then re-erases it to preud-union types in symbol signatures so that ctor/method types keep $bar. On the other hand, expressions typed a js.| in Scala 3 are still erased to Object.

problem
The problem is, when we box js.UndefOr wrapped by a value class pre-compiled in Scala 2 (for example, UnitOrOps).

Erasure emit new UndefOrOps(x) with x: Object while the VC ctor expects $bar, and GenBCode failed in adapt with Cannot emit primitive conversion from Ljava/lang/Object; to Lscala/scalajs/js/$bar;

final class UndefOrOps[A] private[js] (private val self: js.UndefOr[A]) extends AnyVal {

https://github.com/scala-js/scala-js/blob/5cc1be6722317e2ae6d0fea6d9d4066f27239eae/library/src/main/scala/scala/scalajs/js/UndefOrOps.scala#L23

import scala.scalajs.js
import scala.scalajs.js.internal.UnitOps

def wrap(left: Any): Unit = ()
def repro(str: js.UndefOr[String]): Unit =
  wrap(UnitOps.unitOrOps(str))

(It's wired situation... the original report used ScalaTest assert(str.nonEmpty), where a macro binds the Select receiver and passes it to Any) 123

fix
In Erasure.box, look up the primary constructor parameter type of VC and pass the underlying value through adaptToType.

Have you relied on LLM-based tools in this contribution?

Yes, and checked every single line if it matches with my intent.

How was the solution tested?

New automated tests (including the issue's reproducer, if applicable)

Footnotes

  1. https://github.com/scalatest/scalatest/blob/b4efe5a942fd290e9b1bb377a2efeb816b34ea21/dotty/core/src/main/scala/org/scalatest/Assertions.scala#L472-L473 ↩

  2. https://github.com/scalatest/scalatest/blob/b4efe5a942fd290e9b1bb377a2efeb816b34ea21/dotty/core/src/main/scala/org/scalatest/AssertionsMacro.scala#L32-L53 ↩

  3. https://github.com/scalatest/scalatest/blob/b4efe5a942fd290e9b1bb377a2efeb816b34ea21/dotty/scalactic/src/main/scala/org/scalactic/BooleanMacro.scala#L20-L289 ↩

scala#27066

**background**
Scala.js `js.UndefOr` pseudo-unions in Scala 2 are compiled as
`scala.scalajs.js.|` (`$bar`), and `Scala2Unpickler` reads that
type as a real `OrType` in Scala3.

`TypeErasure` then re-erases it to preud-union types in symbol
signatures so that ctor/method types libraries keep `$bar`.
On the other hand, expressions typed a `js.|` in Scala 3 are
still erased to `Object`.

**problem**
The problem is, when we box `js.UndefOr` wrapped by a value class
pre-compiled in Scala 2 (for example, `UnitOrOps`).

Erasure emit `new UndefOrOps(x)` with `x: Object` while the VC ctor
expects `$bar`, and `GenBCode` failed in `adapt` with
`Cannot emit primitive conversion from Ljava/lang/Object; to Lscala/scalajs/js/$bar;`

```scala
final class UndefOrOps[A] private[js] (private val self: js.UndefOr[A]) extends AnyVal {
```
https://github.com/scala-js/scala-js/blob/5cc1be6722317e2ae6d0fea6d9d4066f27239eae/library/src/main/scala/scala/scalajs/js/UndefOrOps.scala#L23

```scala
import scala.scalajs.js
import scala.scalajs.js.internal.UnitOps

def wrap(left: Any): Unit = ()
def repro(str: js.UndefOr[String]): Unit =
  wrap(UnitOps.unitOrOps(str))
```

(It's wired situation... the original report used ScalaTest `assert(str.nonEmpty)`,
where a macro binds the `Select` receiver and passes it to `Any`) [^1][^2][^3]

**fix**
In `Erasure.box`, look up the primary constructor parameter type of VC
and pass the underlying value through `adaptToType`.

[^1]: https://github.com/scalatest/scalatest/blob/b4efe5a942fd290e9b1bb377a2efeb816b34ea21/dotty/core/src/main/scala/org/scalatest/Assertions.scala#L472-L473
[^2]: https://github.com/scalatest/scalatest/blob/b4efe5a942fd290e9b1bb377a2efeb816b34ea21/dotty/core/src/main/scala/org/scalatest/AssertionsMacro.scala#L32-L53
[^3]: https://github.com/scalatest/scalatest/blob/b4efe5a942fd290e9b1bb377a2efeb816b34ea21/dotty/scalactic/src/main/scala/org/scalactic/BooleanMacro.scala#L20-L289
val ctor = cls.primaryConstructor
transformInfo(ctor, ctor.info) match
case mt: MethodType if mt.paramInfos.nonEmpty =>
New(tycon, adaptToType(cast(tree, underlying), mt.paramInfos.head) :: Nil)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkcast scala/scalajs/js/$bar would be inserted before invokespecial UndefOrOps.<init>:(Lscala/scalajs/js/$bar;)V for the test scenario.

@tanishiking
tanishiking marked this pull request as ready for review September 17, 2026 01:52
@tanishiking
tanishiking requested a review from sjrd September 17, 2026 01:54
implicit val testGroup: TestGroup = TestGroup("runScalaJS")
aggregateTests(
compileFilesInDir("tests/run", scalaJSOptions),
compileFilesInDir("tests/run-scalajs", scalaJSOptions),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use the sjsJUnitTests for run-like tests in Scala.js:
https://github.com/scala/scala3/tree/main/tests/sjs-junit/test/org/scalajs/testsuite

The macro can be defined in the Compile scope.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, didn't know sjsJUnitTests, thanks!

case mt: MethodType if mt.paramInfos.nonEmpty =>
New(tycon, adaptToType(cast(tree, underlying), mt.paramInfos.head) :: Nil)
case _ =>
New(tycon, cast(tree, underlying) :: Nil)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both branches compute cast(tree, underlying). Consolidate?

@tanishiking
tanishiking requested a review from sjrd September 18, 2026 08:05
@tanishiking
tanishiking merged commit c1cbd05 into scala:main Sep 18, 2026
64 checks passed
@tanishiking
tanishiking deleted the i27066-scalajs-sig branch September 18, 2026 11:39
@WojciechMazur WojciechMazur added this to the 3.10.1 milestone Oct 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot emit primitive conversion from Ljava/lang/Object; to Lscala/scalajs/js/$bar

3 participants