-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Unsoundness from overriding vals #16092
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
Comments
Good point. |
I tried to construct a similar unsoundness example using just member val overriding (no val parameters) but failed. It would be good to find out whether that's just my lack of imagination or indeed it's necessary to have an overridden val parameter to exhibit the unsoundness. |
We disallow overriding of val parameters, which fixes the soundness problem discovered in scala#16092. There is one exception: If a val parameter is overridden by another val parameter that can be shown to always have the same value (in the sense established by Paramforwarding.inheritedAccessor). This exception is needed to make a not-so-uncommon pattern of case class inheritance go through. Example: abstract class A(val x: Int) case class B(override val x: Int) extends A(x) case class C(override val x: Int) extends A(x) case object D extends A(0) Here, the `override val`s are necessary since case class parameters are always vals, so they do override the val in class A. It should be noted that the override val generates a second field, so this not a very efficient representation. A better design would be to use an abstract field in `A`: abstract class A { val x: Int } case class B(val x: Int) extends A case class C(val x: Int) extends A case object D extends A { val a = 0 } But that causes slightly more work for cases as in D. Which seems to be why the first pattern is sometimes used. It might be desirable to disallow the second pattern, but that would cause quite a bit of migration hassle since it requires synchronized changes at several places of a class hierarchy.
For what it's worth, I took a look at this issue and I also can't find an example where a similar issue occurs without using val parameters. It seems that the real problem is allowing |
We disallow overriding of val parameters, which fixes the soundness problem discovered in #16092. There is one exception: If a val parameter is overridden by another val parameter that can be shown to always have the same value (in the sense established by Paramforwarding.inheritedAccessor). This exception is needed to make a not-so-uncommon pattern of case class inheritance go through. Example: abstract class A(val x: Int) case class B(override val x: Int) extends A(x) case class C(override val x: Int) extends A(x) case object D extends A(0) Here, the `override val`s are necessary since case class parameters are always vals, so they do override the val in class A. It should be noted that the override val generates a second field, so this not a very efficient representation. A better design would be to use an abstract field in `A`: abstract class A { val x: Int } case class B(val x: Int) extends A case class C(val x: Int) extends A case object D extends A { val a = 0 } But that causes slightly more work for cases as in D. Which seems to be why the first pattern is sometimes used. It might be desirable to disallow the first pattern, but that would cause quite a bit of migration hassle since it requires synchronized changes at several places of a class hierarchy. Fixes #16092
We disallow overriding of val parameters, which fixes the soundness problem discovered in scala#16092. There is one exception: If a val parameter is overridden by another val parameter that can be shown to always have the same value (in the sense established by Paramforwarding.inheritedAccessor). This exception is needed to make a not-so-uncommon pattern of case class inheritance go through. Example: abstract class A(val x: Int) case class B(override val x: Int) extends A(x) case class C(override val x: Int) extends A(x) case object D extends A(0) Here, the `override val`s are necessary since case class parameters are always vals, so they do override the val in class A. It should be noted that the override val generates a second field, so this not a very efficient representation. A better design would be to use an abstract field in `A`: abstract class A { val x: Int } case class B(val x: Int) extends A case class C(val x: Int) extends A case object D extends A { val a = 0 } But that causes slightly more work for cases as in D. Which seems to be why the first pattern is sometimes used. It might be desirable to disallow the second pattern, but that would cause quite a bit of migration hassle since it requires synchronized changes at several places of a class hierarchy.
Tested on Dotty 3.2.0 and 3.2.1-RC2 (both via Scastie). What it says on the tin: overriding a
val
basically makes paths containing thatval
unstable. I searched the Dotty issue tracker for this issue and didn't find it, but I was expecting that a (serious?) soundness failure would be there. So this might be a duplicate.Code
Output
No compiler error, and a
ClassCastException
when run.Expectation
Compiler error of some kind.
The text was updated successfully, but these errors were encountered: