Skip to content
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

Array.ofDim[Unit](0) throws IllegalArgumentException on Scala.js #22226

Closed
armanbilge opened this issue Dec 17, 2024 · 2 comments
Closed

Array.ofDim[Unit](0) throws IllegalArgumentException on Scala.js #22226

armanbilge opened this issue Dec 17, 2024 · 2 comments

Comments

@armanbilge
Copy link
Contributor

Compiler version

3.6.4-RC1-bin-20241216-7dc5008-NIGHTLY

Minimized code

//> using scala 3.6.4-RC1-bin-20241216-7dc5008-NIGHTLY
//> using platform js

@main def main = Array.ofDim[Unit](0)

Output

/tmp/main12667871020961532039.js:1141
      throw e;
      ^

java.lang.IllegalArgumentException
    at $TypeData.newArray (/tmp/main12667871020961532039.js:851:11)
    at $p_jl_reflect_Array$__rec$1__jl_Class__I__AI__O (/tmp/main12667871020961532039.js:1085:39)
    at $c_jl_reflect_Array$.newInstance__jl_Class__AI__O (/tmp/main12667871020961532039.js:1121:10)
    at $c_Lbug$package$.main__Ajl_Void (/tmp/main12667871020961532039.js:982:52)
    at $s_Lmain__main__AT__V (/tmp/main12667871020961532039.js:1135:24)
    at Object.<anonymous> (/tmp/main12667871020961532039.js:2184:1)
    at Object.<anonymous> (/tmp/main12667871020961532039.js:2185:4)
    at Module._compile (node:internal/modules/cjs/loader:1546:14)
    at Object..js (node:internal/modules/cjs/loader:1689:10)
    at Module.load (node:internal/modules/cjs/loader:1318:32) {
  jl_Throwable__f_s: null
}

Expectation

On the JVM it returns an array of scala.runtime.BoxedUnit.

Follow-up to scala-js/scala-js#5084.

@armanbilge armanbilge added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 17, 2024
@sjrd sjrd self-assigned this Dec 17, 2024
@sjrd sjrd added area:scala.js and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 17, 2024
@SethTisue
Copy link
Member

“Doeraene reward check” when? https://en.wikipedia.org/wiki/Knuth_reward_check

@sjrd
Copy link
Member

sjrd commented Dec 18, 2024

Thanks for the report.

It turns out it's also broken for new Array[Unit](0), and also Array.ofDim[Unit](0, 0).

In general this is because the ArrayConstructors phase uses classOf[Unit] as element type when calling newArray, but it's supposed to use classOf[BoxedUnit].

The JVM is not affected because it doesn't care about this first argument. The JVM backend rewrites calls to newArray in a way that doesn't use it.

hamzaremmal added a commit that referenced this issue Dec 18, 2024
…tors`. (#22238)

The `ArrayConstructors` phase rewrites array constructors to calls to
`scala.runtime.Arrays.newArray`. When it does that, it must pass the
run-time `jl.Class` of the element type. Previously, it used
`classOf[Unit]` when creating an `Array[Unit]` (or nested). That is not
correct, as from the Java perspective, we need to create
`Array[BoxedUnit]`.

We now identify `elemType <: Unit` and replace it with `BoxedUnit`.

---

This highlights a limitation of the Scala.js backend. We should rewrite
calls to `newArray` in the backend to use direct array creation instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants