aboutsummaryrefslogtreecommitdiff
path: root/tests/neg/overrides.scala
blob: 149220bd560d7453a72d3ec894f6ee9753af22ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
class Foo {
  type A = Int
  type B >: Int <: Int
  def get: A = 42
}
trait T {
  lazy val x: Int
  val y: Int
}
class Bar extends Foo with T {
  override type A = Any   // error
  type B >: String <: Any  // error
  override def get: A = "bar"
  val x = 2  // error
  lazy val y = 3 // error
}
object Test {
  def main(args: Array[String]): Unit = {
    val foo: Foo = new Bar
    val i: Int = foo.get
  }
}

package p1 {
  abstract class T1 {
    protected def bug(p: Int = 1): Int // without 'protected' compiles fine
  }
}
package p2 { // all being in the same package compiles fine
  import p1._
  abstract class T2 extends T1 {
    class A {
      bug()
    }
  }

}

class A[T] {

  def f(x: T)(y: T = x) = y

}

class B extends A[Int] {

  def f(x: Int)(y: Int) = y // error: needs `override' modifier

  f(2)()

}

class X {
  def f: A[Int] = ???
}
class Y extends X {
  def f: A[Int] = ??? // error: needs `override' modifier
}


class A1
class B1

class X1 {
  def f(): A1 = ???
}
class Y1 extends X1 {
  override def f(): B1 = ??? // error: has incompatible type
}

class X2 {
  type T = A1
}
class Y2 extends X2 {
  type T = B1 // error: needs `override' modifier
}

class X3 {
  override type T = A1 // error: overrides nothing
}

package p3 {

// Dotty change of rules: Toverrider#f does not
// override TCommon#f, hence the accidental override rule
// applies.
trait TCommon {
  def f: String
}

class C1 extends TCommon {
  def f = "in C1"
}

trait TOverrider { this: TCommon =>
  override def f = "in TOverrider"   // The overridden self-type member...
}

class C2 extends C1 with TOverrider  // ... fails to override, here. // error: accidental override

}

package p4 {

  abstract class C[T] { def head: T }
  case class D[T](head: Int) extends C[T] // error: has incompatible type

}

package p5 {
class A {
  def m: String = "foo"
}

class B extends A {
  override val m: Int = 42 // error: has incompatible type
}

class C extends A {
  override def m: Int = 42 // error: has incompatible type
}
}