aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/scala/async/run/anf/AnfTransformSpec.scala
blob: 29b7c1d00a7ed5b11476ad372b5ec213a423670f (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
 * Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
 */

package scala.async
package run
package anf

import language.{reflectiveCalls, postfixOps}
import scala.concurrent.{Future, ExecutionContext, future, Await}
import scala.concurrent.duration._
import scala.async.Async.{async, await}
import org.junit.Test
import scala.async.internal.AsyncId


class AnfTestClass {

  import ExecutionContext.Implicits.global

  def base(x: Int): Future[Int] = future {
    x + 2
  }

  def m(y: Int): Future[Int] = async {
    val blerg = base(y)
    await(blerg)
  }

  def m2(y: Int): Future[Int] = async {
    val f = base(y)
    val f2 = base(y + 1)
    await(f) + await(f2)
  }

  def m3(y: Int): Future[Int] = async {
    val f = base(y)
    var z = 0
    if (y > 0) {
      z = await(f) + 2
    } else {
      z = await(f) - 2
    }
    z
  }

  def m4(y: Int): Future[Int] = async {
    val f = base(y)
    val z = if (y > 0) {
      await(f) + 2
    } else {
      await(f) - 2
    }
    z + 1
  }

  def futureUnitIfElse(y: Int): Future[Unit] = async {
    val f = base(y)
    if (y > 0) {
      State.result = await(f) + 2
    } else {
      State.result = await(f) - 2
    }
  }
}

object State {
  @volatile var result: Int = 0
}

class AnfTransformSpec {

  @Test
  def `simple ANF transform`() {
    val o = new AnfTestClass
    val fut = o.m(10)
    val res = Await.result(fut, 2 seconds)
    res mustBe (12)
  }

  @Test
  def `simple ANF transform 2`() {
    val o = new AnfTestClass
    val fut = o.m2(10)
    val res = Await.result(fut, 2 seconds)
    res mustBe (25)
  }

  @Test
  def `simple ANF transform 3`() {
    val o = new AnfTestClass
    val fut = o.m3(10)
    val res = Await.result(fut, 2 seconds)
    res mustBe (14)
  }

  @Test
  def `ANF transform of assigning the result of an if-else`() {
    val o = new AnfTestClass
    val fut = o.m4(10)
    val res = Await.result(fut, 2 seconds)
    res mustBe (15)
  }

  @Test
  def `Unit-typed if-else in tail position`() {
    val o = new AnfTestClass
    val fut = o.futureUnitIfElse(10)
    Await.result(fut, 2 seconds)
    State.result mustBe (14)
  }

  @Test
  def `inlining block does not produce duplicate definition`() {
    AsyncId.async {
      val f = 12
      val x = AsyncId.await(f)

      {
        type X = Int
        val x: X = 42
        println(x)
      }
      type X = Int
      x: X
    }
  }

  @Test
  def `inlining block in tail position does not produce duplicate definition`() {
    AsyncId.async {
      val f = 12
      val x = AsyncId.await(f)

      {
        val x = 42
        x
      }
    } mustBe (42)
  }

  @Test
  def `match as expression 1`() {
    import ExecutionContext.Implicits.global
    val result = AsyncId.async {
      val x = "" match {
        case _ => AsyncId.await(1) + 1
      }
      x
    }
    result mustBe (2)
  }

  @Test
  def `match as expression 2`() {
    import ExecutionContext.Implicits.global
    val result = AsyncId.async {
      val x = "" match {
        case "" if false => AsyncId.await(1) + 1
        case _           => 2 + AsyncId.await(1)
      }
      val y = x
      "" match {
        case _ => AsyncId.await(y) + 100
      }
    }
    result mustBe (103)
  }

  @Test
  def nestedAwaitAsBareExpression() {
    import ExecutionContext.Implicits.global
    import AsyncId.{async, await}
    val result = async {
      await(await("").isEmpty)
    }
    result mustBe (true)
  }

  @Test
  def nestedAwaitInBlock() {
    import ExecutionContext.Implicits.global
    import AsyncId.{async, await}
    val result = async {
      ()
      await(await("").isEmpty)
    }
    result mustBe (true)
  }

  @Test
  def nestedAwaitInIf() {
    import ExecutionContext.Implicits.global
    import AsyncId.{async, await}
    val result = async {
      if ("".isEmpty)
        await(await("").isEmpty)
      else 0
    }
    result mustBe (true)
  }

  @Test
  def byNameExpressionsArentLifted() {
    import AsyncId.{async, await}
    def foo(ignored: => Any, b: Int) = b
    val result = async {
      foo(???, await(1))
    }
    result mustBe (1)
  }

  @Test
  def evaluationOrderRespected() {
    import AsyncId.{async, await}
    def foo(a: Int, b: Int) = (a, b)
    val result = async {
      var i = 0
      def next() = {
        i += 1
        i
      }
      foo(next(), await(next()))
    }
    result mustBe ((1, 2))
  }

  @Test
  def awaitInNonPrimaryParamSection1() {
    import AsyncId.{async, await}
    def foo(a0: Int)(b0: Int) = s"a0 = $a0, b0 = $b0"
    val res = async {
      var i = 0
      def get = {i += 1; i}
      foo(get)(await(get))
    }
    res mustBe "a0 = 1, b0 = 2"
  }

  @Test
  def awaitInNonPrimaryParamSection2() {
    import AsyncId.{async, await}
    def foo[T](a0: Int)(b0: Int*) = s"a0 = $a0, b0 = ${b0.head}"
    val res = async {
      var i = 0
      def get = async {i += 1; i}
      foo[Int](await(get))(await(get) :: await(async(Nil)) : _*)
    }
    res mustBe "a0 = 1, b0 = 2"
  }

  @Test
  def awaitInNonPrimaryParamSectionWithLazy1() {
    import AsyncId.{async, await}
    def foo[T](a: => Int)(b: Int) = b
    val res = async {
      def get = async {0}
      foo[Int](???)(await(get))
    }
    res mustBe 0
  }

  @Test
  def awaitInNonPrimaryParamSectionWithLazy2() {
    import AsyncId.{async, await}
    def foo[T](a: Int)(b: => Int) = a
    val res = async {
      def get = async {0}
      foo[Int](await(get))(???)
    }
    res mustBe 0
  }

  @Test
  def awaitWithLazy() {
    import AsyncId.{async, await}
    def foo[T](a: Int, b: => Int) = a
    val res = async {
      def get = async {0}
      foo[Int](await(get), ???)
    }
    res mustBe 0
  }

  @Test
  def awaitOkInReciever() {
    import AsyncId.{async, await}
    class Foo { def bar(a: Int)(b: Int) = a + b }
    async {
      await(async(new Foo)).bar(1)(2)
    }
  }

  @Test
  def namedArgumentsRespectEvaluationOrder() {
    import AsyncId.{async, await}
    def foo(a: Int, b: Int) = (a, b)
    val result = async {
      var i = 0
      def next() = {
        i += 1
        i
      }
      foo(b = next(), a = await(next()))
    }
    result mustBe ((2, 1))
  }

  @Test
  def namedAndDefaultArgumentsRespectEvaluationOrder() {
    import AsyncId.{async, await}
    var i = 0
    def next() = {
      i += 1
      i
    }
    def foo(a: Int = next(), b: Int = next()) = (a, b)
    async {
      foo(b = await(next()))
    } mustBe ((2, 1))
    i = 0
    async {
      foo(a = await(next()))
    } mustBe ((1, 2))
  }

  @Test
  def repeatedParams1() {
    import AsyncId.{async, await}
    var i = 0
    def foo(a: Int, b: Int*) = b.toList
    def id(i: Int) = i
    async {
      foo(await(0), id(1), id(2), id(3), await(4))
    } mustBe (List(1, 2, 3, 4))
  }

  @Test
  def repeatedParams2() {
    import AsyncId.{async, await}
    var i = 0
    def foo(a: Int, b: Int*) = b.toList
    def id(i: Int) = i
    async {
      foo(await(0), List(id(1), id(2), id(3)): _*)
    } mustBe (List(1, 2, 3))
  }

  @Test
  def awaitInThrow() {
    import _root_.scala.async.internal.AsyncId.{async, await}
    intercept[Exception](
      async {
        throw new Exception("msg: " + await(0))
      }
    ).getMessage mustBe "msg: 0"
  }

  @Test
  def awaitInTyped() {
    import _root_.scala.async.internal.AsyncId.{async, await}
    async {
      (("msg: " + await(0)): String).toString
    } mustBe "msg: 0"
  }


  @Test
  def awaitInAssign() {
    import _root_.scala.async.internal.AsyncId.{async, await}
    async {
      var x = 0
      x = await(1)
      x
    } mustBe 1
  }

  @Test
  def caseBodyMustBeTypedAsUnit() {
    import _root_.scala.async.internal.AsyncId.{async, await}
    val Up = 1
    val Down = 2
    val sign = async {
      await(1) match {
        case Up   => 1.0
        case Down => -1.0
      }
    }
    sign mustBe 1.0
  }

  @Test
  def awaitInImplicitApply() {
    val tb = mkToolbox(s"-cp ${toolboxClasspath}")
    val tree = tb.typeCheck(tb.parse {
      """
        | import language.implicitConversions
        | import _root_.scala.async.internal.AsyncId.{async, await}
        | implicit def view(a: Int): String = ""
        | async {
        |   await(0).length
        | }
      """.stripMargin
    })
    val applyImplicitView = tree.collect { case x if x.getClass.getName.endsWith("ApplyImplicitView") => x }
    applyImplicitView.map(_.toString) mustStartWith List("view(a$macro$")
  }

  @Test
  def nothingTypedIf(): Unit = {
    import scala.async.internal.AsyncId.{async, await}
    val result = util.Try(async {
      if (true) {
        val n = await(1)
        if (n < 2) {
          throw new RuntimeException("case a")
        }
        else {
          throw new RuntimeException("case b")
        }
      }
      else {
        "case c"
      }
    })

    assert(result.asInstanceOf[util.Failure[_]].exception.getMessage == "case a")
  }

  @Test
  def nothingTypedMatch(): Unit = {
    import scala.async.internal.AsyncId.{async, await}
    val result = util.Try(async {
      0 match {
        case _ if "".isEmpty =>
          val n = await(1)
          n match {
            case _ if n < 2 =>
              throw new RuntimeException("case a")
            case _ =>
              throw new RuntimeException("case b")
          }
        case _ =>
          "case c"
      }
    })

    assert(result.asInstanceOf[util.Failure[_]].exception.getMessage == "case a")
  }
}