Commit 8d723a9
authored
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 #16092File tree
19 files changed
+138
-83
lines changed- community-build/community-projects
- compiler/src/dotty/tools/dotc
- transform
- typer
- tests
- init/neg
- neg-custom-args/deprecation
- neg-strict
- neg
- pos-with-compiler/tasty
- pos
- run
19 files changed
+138
-83
lines changedLines changed: 16 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
| |||
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | 43 | | |
57 | 44 | | |
58 | 45 | | |
| |||
84 | 71 | | |
85 | 72 | | |
86 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
19 | | - | |
| 18 | + | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| |||
264 | 264 | | |
265 | 265 | | |
266 | 266 | | |
| 267 | + | |
| 268 | + | |
267 | 269 | | |
268 | 270 | | |
269 | 271 | | |
| |||
514 | 516 | | |
515 | 517 | | |
516 | 518 | | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
517 | 526 | | |
518 | 527 | | |
519 | 528 | | |
520 | 529 | | |
521 | 530 | | |
522 | 531 | | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
523 | 539 | | |
524 | 540 | | |
525 | 541 | | |
| |||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
0 commit comments