Skip to content

Reject references to self in super constructor calls #12567

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

Merged
merged 3 commits into from
May 25, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class Compiler {
new ElimStaticThis, // Replace `this` references to static objects by global identifiers
new CountOuterAccesses) :: // Identify outer accessors that can be dropped
List(new DropOuterAccessors, // Drop unused outer accessors
new CheckNoSuperThis, // Check that supercalls don't contain references to `this`
new Flatten, // Lift all inner classes to package scope
new RenameLifted, // Renames lifted classes to local numbering scheme
new TransformWildcards, // Replace wildcards with default values
Expand Down
50 changes: 50 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/CheckNoSuperThis.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package dotty.tools.dotc
package transform

import core.*
import MegaPhase.MiniPhase
import Contexts.*, Types.*, Symbols.*, SymDenotations.*, Flags.*
import ast.*
import Trees.*
import Decorators.*

import annotation.threadUnsafe

object CheckNoSuperThis:
val name: String = "checkNoSuperThis"

/** Checks that super and this calls do not pass `this` as (part of) an argument. */
class CheckNoSuperThis extends MiniPhase:
thisPhase =>
import tpd._

override def phaseName: String = CheckNoSuperThis.name

override def runsAfterGroupsOf: Set[String] = Set(Constructors.name)

override def transformDefDef(mdef: DefDef)(using Context): DefDef =
if mdef.symbol.isClassConstructor then
mdef.rhs match
case Block(stats, _) => splitAtSuper(stats) match
case (Apply(_, superArgs) :: _, _) =>
val cls = mdef.symbol.owner
def fail(t: Tree) =
report.error(em"super constructor cannot be passed a self reference $t unless parameter is declared by-name", t.srcPos)
for arg <- superArgs do
arg.foreachSubTree {
case t: This if t.symbol == cls =>
fail(t)
case t: RefTree => t.tpe match
case tpe @ TermRef(prefix, _)
if (prefix == cls.thisType
|| cls.is(Module)
&& (prefix.termSymbol == cls.sourceModule || tpe.symbol == cls.sourceModule)
) && !tpe.symbol.is(JavaStatic) => fail(t)
case _ =>
case _ =>
}
case _ =>
case _ =>
mdef

end CheckNoSuperThis
12 changes: 0 additions & 12 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -991,18 +991,6 @@ trait Checking {
report.error(i"""$called is already implemented by super${caller.superClass},
|its constructor cannot be called again""", call.srcPos)

if (caller.is(Module)) {
val traverser = new TreeTraverser {
def traverse(tree: Tree)(using Context) = tree match {
case tree: RefTree if tree.isTerm && (tree.tpe.classSymbol eq caller) =>
report.error("super constructor cannot be passed a self reference", tree.srcPos)
case _ =>
traverseChildren(tree)
}
}
traverser.traverse(call)
}

// Check that constructor call is of the form _.<init>(args1)...(argsN).
// This guards against calls resulting from inserted implicits or applies.
def checkLegalConstructorCall(tree: Tree, encl: Tree, kind: String): Unit = tree match {
Expand Down
5 changes: 5 additions & 0 deletions tests/neg/i11045.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
abstract class Foo(x: Any)
class Boom(var x: Unit, y: Unit) extends Foo((x: Int) => x) // error: super constructor cannot be passed a self reference
@main def Test =
Boom((), ())

8 changes: 8 additions & 0 deletions tests/neg/i11208.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.reflect.ClassTag

@main def run = println(Foo)

abstract class Bar[T](implicit val thisClassTag: ClassTag[T])

class Foo
object Foo extends Bar[Foo] // error
6 changes: 6 additions & 0 deletions tests/neg/i11208a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Foo(implicit val foo: Foo)

object Test extends App {
implicit object Bar extends Foo // error
Bar.foo
}
14 changes: 14 additions & 0 deletions tests/neg/i12557.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package example

abstract class X[P <: Product](using val m: scala.deriving.Mirror.ProductOf[P]) {
def unapply(p: P): m.MirroredElemTypes = ???
}

case class A(a: Int)
object A extends X[A] // error

object Main {
def main(args: Array[String]): Unit = {
A.unapply(A(2))
}
}
2 changes: 0 additions & 2 deletions tests/pos/i11045.scala

This file was deleted.