File tree Expand file tree Collapse file tree 3 files changed +51
-2
lines changed
compiler/src/dotty/tools/dotc/typer Expand file tree Collapse file tree 3 files changed +51
-2
lines changed Original file line number Diff line number Diff line change 1
- package dotty .tools .dotc
1
+ package dotty .tools
2
+ package dotc
2
3
package typer
3
4
4
5
import transform ._
@@ -800,7 +801,34 @@ object RefChecks {
800
801
report.error(problem(), clazz.srcPos)
801
802
}
802
803
804
+ // check that basetype and subtype agree on types of trait parameters
805
+ //
806
+ // I.e. trait and class parameters not only need to conform to the expected
807
+ // type of the corresponding base-trait, but also to the type as seen by the
808
+ // inheriting subtype.
809
+ def checkTraitParametersOK () = for {
810
+ parent <- clazz.info.parents
811
+ parentSym = parent.classSymbol
812
+ if parentSym.isClass
813
+ cls = parentSym.asClass
814
+ if cls.paramAccessors.nonEmpty
815
+ param <- cls.paramAccessors
816
+ } {
817
+ val tpeFromParent = parent.memberInfo(param)
818
+ val tpeFromClazz = clazz.thisType.memberInfo(param)
819
+ if (! (tpeFromParent <:< tpeFromClazz)) {
820
+ val msg =
821
+ em """ illegal parameter: The types of $param do not match.
822
+ |
823
+ | $param in $cls has type: $tpeFromParent
824
+ | but $clazz expects $param to have type: $tpeFromClazz"""
825
+
826
+ report.error(msg, clazz.srcPos)
827
+ }
828
+ }
829
+
803
830
checkParameterizedTraitsOK()
831
+ checkTraitParametersOK()
804
832
}
805
833
806
834
/** Check that `site` does not inherit conflicting generic instances of `baseCls`,
Original file line number Diff line number Diff line change
1
+ trait Foo {
2
+ def name : String
3
+ }
4
+ class Bar
5
+
6
+ trait CClass [+ A ](val a : A ) {
7
+ val b = a
8
+ }
9
+
10
+ trait CTrait [+ A ](val a : A ) {
11
+ val b = a
12
+ }
13
+ trait DTrait [+ B ] extends CTrait [B ]
14
+ trait DClass [+ B ] extends CClass [B ]
15
+
16
+ final class F1 extends DTrait [Foo ] with CTrait [Bar ](new Bar ) // error: illegal parameter
17
+ final class F2 extends CTrait [Bar ](new Bar ) with DTrait [Foo ] // error: illegal parameter
18
+ final class F3 extends DClass [Foo ] with CClass [Bar ](new Bar ) // error: illegal parameter
19
+ final class F4 extends CClass [Bar ](new Bar ) with DClass [Foo ] // error: illegal parameter
20
+
21
+ final class F5 extends DTrait [Foo ] with CTrait [Foo & Bar ](new Bar with Foo { def name = " hello" }) // ok
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ object Test extends App {
3
3
class B [+ X ](val y : X ) extends A [X ](y)
4
4
class C extends B (5 ) with A [String ] // error: illegal inheritance
5
5
6
- class D extends B (5 ) with A [Any ] // ok
6
+ class D extends B (5 ) with A [Any ] // error: illegal parameter
7
7
8
8
def f (a : A [Int ]): String = a match {
9
9
case c : C => c.x
You can’t perform that action at this time.
0 commit comments