-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
compiler/src/dotty/tools/dotc/transform/CheckNoSuperThis.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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((), ()) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.