Skip to content

Commit

Permalink
Merge pull request #10268 from lrytz/t12715
Browse files Browse the repository at this point in the history
Disallow mixins where super calls bind to vals
  • Loading branch information
lrytz committed Feb 21, 2023
2 parents eae0711 + 05e861b commit edfb564
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/compiler/scala/tools/nsc/transform/Mixin.scala
Expand Up @@ -357,6 +357,8 @@ abstract class Mixin extends Transform with ast.TreeDSL with AccessorSynthesis {
case alias1 =>
registerRequiredDirectInterface(alias1, clazz, parent =>
s"Unable to implement a super accessor required by trait ${mixinClass.name} unless $parent is directly extended by $clazz.")
if (alias1.isValue && !alias1.isMethod || alias1.isAccessor)
reporter.error(clazz.pos, s"parent $mixinClass has a super call to method ${mixinMember.alias.fullNameString}, which binds to the value ${alias1.fullNameString}. Super calls can only target methods.")
superAccessor.asInstanceOf[TermSymbol] setAlias alias1
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/files/neg/t12715.check
@@ -0,0 +1,7 @@
t12715.scala:21: error: parent trait E has a super call to method B.f, which binds to the value D.f. Super calls can only target methods.
object O1 extends B with C with D with E
^
t12715.scala:22: error: parent trait E has a super call to method B.f, which binds to the value C.f. Super calls can only target methods.
object O2 extends B with C with E with D
^
2 errors
23 changes: 23 additions & 0 deletions test/files/neg/t12715.scala
@@ -0,0 +1,23 @@
trait A {
def f: String
}

trait B extends A {
def f = "B";
}

trait C extends A {
override val f = "C"
}

trait D extends C {
override val f = "D"
}

trait E extends A with B {
def d = super.f
}

object O1 extends B with C with D with E
object O2 extends B with C with E with D
object O3 extends B with E with C with D
4 changes: 4 additions & 0 deletions test/files/neg/t12715b.check
@@ -0,0 +1,4 @@
t12715b.scala:17: 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.
new A(10.0f) with C with D {}
^
1 error
19 changes: 19 additions & 0 deletions test/files/neg/t12715b.scala
@@ -0,0 +1,19 @@
trait B {
def f: Float = 1.0f
}

class A(override val f: Float) extends B

trait C extends B {
abstract override val f = super.f + 100.0f
}

trait D extends B {
abstract override val f = super.f + 1000.0f
}

object Test {
def main(args: Array[String]): Unit = {
new A(10.0f) with C with D {}
}
}

0 comments on commit edfb564

Please sign in to comment.