Skip to content

Commit 40cd899

Browse files
committed
Check that a self type T is closed.
What is checked: A self type T is a subtype of all selftypes of classes refernced by T. That is, a self type has to subsume all self types of its required type. Ot, otherwise said, requirements must be closed; you cannot discover new ones in following them.
1 parent 35151a1 commit 40cd899

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/dotty/tools/dotc/typer/RefChecks.scala

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,20 @@ object RefChecks {
7171
}
7272
}
7373

74-
/** Check that self type of this class conforms to self types of parents */
75-
private def checkSelfType(clazz: Symbol)(implicit ctx: Context): Unit = clazz.info match {
74+
/** Check that self type of this class conforms to self types of parents
75+
* and required classes.
76+
*/
77+
private def checkSelfType(cls: Symbol)(implicit ctx: Context): Unit = cls.info match {
7678
case cinfo: ClassInfo =>
77-
for (parent <- cinfo.classParents) {
78-
val pself = parent.givenSelfType.asSeenFrom(clazz.thisType, parent.classSymbol)
79-
if (pself.exists && !(cinfo.selfType <:< pself))
80-
ctx.error(d"illegal inheritance: self type ${cinfo.selfType} of $clazz does not conform to self type $pself of parent ${parent.classSymbol}", clazz.pos)
79+
def checkSelfConforms(other: TypeRef, category: String, relation: String) = {
80+
val otherSelf = other.givenSelfType.asSeenFrom(cls.thisType, other.classSymbol)
81+
if (otherSelf.exists && !(cinfo.selfType <:< otherSelf))
82+
ctx.error(d"$category: self type ${cinfo.selfType} of $cls does not conform to self type $otherSelf of $relation ${other.classSymbol}", cls.pos)
8183
}
84+
for (parent <- cinfo.classParents)
85+
checkSelfConforms(parent, "illegal inheritance", "parent")
86+
for (reqd <- cinfo.givenSelfType.classSymbols)
87+
checkSelfConforms(reqd.typeRef, "missing requirement", "required")
8288
case _ =>
8389
}
8490

test/dotc/tests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class tests extends CompilerTest {
136136
@Test def neg_moduleSubtyping = compileFile(negDir, "moduleSubtyping", xerrors = 4)
137137
@Test def neg_escapingRefs = compileFile(negDir, "escapingRefs", xerrors = 2)
138138
@Test def neg_instantiateAbstract = compileFile(negDir, "instantiateAbstract", xerrors = 8)
139-
@Test def neg_selfInheritance = compileFile(negDir, "selfInheritance", xerrors = 5)
139+
@Test def neg_selfInheritance = compileFile(negDir, "selfInheritance", xerrors = 6)
140140
@Test def neg_selfreq = compileFile(negDir, "selfreq", xerrors = 4)
141141
@Test def neg_shadowedImplicits = compileFile(negDir, "arrayclone-new", xerrors = 2)
142142
@Test def neg_traitParamsTyper = compileFile(negDir, "traitParamsTyper", xerrors = 5)

tests/neg/selfInheritance.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ object Test {
2626
object M extends C // error
2727

2828
}
29+
30+
trait X { self: Y => }
31+
trait Y { self: Z => }
32+
trait Z

0 commit comments

Comments
 (0)