summaryrefslogtreecommitdiff
path: root/test/files/run/t9697.scala
blob: eb8e44f8fc33280ef8b5fd33fb23f83d81101915 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
object log {
  val b = new collection.mutable.StringBuilder
  def apply(s: Any): Unit = b.append(s)
  def check(s: String) = {
    val bs = b.toString
    assert(s == bs, bs)
    b.clear()
  }
}

package t9697 {
  abstract class WA extends DelayedInit {
    override def delayedInit(x: => Unit): Unit = x
    val waField = "4"
  }

  class C {
    def b(s: String) = log(s)
    val cField = "1"

    {
      val dummyLocal = "2"
      new WA {
        val anonField = "3"
        b(cField)
        b(dummyLocal)
        b(anonField)
        b(waField)
      }
    }
  }
}

package sd229 {
  class Broken {
    def is(ee: AnyRef) = {
      new Delayed {
        log(ee)
      }
    }
  }

  class Delayed extends DelayedInit {
    def delayedInit(x: => Unit): Unit = x
  }
}


// already fixed in 2.11.8, crashes in 2.10.6
package t4683a {
  class A { log("a") }
  class B { log("b") }
  class Bug extends DelayedInit {
    log("bug")
    def foo(a: A): B = new B
    def delayedInit(init: => Unit): Unit = init
  }
}

// already fixed in 2.12.0-RC1, crashes in 2.11.8
package t4683b {
  class Entity extends DelayedInit {
    def delayedInit(x: => Unit): Unit = x
    
    class Field
    
    protected def EntityField[T <: Entity: reflect.ClassTag] = new Field
    
    def find[T <: Entity: reflect.ClassTag] {
      Nil.map(dbo => {
        class EntityHolder extends Entity {
          val entity = EntityField[T]
        }
      })
      log("find")
    }
  }
}

package t4683c {
  trait T extends DelayedInit {
    def delayedInit(body: => Unit) = {
      log("init")
      body
    }
  }
}

package t4683d {
  class C extends DelayedInit {
    def delayedInit(body: => Unit): Unit = body
  }
  class Injector {
    def test: Object = {
      val name = "k"
      class crash extends C {
        log(name)
      }
      new crash()
    }
  }
}

package t4683e {
  class DelayedInitTest {
    def a = log("uh")
    class B extends DelayedInit {
      a
      def delayedInit(body: => Unit): Unit = body
    }
  }
}

package t4683f {
  class Foo extends DelayedInit {
    log("fooInit")
    def delayedInit(newBody: => Unit): Unit = {
      log("delayedInit")
      inits = {
        val f = () => newBody
        if (inits == null) {
          log("initsNull")
          List(f)
        } else
          f :: inits
      }
    }
    def foo = log("foo")
    var inits: List[() => Unit] = Nil
  }
  
  class Bar extends Foo {
    log("barInit")
    def bar = foo
    def newBaz: Foo = new Baz 
    private class Baz extends Foo {
      log("bazInit")
      bar
    }
  }
}

package t4683g {
  trait MatExpWorld { self =>
    class T extends Runner { val expWorld: self.type = self }
  }
  
  trait Runner extends DelayedInit {
    def delayedInit(init: => Unit): Unit = init
    val expWorld: MatExpWorld
  }
}


object Test extends App {
  new t9697.C()
  log.check("1234")

  new sd229.Broken().is("hi")
  log.check("hi")

  val a: t4683a.A = new t4683a.A
  var b: t4683a.B = null
  new t4683a.Bug {
    val b = foo(a)
  }
  log.check("abugb")

  new t4683b.Entity().find[t4683b.Entity]
  log.check("find")

  val f = (p1: Int) => new t4683c.T { log(p1) }
  f(5)
  log.check("init5")

  new t4683d.Injector().test
  log.check("k")

  val dit = new t4683e.DelayedInitTest()
  new dit.B()
  log.check("uh")

  val fuu = new t4683f.Foo
  log.check("delayedInitinitsNull")
  fuu.inits.foreach(_.apply())
  log.check("fooInit")
  assert(fuu.inits == Nil) // the (delayed) initializer of Foo sets the inits field to Nil

  val brr = new t4683f.Bar
  log.check("delayedInitinitsNulldelayedInit") // delayedInit is called once for each constructor
  brr.inits.foreach(_.apply())
  log.check("barInitfooInit")
  assert(brr.inits == Nil)

  val bzz = brr.newBaz
  log.check("delayedInitinitsNulldelayedInit")
  bzz.inits.foreach(_.apply())
  log.check("bazInitfoofooInit")
  assert(bzz.inits == Nil)

  val mew = new t4683g.MatExpWorld { }
  val mt = new mew.T
  assert(mt.expWorld == mew)
}