File tree Expand file tree Collapse file tree 3 files changed +41
-0
lines changed
src/dotty/tools/dotc/typer Expand file tree Collapse file tree 3 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -71,6 +71,17 @@ object RefChecks {
71
71
}
72
72
}
73
73
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 {
76
+ 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)
81
+ }
82
+ case _ =>
83
+ }
84
+
74
85
// Override checking ------------------------------------------------------------
75
86
76
87
/** 1. Check all members of class `clazz` for overriding conditions.
@@ -770,6 +781,7 @@ class RefChecks extends MiniPhase with SymTransformer { thisTransformer =>
770
781
override def transformTemplate (tree : Template )(implicit ctx : Context , info : TransformerInfo ) = {
771
782
val cls = ctx.owner
772
783
checkOverloadedRestrictions(cls)
784
+ checkSelfType(cls)
773
785
checkAllOverrides(cls)
774
786
checkAnyValSubclass(cls)
775
787
tree
Original file line number Diff line number Diff line change @@ -126,6 +126,7 @@ class tests extends CompilerTest {
126
126
@ Test def neg_i0281 = compileFile(negDir, " i0281-null-primitive-conforms" , xerrors = 3 )
127
127
@ Test def neg_moduleSubtyping = compileFile(negDir, " moduleSubtyping" , xerrors = 4 )
128
128
@ Test def neg_escapingRefs = compileFile(negDir, " escapingRefs" , xerrors = 2 )
129
+ @ Test def neg_selfInheritance = compileFile(negDir, " selfInheritance" , xerrors = 5 )
129
130
130
131
@ Test def dotc = compileDir(dotcDir + " tools/dotc" , failedOther)(allowDeepSubtypes ++ twice) // see dotc_core
131
132
@ Test def dotc_ast = compileDir(dotcDir + " tools/dotc/ast" , failedOther ++ twice)
Original file line number Diff line number Diff line change
1
+ trait T { self : B => }
2
+
3
+ abstract class A { self : B =>
4
+
5
+ }
6
+
7
+ class B extends A with T {
8
+ }
9
+
10
+ class C { self : B =>
11
+
12
+ }
13
+
14
+ class D extends A // error
15
+
16
+ class E extends T // error
17
+
18
+ object Test {
19
+
20
+ new B () {}
21
+
22
+ new A () {} // error
23
+
24
+ object O extends A // error
25
+
26
+ object M extends C // error
27
+
28
+ }
You can’t perform that action at this time.
0 commit comments