Skip to content

Commit 34d87db

Browse files
committed
Reject overrides only with -source future
Currently, the following CB projects have illegal overrides of val parameters - spire - scalaz - specs2 - akka I checked the spire issue and its seems to require a non-trivial refactoring to avoid the problem. More than I could achieve, given that I know nothing of spire. In light of this I think we can enforce the restriction only under -source future and make it a deprecation warning for now.
1 parent b5f307d commit 34d87db

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

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

Lines changed: 13 additions & 8 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,8 +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 If O is a val parameter, M must be a val parameter that passes on its
268-
* value to O.
267+
* 1.13 Under -source future, if O is a val parameter, M must be a val parameter
268+
* that passes its on to O.
269269
* 2. Check that only abstract classes have deferred members
270270
* 3. Check that concrete classes do not have deferred definitions
271271
* that are not implemented in a subclass.
@@ -448,10 +448,6 @@ object RefChecks {
448448
overrideError("cannot be used here - classes can only override abstract types")
449449
else if other.isEffectivelyFinal then // (1.2)
450450
overrideError(i"cannot override final member ${other.showLocated}")
451-
else if other.is(ParamAccessor) &&
452-
!(member.is(ParamAccessor) && ParamForwarding.inheritedAccessor(member) == other)
453-
then // (1.13)
454-
overrideError(i"cannot override val parameter ${other.showLocated}")
455451
else if (member.is(ExtensionMethod) && !other.is(ExtensionMethod)) // (1.3)
456452
overrideError("is an extension method, cannot override a normal method")
457453
else if (other.is(ExtensionMethod) && !member.is(ExtensionMethod)) // (1.3)
@@ -520,6 +516,15 @@ object RefChecks {
520516
overrideError(i"needs to be declared with @targetName(${"\""}${other.targetName}${"\""}) so that external names match")
521517
else
522518
overrideError("cannot have a @targetName annotation since external names would be different")
519+
else if other.is(ParamAccessor)
520+
&& !(member.is(ParamAccessor) && ParamForwarding.inheritedAccessor(member) == other)
521+
then // (1.13)
522+
if sourceVersion.isAtLeast(`future`) then
523+
overrideError(i"cannot override val parameter ${other.showLocated}")
524+
else
525+
report.deprecationWarning(
526+
i"overriding val parameter ${other.showLocated} is deprecated, will be illegal in a future version",
527+
member.srcPos)
523528
else if !other.isExperimental && member.hasAnnotation(defn.ExperimentalAnnot) then // (1.12)
524529
overrideError("may not override non-experimental member")
525530
else if other.hasAnnotation(defn.DeprecatedOverridingAnnot) then

tests/neg/i16092.scala renamed to tests/neg-strict/i16092.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Z(val x: X, val t: x.T) {
77
def process(): Unit = x.process(t)
88
}
99
class Evil(x1: X, x2: X, t: x1.T) extends Z(x1, t) {
10-
val x: X = x2 // error breaks connection between x and t
10+
override val x: X = x2 // error breaks connection between x and t
1111
}
1212
// alarm bells should be ringing by now
1313

0 commit comments

Comments
 (0)