Skip to content

Fix #5083: Disallow using trait parameters as prefix for its parents #5108

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 4 commits into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 20 additions & 4 deletions compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,37 @@ object RefChecks {
/** Check that self type of this class conforms to self types of parents.
* and required classes.
*/
private def checkParents(cls: Symbol)(implicit ctx: Context): Unit = cls.info match {
private def checkParents(cls: Symbol, tmpl: Template)(implicit ctx: Context): Unit = cls.info match {
case cinfo: ClassInfo =>
def checkSelfConforms(other: ClassSymbol, category: String, relation: String) = {
val otherSelf = other.givenSelfType.asSeenFrom(cls.thisType, other.classSymbol)
if (otherSelf.exists && !(cinfo.selfType <:< otherSelf))
ctx.error(DoesNotConformToSelfType(category, cinfo.selfType, cls, otherSelf, relation, other.classSymbol),
cls.pos)
}
for (parent <- cinfo.classParents)
checkSelfConforms(parent.classSymbol.asClass, "illegal inheritance", "parent")
for (parent <- tmpl.parents) {
checkSelfConforms(parent.tpe.classSymbol.asClass, "illegal inheritance", "parent")
checkParentPrefix(cls, parent)
}
for (reqd <- cinfo.cls.givenSelfType.classSymbols)
checkSelfConforms(reqd, "missing requirement", "required")
case _ =>
}

/** Disallow using trait parameters as prefix for its parents.
*
* The rationale is to ensure outer-related NPE never happen in Scala.
* Otherwise, outer NPE may happen, see tests/neg/i5083.scala
*/
private def checkParentPrefix(cls: Symbol, parent: Tree)(implicit ctx: Context): Unit =
parent.tpe.typeConstructor match {
case TypeRef(ref: TermRef, _) =>
val paramRefs = ref.namedPartsWith(ntp => ntp.symbol.enclosingClass == cls)
if (paramRefs.nonEmpty && cls.is(Trait))
ctx.error("trait parameters cannot be used as parent prefixes", parent.pos)
case _ =>
}

/** Check that a class and its companion object to not both define
* a class or module with same name
*/
Expand Down Expand Up @@ -960,7 +976,7 @@ class RefChecks extends MiniPhase { thisPhase =>
override def transformTemplate(tree: Template)(implicit ctx: Context) = try {
val cls = ctx.owner
checkOverloadedRestrictions(cls)
checkParents(cls)
checkParents(cls, tree)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use two different methods for checking parents:

checkParent(cls)
if (cls.is(Trait)) tree.parents.foreach(checkParentPrefix(cls, _))

That way the logic of checkParents can be left unchanged

checkCompanionNameClashes(cls)
checkAllOverrides(cls)
tree
Expand Down
25 changes: 25 additions & 0 deletions tests/neg/i5083.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class A(a: Int) {
abstract class X {
def f: Int
val x = a + f // NPE: the outer for `Y` is not yet set
}

trait Y {
val y = a
def f: Int = A.this.a // NPE: the outer for `Y` is not yet set
}

trait Z(val o: A) extends o.Y { // error
val z = a
}

class B extends X with Z(new A(4))
}


object Test {
def main(args: Array[String]): Unit = {
val a = new A(3)
new a.B
}
}
15 changes: 15 additions & 0 deletions tests/neg/i5083b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class A(a: Int) {
class X {
val x = a
}

trait Y {
val y = a
}

val z: Z = ???

trait Z(val o: A) extends z.o.Y // error: cyclic reference `z`

class B extends X with Z(new A(4))
}
25 changes: 25 additions & 0 deletions tests/neg/i5083c.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class A(a: Int) {
trait X {
def f: Int
val x = a + f // NPE: the outer for `Y` is not yet set
}

trait Y {
val y = a
def f: Int = A.this.a // NPE: the outer for `Y` is not yet set
}

class Z(o: A) extends m.Y { // error
val m = o
}

class B extends Z(new A(4))
}


object Test {
def main(args: Array[String]): Unit = {
val a = new A(3)
new a.B
}
}