Skip to content

Commit 8d723a9

Browse files
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 #16092
2 parents 9d574ce + fb75d96 commit 8d723a9

File tree

19 files changed

+138
-83
lines changed

19 files changed

+138
-83
lines changed

compiler/src/dotty/tools/dotc/transform/ParamForwarding.scala

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ import NameKinds.ParamAccessorName
3030
* The aim of this transformation is to avoid redundant parameter accessor fields.
3131
*/
3232
class ParamForwarding extends MiniPhase with IdentityDenotTransformer:
33-
import ast.tpd._
33+
import ast.tpd.*
34+
import ParamForwarding.inheritedAccessor
3435

3536
private def thisPhase: ParamForwarding = this
3637

@@ -39,20 +40,6 @@ class ParamForwarding extends MiniPhase with IdentityDenotTransformer:
3940
override def description: String = ParamForwarding.description
4041

4142
def transformIfParamAlias(mdef: ValOrDefDef)(using Context): Tree =
42-
43-
def inheritedAccessor(sym: Symbol)(using Context): Symbol =
44-
val candidate = sym.owner.asClass.superClass
45-
.info.decl(sym.name).suchThat(_.is(ParamAccessor, butNot = Mutable))
46-
.symbol
47-
if !candidate.is(Private) // candidate might be private and accessible if it is in an outer class
48-
&& candidate.isAccessibleFrom(currentClass.thisType, superAccess = true)
49-
then
50-
candidate
51-
else if candidate.is(SuperParamAlias) then
52-
inheritedAccessor(candidate)
53-
else
54-
NoSymbol
55-
5643
val sym = mdef.symbol.asTerm
5744
if sym.is(SuperParamAlias) then
5845
assert(sym.is(ParamAccessor, butNot = Mutable))
@@ -84,3 +71,17 @@ class ParamForwarding extends MiniPhase with IdentityDenotTransformer:
8471
object ParamForwarding:
8572
val name: String = "paramForwarding"
8673
val description: String = "add forwarders for aliases of superclass parameters"
74+
75+
def inheritedAccessor(sym: Symbol)(using Context): Symbol =
76+
val candidate = sym.owner.asClass.superClass
77+
.info.decl(sym.name).suchThat(_.is(ParamAccessor, butNot = Mutable))
78+
.symbol
79+
if !candidate.is(Private) // candidate might be private and accessible if it is in an outer class
80+
&& candidate.isAccessibleFrom(currentClass.thisType, superAccess = true)
81+
then
82+
candidate
83+
else if candidate.is(SuperParamAlias) then
84+
inheritedAccessor(candidate)
85+
else
86+
NoSymbol
87+
end ParamForwarding

compiler/src/dotty/tools/dotc/typer/RefChecks.scala

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import config.Printers.{checks, noPrinter}
1515
import Decorators._
1616
import OverridingPairs.isOverridingPair
1717
import typer.ErrorReporting._
18-
import config.Feature.{warnOnMigration, migrateTo3}
19-
import config.SourceVersion.`3.0`
18+
import config.Feature.{warnOnMigration, migrateTo3, sourceVersion}
19+
import config.SourceVersion.{`3.0`, `future`}
2020
import config.Printers.refcheck
2121
import reporting._
2222
import Constants.Constant
@@ -264,6 +264,8 @@ object RefChecks {
264264
* 1.10. If O is inline (and deferred, otherwise O would be final), M must be inline
265265
* 1.11. If O is a Scala-2 macro, M must be a Scala-2 macro.
266266
* 1.12. If O is non-experimental, M must be non-experimental.
267+
* 1.13 Under -source future, if O is a val parameter, M must be a val parameter
268+
* that passes its value on to O.
267269
* 2. Check that only abstract classes have deferred members
268270
* 3. Check that concrete classes do not have deferred definitions
269271
* that are not implemented in a subclass.
@@ -514,12 +516,26 @@ object RefChecks {
514516
overrideError(i"needs to be declared with @targetName(${"\""}${other.targetName}${"\""}) so that external names match")
515517
else
516518
overrideError("cannot have a @targetName annotation since external names would be different")
519+
else if other.is(ParamAccessor) && !isInheritedAccessor(member, other) then // (1.13)
520+
if sourceVersion.isAtLeast(`future`) then
521+
overrideError(i"cannot override val parameter ${other.showLocated}")
522+
else
523+
report.deprecationWarning(
524+
i"overriding val parameter ${other.showLocated} is deprecated, will be illegal in a future version",
525+
member.srcPos)
517526
else if !other.isExperimental && member.hasAnnotation(defn.ExperimentalAnnot) then // (1.12)
518527
overrideError("may not override non-experimental member")
519528
else if other.hasAnnotation(defn.DeprecatedOverridingAnnot) then
520529
overrideDeprecation("", member, other, "removed or renamed")
521530
end checkOverride
522531

532+
def isInheritedAccessor(mbr: Symbol, other: Symbol): Boolean =
533+
mbr.is(ParamAccessor)
534+
&& {
535+
val next = ParamForwarding.inheritedAccessor(mbr)
536+
next == other || isInheritedAccessor(next, other)
537+
}
538+
523539
OverridingPairsChecker(clazz, self).checkAll(checkOverride)
524540
printMixinOverrideErrors()
525541

tests/init/neg/override13.scala

Lines changed: 0 additions & 13 deletions
This file was deleted.

tests/init/neg/override16.scala

Lines changed: 0 additions & 23 deletions
This file was deleted.

tests/init/neg/override5.scala

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,3 @@ trait Base {
2525

2626
val message = "hello, " + name
2727
}
28-
29-
class Derived(val name: String) extends Base
30-
31-
class Derived2 extends Derived("hello") {
32-
override val name: String = "ok" // error
33-
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
trait Pet(val name: String, rest: Int):
2+
def f(suffix: String) = s"$name$suffix$rest"
3+
4+
class Birdie(override val name: String) extends Pet("huh", 1) // error
5+
6+

0 commit comments

Comments
 (0)