summaryrefslogtreecommitdiff
path: root/test/files/run/inline-ex-handlers.scala
blob: 964594d258b520c34bb65c5b739ffb1adbdc9c29 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import scala.tools.partest.IcodeComparison

object Test extends IcodeComparison {
  override def printIcodeAfterPhase = "inlinehandlers"
}

import scala.util.Random._

/** There should be no inlining taking place in this class */
object TestInlineHandlersNoInline {

  def main(args: Array[String]): Unit = {
    println("TestInlineHandlersNoInline")
    var result = -1

    try {
      if (nextInt % 2 == 0)
      throw new IllegalArgumentException("something")
      result = 1
    } catch {
      case e: StackOverflowError =>
      println("Stack overflow")
    }

    result
  }
}

/** Just a simple inlining should take place in this class */
object TestInlineHandlersSimpleInline {

  def main(args: Array[String]): Unit = {
    println("TestInlineHandlersSimpleInline")
    var result = -1

    try {
      if (nextInt % 2 == 0)
      throw new IllegalArgumentException("something")
      result = 1
    } catch {
      case e: IllegalArgumentException =>
      println("IllegalArgumentException")
    }

    result
  }
}

/** Inlining should take place because the handler is taking a superclass of the exception thrown */
object TestInlineHandlersSubclassInline {

  def main(args: Array[String]): Unit = {
    println("TestInlineHandlersSubclassInline")
    var result = -1

    try {
      if (nextInt % 2 == 0)
      throw new IllegalArgumentException("something")
      result = 1
    } catch {
      case e: RuntimeException =>
      println("RuntimeException")
    }

    result
  }
}

/** For this class, the finally handler should be inlined */
object TestInlineHandlersFinallyInline {

  def main(args: Array[String]): Unit = {
    println("TestInlineHandlersFinallyInline")
    var result = -1

    try {
      if (nextInt % 2 == 0)
      throw new IllegalArgumentException("something")
      result = 1
    } catch {
      case e: Exception => throw e
    } finally {
      println("finally")
      result = (result - 1) / 2
    }

    result
  }
}


case class MyException(message: String) extends RuntimeException(message)

/** For this class, we test inlining for a case class error */
object TestInlineHandlersCaseClassExceptionInline {

  def main(args: Array[String]): Unit = {
    println("TestInlineHandlersCaseClassExceptionInline")
    var result = -1

    try {
      if (nextInt % 2 == 0)
      throw new MyException("something")
      result = 1
    } catch {
      case MyException(message) => println(message)
    }

    result
  }
}


/** For this class, inline should take place in the inner handler */
object TestInlineHandlersNestedHandlerInnerInline {

  def main(args: Array[String]): Unit = {
    println("TestInlineHandlersNestedHandlersInnerInline")
    var result = -1

    try {
      try {
        if (nextInt % 2 == 0)
        throw new MyException("something")
        result = 1
      } catch {
        case MyException(message) => println(message)
      }
    } catch {
      case e: IllegalArgumentException => println("IllegalArgumentException")
    }

    result
  }
}


/** For this class, inline should take place in the outer handler */
object TestInlineHandlersNestedHandlerOuterInline {

  def main(args: Array[String]): Unit = {
    println("TestInlineHandlersNestedHandlersOuterInline")
    var result = -1

    try {
      try {
        if (nextInt % 2 == 0)
        throw new MyException("something")
        result = 1
      } catch {
        case e: IllegalArgumentException => println("IllegalArgumentException")
      }
    } catch {
      case MyException(message) => println(message)
    }

    result
  }
}


/** For this class, inline should take place in the all handlers (inner, outer and finally) */
object TestInlineHandlersNestedHandlerAllInline {

