-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disallow overriding val parameters (#16096)
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
- Loading branch information
Showing
19 changed files
with
138 additions
and
83 deletions.
There are no files selected for viewing
Submodule scalacheck
updated
1 files
+3 −2 | core/shared/src/main/scala/org/scalacheck/Properties.scala |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
trait Pet(val name: String, rest: Int): | ||
def f(suffix: String) = s"$name$suffix$rest" | ||
|
||
class Birdie(override val name: String) extends Pet("huh", 1) // error | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
trait X { | ||
type T | ||
def process(t: T): Unit | ||
} | ||
|
||
class Z(val x: X, val t: x.T) { | ||
def process(): Unit = x.process(t) | ||
} | ||
class Evil(x1: X, x2: X, t: x1.T) extends Z(x1, t) { | ||
override val x: X = x2 // error breaks connection between x and t | ||
} | ||
// alarm bells should be ringing by now | ||
|
||
// taking it to its conclusion... | ||
object x1 extends X { | ||
override type T = Int | ||
override def process(t: T): Unit = println("Int: " + t) | ||
} | ||
object x2 extends X { | ||
override type T = String | ||
override def process(t: T): Unit = println("String: " + t) | ||
} | ||
|
||
@main def Test = new Evil(x1, x2, 42).process() // BOOM: basically did x2.process(42) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
trait X: | ||
type T | ||
def process(t: T): Unit | ||
|
||
abstract class Z: | ||
def x1: X | ||
val x: X = x1 | ||
def t: x.T | ||
def process(): Unit = x.process(t) | ||
|
||
class Evil extends Z: | ||
def x2: X | ||
override val x: X = x2 | ||
|
||
// alarm bells should be ringing by now | ||
|
||
// taking it to its conclusion... | ||
object X1 extends X: | ||
override type T = Int | ||
override def process(t: T): Unit = println("Int: " + t) | ||
|
||
object X2 extends X: | ||
override type T = String | ||
override def process(t: T): Unit = println("String: " + t) | ||
|
||
@main def Test = | ||
new Evil{ | ||
val x1 = X1 | ||
val x2 = X2 | ||
val t = 42 // error | ||
}.process() // BOOM: basically did x2.process(42) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
trait A(val s: String) { println(s) } | ||
trait B extends A { override val s = "B" } // requires override val s | ||
trait A(s: String) { println(s) } | ||
trait B extends A { val s = "B" } | ||
class C extends B // error | ||
@main def Test = C() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class A(val x: Int) | ||
class B(override val x: Int) extends A(x) | ||
|
||
class C(x: Int) extends A(x) | ||
case class D(override val x: Int) extends C(x) | ||
|
||
// The following is extracted from akka: | ||
trait LogEvent { | ||
def cause: Throwable | ||
} | ||
|
||
/** | ||
* For ERROR Logging | ||
*/ | ||
case class Error(override val cause: Throwable) extends LogEvent | ||
class Error2(override val cause: Throwable) extends Error(cause) | ||
class Error3(override val cause: Throwable) extends Error2(cause) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
class A(a: Int) | ||
|
||
class B extends A(1): | ||
val a = 2 // ok | ||
|
||
@main def Test = | ||
assert(B().a == 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters