Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow mixins where super calls bind to vals #10268

Merged
merged 1 commit into from Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 {}
}
}