Skip to content

Commit 9a9834e

Browse files
authored
Disallow mixins where super calls bind to vals (#16908)
2 parents 769ca3b + a53b185 commit 9a9834e

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ object ResolveSuper {
119119
report.error(IllegalSuperAccessor(base, memberName, targetName, acc, accTp, other.symbol, otherTp), base.srcPos)
120120
bcs = bcs.tail
121121
}
122+
if sym.is(Accessor) then
123+
report.error(
124+
em"parent ${acc.owner} has a super call which binds to the value ${sym.showFullName}. Super calls can only target methods.", base)
122125
sym.orElse {
123126
val originalName = acc.name.asTermName.originalOfSuperAccessorName
124127
report.error(em"Member method ${originalName.debugString} of mixin ${acc.owner} is missing a concrete super implementation in $base.", base.srcPos)

tests/neg/t12715.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
trait A { def f: String }
2+
trait B extends A { def f = "B" }
3+
trait C extends A { override val f = "C" }
4+
trait D extends C { override val f = "D" }
5+
trait E extends A, B { def d = super.f }
6+
final class O1 extends B, C, D, E // error: parent trait E has a super call which binds to the value D.f. Super calls can only target methods.
7+
final class O2 extends B, C, E, D // error: parent trait E has a super call which binds to the value C.f. Super calls can only target methods.
8+
final class O3 extends B, E, C, D
9+
10+
object Main:
11+
def main(args: Array[String]): Unit =
12+
println(O1().f) // D
13+
println(O2().f) // D
14+
println(O3().f) // D
15+
println(O3().d) // B
16+
O1().d // was: NoSuchMethodError: 'java.lang.String D.f$(D)'
17+
O2().d // was: NoSuchMethodError: 'java.lang.String C.f$(C)'

tests/neg/t12715b.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
trait B:
2+
def f: Float = 1.0f
3+
4+
class A(override val f: Float) extends B
5+
6+
trait C extends B:
7+
abstract override val f = super.f + 100.0f
8+
9+
trait D extends B:
10+
abstract override val f = super.f + 1000.0f
11+
12+
class ACD10 extends A(10.0f) with C with D // error: parent trait D has a super call to method B.f, which binds to the value C.f. Super calls can only target methods.
13+
14+
object Test:
15+
def main(args: Array[String]): Unit =
16+
new ACD10 // was: NoSuchMethodError: 'float C.f$(C)'

0 commit comments

Comments
 (0)