summaryrefslogtreecommitdiff
path: root/test/files/run/checked.scala
blob: e4db9c0916f2229c1ff2979e5a6ef0e06e44a01f (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
/* Test checked initializers. Needs to be run with -Xexperimental and -checkinit
 */

// 0 inherited fields
class A {
  val x = 1
  val y = 2
  var z = 3
}

// 3 inherited fields
class B extends A {
  val b1 = 1
  var b2 = 2
}


trait T {
  val t1 = 1
  var t2 = 2
}

// Should not throw
class D extends B with T {
  val sum = x + y + z + b1 + b2 + t1 + t2
  override def toString =
    "sum = " + sum

}

abstract class NeedsXEarly {
  val x: Int
  val y = x + 1
}

// should pass
class GoodX extends { val x = 1 } with NeedsXEarly {
}

// should throw
class BadX extends NeedsXEarly {
  val x = 1
  println(y)
}

// should pass
class UglyX extends NeedsXEarly {
  lazy val x = 1
  println(y)
}

trait XY {
  val x = 1
  val y = 2
}

// needs x and y early
trait LazyFields {
  lazy val lz1 = 1
  lazy val lz2 = 2
  val x: Int
  val y: Int
  val needsSomeEarly = {
    println("x = " + x)
    println("y = " + y)
    println("lz1 = " + lz1)
    println("lz2 = " + lz2)
    x + y + lz1 + lz2
  }
}

// will fail at init
class BadMixin extends LazyFields with XY {
  println("[OK]: " + needsSomeEarly)
}

// should print 24
class GoodMixin extends {
        override val x = 10
        override val y = 11
      } with LazyFields with XY {
  println("[OK]: " + needsSomeEarly)
}

class TestInterference extends {
  override val x = 10
  override val y = 11
} with A with T with LazyFields {
  println("[OK]: " + needsSomeEarly)
}


object Test extends App {

  def shouldThrow(t: => Unit) = try {
    t
    println("[FAIL]: No UFE thrown")
  } catch {
    case UninitializedFieldError(msg) =>
      println("[OK] Caught UFE: " + msg)
  }


  val d = new D()
  println(d)

  shouldThrow(new BadX)
  (new GoodX)
  (new UglyX)

  shouldThrow(new BadMixin)
  (new GoodMixin)

  (new TestInterference)
}