  def main(args: Array[String]): Unit = {
    println("TestInlineHandlersNestedHandlersOuterInline")
    var result = -1

    try {
      try {
        if (nextInt % 2 == 0)
        throw new MyException("something")
        result = 1
      } catch {
        case MyException(message) =>
        println(message)
        throw MyException(message)
      }
    } catch {
      case MyException(message) =>
      println(message)
      throw MyException(message)
    } finally {
      println("finally")
      result = (result - 1) / 2
    }

    result
  }
}


/** This class is meant to test whether the inline handler is copied only once for multiple inlines */
object TestInlineHandlersSingleCopy {

  def main(args: Array[String]): Unit = {
    println("TestInlineHandlersSingleCopy")
    var result = -1

    try {

      if (nextInt % 2 == 0)
      throw new MyException("something")

      println("A side effect in the middle")
      result = 3 // another one

      if (nextInt % 3 == 2)
      throw new MyException("something else")
      result = 1
    } catch {
      case MyException(message) =>
      println(message)
    }

    result
  }
}

/** This should test the special exception handler for synchronized blocks */
object TestInlineHandlersSynchronized {

  def main(args: Array[String]): Unit = {
    println("TestInlineHandlersSynchronized")
    var result = "hello"

    // any exception thrown here will be caught by a default handler that does MONTIOR_EXIT on result :)
    result.synchronized {
      throw MyException(result)
    }

    result.length
  }
}

/** This should test the special exception handler for synchronized blocks with stack */
object TestInlineHandlersSynchronizedWithStack {

  def main(args: Array[String]): Unit = {
    println("TestInlineHandlersSynchronizedWithStack")
    var result = "hello"

    // any exception thrown here will be caught by a default handler that does MONTIOR_EXIT on result :)
    result = "abc" + result.synchronized {
      throw MyException(result)
    }

    result.length
  }
}

/** This test should trigger a bug in the dead code elimination phase - it actually crashes ICodeCheckers
object TestInlineHandlersSynchronizedWithStackDoubleThrow {

  def main(args: Array[String]): Unit = {
    println("TestInlineHandlersSynchronizedWithStackDoubleThrow")
    var result = "a"

    // any exception thrown here will be caught by a default handler that does MONTIOR_EXIT on result :)
    result += result.synchronized { throw MyException(result) }
    result += result.synchronized { throw MyException(result) }

    result.length
  }
}
*/

/** This test should check the preciseness of the inliner: it should not do any inlining here
* as it is not able to discern between the different exceptions
*/
object TestInlineHandlersPreciseness {

  def main(args: Array[String]): Unit = {
    println("TestInlineHandlersCorrectHandler")

    try {
      val exception: Throwable =
      if (scala.util.Random.nextInt % 2 == 0)
      new IllegalArgumentException("even")
      else
      new StackOverflowError("odd")
      throw exception
    } catch {
      case e: IllegalArgumentException =>
      println("Correct, IllegalArgumentException")
      case e: StackOverflowError =>
      println("Correct, StackOverflowException")
      case t: Throwable =>
      println("WROOOONG, not Throwable!")
    }
  }
}

/** This check should verify that the double no-local exception handler is duplicated correctly */
object TestInlineHandlersDoubleNoLocal {

  val a1: String = "a"
  val a2: String = "b"

  def main(args: Array[String]): Unit = {
    println("TestInlineHandlersDoubleNoLocal")

    try {
      a1.synchronized {
        a2. synchronized {
          throw new MyException("crash")
        }
      }
    } catch {
      case t: Throwable => println("Caught crash: " + t.toString)
    }

    /*        try {
      val exception: Throwable =
      if (scala.util.Random.nextInt % 2 == 0)
      new IllegalArgumentException("even")
      else
      new StackOverflowError("odd")
      throw exception
    } catch {
      case e: IllegalArgumentException =>
      println("Correct, IllegalArgumentException")
      case e: StackOverflowError =>
      println("Correct, StackOverflowException")
      case t: Throwable =>
      println("WROOOONG, not Throwable!")
    }*/
  }
}