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[A[Int]] where A[_] is an abstract type member causes ClassCastException at runtime #22888

Closed
albertpchen opened this issue Mar 29, 2025 · 2 comments · Fixed by #22938
Closed
Labels
area:typer itype:bug itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException)

Comments

@albertpchen
Copy link
Contributor

Compiler version

3.6.3

Minimized code

trait Foo:
  type A[T]
  var arr: Array[A[Int]] = null

class Bar() extends Foo:
  type A[T] = Int

object Main:
  def main(args: Array[String]) =
    val bar = new Bar()
    bar.arr = Array.ofDim[Int](1)
    bar.arr(0) = 123

Output

Exception in thread "main" java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [I ([Ljava.lang.Object; and [I are in module java.base of loader 'bootstrap')

Expectation

It should run without crashing

@albertpchen albertpchen added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Mar 29, 2025
@som-snytt
Copy link
Contributor

Same behavior at current 3.7.1-RC1-bin-SNAPSHOT-nonbootstrapped-git-d519790

"Fixed in Scala 2"

@Gedochao Gedochao added itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException) area:typer and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 1, 2025
@albertpchen
Copy link
Contributor Author

albertpchen commented Apr 3, 2025

In case anyone else is running into this issue, I was able to work around this by adding a type bound

trait Foo:
  type Dummy
  type A[T] <: Dummy
  var arr: Array[A[Int]] = null

class Bar() extends Foo:
  type Dummy = Any
  type A[T] = Int

object Main:
  def main(args: Array[String]) =
    val bar = new Bar()
    bar.arr = Array.ofDim[Int](1)
    bar.arr(0) = 123

@smarter smarter closed this as completed in b90a185 Apr 8, 2025
smarter added a commit that referenced this issue Apr 8, 2025
A higher-kinded type `type A[T]` has to be treated just like an abstract
type `type A`.

Fixes #22888
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:typer itype:bug itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants