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

Incorrect field initialization in specialized traits #10094

Closed
scabug opened this issue Dec 6, 2016 · 1 comment · Fixed by scala/scala#9700
Closed

Incorrect field initialization in specialized traits #10094

scabug opened this issue Dec 6, 2016 · 1 comment · Fixed by scala/scala#9700

Comments

@scabug
Copy link

scabug commented Dec 6, 2016

Reported on scala-internals by Georgi Jojgov.

trait T[@specialized(Int) S] {
  def initialValue: S
  var value: S = initialValue
}

final class C[@specialized(Int) S](val initialValue: S) extends T[S]

object Test extends App {
  new C("hi")
}

Results in a ClassCastException: String cannot be cast to Integer at run-time.

The bad code is generated in constructors:

[[syntax trees at end of              constructors]] // Test.scala
...
  abstract trait T extends Object {
    ...
    def /*T*/$init$(): Unit = {
      T.this.value_=((T.this.initialValue(): Object));
      T.this.value$mcI$sp_=(scala.Int.unbox(T.this.value()));
    }
  }

when creating a C[String], the unboxing of value will fail.

As a test case, here's a full example (as reported on the mailing list), it works in 2.11.8

trait BaseSp[@specialized(Long, Double) S] {
  def initialValue: S
  var value: S = initialValue
  override def toString = "" + value
}

trait BaseNotSp[S] {
  def initialValue: S
  var value: S = initialValue
  override def toString = "" + value
}

final class Bar_Sp_Sp    [@specialized(Long, Double) S](val initialValue: S) extends BaseSp[S]
final class Bar_NotSp_Sp [                           S](val initialValue: S) extends BaseSp[S]
final class Bar_Sp_NotSp [@specialized(Long, Double) S](val initialValue: S) extends BaseNotSp[S]
final class Bar_NotSp_NotSp                         [S](val initialValue: S) extends BaseNotSp[S]

object Test extends App {
  val a = new Bar_Sp_Sp[Double](Double.MaxValue)    // CCE
  val b = new Bar_Sp_Sp[Long](Long.MaxValue)        // CCE
  val c = new Bar_NotSp_Sp[Double](Double.MaxValue) // AbstractMethodError
  val d = new Bar_NotSp_Sp[Long](Long.MaxValue)     // CCE
  println((a, b, c, d))

  val e = new Bar_Sp_NotSp[Double](Double.MaxValue)
  val f = new Bar_Sp_NotSp[Long](Long.MaxValue)
  val g = new Bar_NotSp_NotSp[Double](Double.MaxValue)
  val h = new Bar_NotSp_NotSp[Long](Long.MaxValue)
  println((e, f, g, h))
}
@scabug
Copy link
Author

scabug commented Dec 6, 2016

Imported From: https://issues.scala-lang.org/browse/SI-10094?orig=1
Reporter: @lrytz
Affected Versions: 2.12.1

@SethTisue SethTisue added this to the Backlog milestone Apr 4, 2020
joroKr21 added a commit to joroKr21/scala that referenced this issue Jul 13, 2021
Didn't investigate in which version it progressed.
joroKr21 added a commit to joroKr21/scala that referenced this issue Jul 13, 2021
Didn't investigate in which version it progressed.
lrytz added a commit to scala/scala that referenced this issue Jul 14, 2021
@SethTisue SethTisue removed this from the Backlog milestone Jul 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants