Skip to content

Commit d4835fa

Browse files
committed
Change order of override error checks
The error check that is not possible to override an extension/normal methods, because it should be as declared. Shound come before the check that the `override` modifier is needed. Add test case for this: 'override-extension_normal-methods'
1 parent de75713 commit d4835fa

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ object RefChecks {
143143
*
144144
* 1.1. M must have the same or stronger access privileges as O.
145145
* 1.2. O must not be effectively final.
146+
* 1.9.2 If M or O are extension methods, they must both be extension methods.
147+
* (Should be before the check for the need of `override` modifier.)
146148
* 1.3. O is deferred, or M has `override` modifier.
147149
* 1.4. If O is stable, then so is M.
148150
* // @M: LIFTED 1.5. Neither M nor O are a parameterized type alias
@@ -157,7 +159,6 @@ object RefChecks {
157159
* 1.8.2 M is of type []S, O is of type ()T and S <: T, or
158160
* 1.8.3 M is of type ()S, O is of type []T and S <: T, or
159161
* 1.9.1 If M is erased, O is erased. If O is erased, M is erased or inline.
160-
* 1.9.2 If M or O are extension methods, they must both be extension methods.
161162
* 1.10. If O is inline (and deferred, otherwise O would be final), M must be inline
162163
* 1.11. If O is a Scala-2 macro, M must be a Scala-2 macro.
163164
* 2. Check that only abstract classes have deferred members
@@ -343,6 +344,10 @@ object RefChecks {
343344
overrideError("cannot be used here - classes can only override abstract types")
344345
else if (other.isEffectivelyFinal) // (1.2)
345346
overrideError(i"cannot override final member ${other.showLocated}")
347+
else if (member.isAllOf(ExtensionMethod) && !other.isAllOf(ExtensionMethod)) // (1.9.2)
348+
overrideError("is an extension method, cannot override a normal method")
349+
else if (other.isAllOf(ExtensionMethod) && !member.isAllOf(ExtensionMethod)) // (1.9.2)
350+
overrideError("is a normal method, cannot override an extension method")
346351
else if (!other.is(Deferred) &&
347352
!other.name.is(DefaultGetterName) &&
348353
!member.isAnyOverride)
@@ -394,10 +399,6 @@ object RefChecks {
394399
overrideError("is erased, cannot override non-erased member")
395400
else if (other.is(Erased) && !member.isOneOf(Erased | Inline)) // (1.9.1)
396401
overrideError("is not erased, cannot override erased member")
397-
else if (member.isAllOf(ExtensionMethod) && !other.isAllOf(ExtensionMethod)) // (1.9.2)
398-
overrideError("is an extension method, cannot override a normal method")
399-
else if (other.isAllOf(ExtensionMethod) && !member.isAllOf(ExtensionMethod)) // (1.9.2)
400-
overrideError("is a normal method, cannot override an extension method")
401402
else if other.is(Inline) && !member.is(Inline) then // (1.10)
402403
overrideError("is not inline, cannot implement an inline method")
403404
else if (other.isScala2Macro && !member.isScala2Macro) // (1.11)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- Error: tests/neg/override-extension_normal-methods.scala:6:6 --------------------------------------------------------
2+
6 | def m[T](x: T): String = "normal method" // error: normal method, cannot override an extension method. Also needs `override' modifier (but this error should be obfuscated).
3+
| ^
4+
| error overriding method m in trait A of type [T](t: T): String;
5+
| method m of type [T](x: T): String is a normal method, cannot override an extension method
6+
-- Error: tests/neg/override-extension_normal-methods.scala:14:6 -------------------------------------------------------
7+
14 | def [T](t: T).m: String = "extrnsion method" // error: extension method, cannot override an normal method. Also needs `override' modifier (but this error should be obfuscated).
8+
| ^
9+
| error overriding method m in trait B of type [T](x: T): String;
10+
| method m of type [T](t: T): String is an extension method, cannot override a normal method
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
trait A {
2+
def [T](t: T).m: String = "extrnsion method"
3+
}
4+
5+
trait AAA extends A {
6+
def m[T](x: T): String = "normal method" // error: normal method, cannot override an extension method. Also needs `override' modifier (but this error should be obfuscated).
7+
}
8+
9+
trait B {
10+
def m[T](x: T): String = "normal method"
11+
}
12+
13+
trait BBB extends B {
14+
def [T](t: T).m: String = "extrnsion method" // error: extension method, cannot override an normal method. Also needs `override' modifier (but this error should be obfuscated).
15+
}

0 commit comments

Comments
 (0)