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

Auxiliary constructors in Value Classes lead to runtime errors. #6148

Closed
scabug opened this issue Jul 27, 2012 · 3 comments
Closed

Auxiliary constructors in Value Classes lead to runtime errors. #6148

scabug opened this issue Jul 27, 2012 · 3 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Jul 27, 2012

scala> class A(val x: Int) extends AnyVal { def this(s: String) = this(s.length) }
defined class A

scala> new A("a") == new A("b")
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
    at scala.runtime.BoxesRunTime.unboxToInt(Unknown Source)
    at .<init>(<console>:9)
    at .<clinit>(<console>)

scala> class X(val a: Int) extends AnyVal { def this(x: String) = this(0) }
defined class X

scala> new X("").a
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
    at

Troublesome spots in the implementation:

https://github.com/scala/scala/blob/39fc4aa84/src/compiler/scala/tools/nsc/transform/Erasure.scala#L362

https://github.com/scala/scala/blob/39fc4aa84/src/compiler/scala/tools/nsc/transform/Erasure.scala#L1003

https://github.com/scala/scala/blob/39fc4aa84/src/compiler/scala/tools/nsc/transform/PostErasure.scala#L36

https://github.com/scala/scala/blob/39fc4aa84/src/compiler/scala/tools/nsc/transform/PostErasure.scala#L46

I tried to constrain these spots to only consider primary constructor, but this triggered a compiler crash which I'm yet to pin down.

A heavy handed approach would be to disallow secondary constructors for now.

@scabug
Copy link
Author

scabug commented Jul 27, 2012

Imported From: https://issues.scala-lang.org/browse/SI-6148?orig=1
Reporter: @retronym
See #5799, #6090

@scabug
Copy link
Author

scabug commented Jul 29, 2012

@retronym said:
scala/scala#1011

@scabug
Copy link
Author

scabug commented Aug 1, 2012

@soc said:
Fixed in aad84ecba3.

@scabug scabug closed this as completed Aug 1, 2012
@scabug scabug added this to the 2.10.0-M6 milestone Apr 7, 2017
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

No branches or pull requests

2 participants