Repository navigation
Fix crash boxing Scala.js (+ Scala2) value class to Object - #27082
Merged
Merged
Conversation
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
tanishiking
commented
Sep 17, 2026
| 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) |
Member
Author
There was a problem hiding this comment.
checkcast scala/scalajs/js/$bar would be inserted before invokespecial UndefOrOps.<init>:(Lscala/scalajs/js/$bar;)V for the test scenario.
tanishiking
marked this pull request as ready for review
September 17, 2026 01:52
sjrd
requested changes
Sep 17, 2026
| implicit val testGroup: TestGroup = TestGroup("runScalaJS") | ||
| aggregateTests( | ||
| compileFilesInDir("tests/run", scalaJSOptions), | ||
| compileFilesInDir("tests/run-scalajs", scalaJSOptions), |
Member
There was a problem hiding this comment.
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.
Member
Author
There was a problem hiding this comment.
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) |
Member
There was a problem hiding this comment.
Both branches compute cast(tree, underlying). Consolidate?
sjrd
approved these changes
Sep 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #27066
background
Scala.js
js.UndefOrpseudo-unions in Scala 2 are compiled asscala.scalajs.js.|($bar), andScala2Unpicklerreads that type as a realOrTypein Scala3.TypeErasurethen re-erases it to preud-union types in symbol signatures so that ctor/method types keep$bar. On the other hand, expressions typed ajs.|in Scala 3 are still erased toObject.problem
The problem is, when we box
js.UndefOrwrapped by a value class pre-compiled in Scala 2 (for example,UnitOrOps).Erasure emit
new UndefOrOps(x)withx: Objectwhile the VC ctor expects$bar, andGenBCodefailed inadaptwithCannot emit primitive conversion from Ljava/lang/Object; to Lscala/scalajs/js/$bar;https://github.com/scala-js/scala-js/blob/5cc1be6722317e2ae6d0fea6d9d4066f27239eae/library/src/main/scala/scala/scalajs/js/UndefOrOps.scala#L23
(It's wired situation... the original report used ScalaTest
assert(str.nonEmpty), where a macro binds theSelectreceiver and passes it toAny) 123fix
In
Erasure.box, look up the primary constructor parameter type of VC and pass the underlying value throughadaptToType.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
https://github.com/scalatest/scalatest/blob/b4efe5a942fd290e9b1bb377a2efeb816b34ea21/dotty/core/src/main/scala/org/scalatest/Assertions.scala#L472-L473 β©
https://github.com/scalatest/scalatest/blob/b4efe5a942fd290e9b1bb377a2efeb816b34ea21/dotty/core/src/main/scala/org/scalatest/AssertionsMacro.scala#L32-L53 β©
https://github.com/scalatest/scalatest/blob/b4efe5a942fd290e9b1bb377a2efeb816b34ea21/dotty/scalactic/src/main/scala/org/scalactic/BooleanMacro.scala#L20-L289 β©