File: foreach_async.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (53 lines) | stat: -rw-r--r-- 1,946 bytes parent folder | download
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
// RUN: %target-typecheck-verify-swift -target %target-swift-5.1-abi-triple
// REQUIRES: concurrency
import _Concurrency

struct AsyncRange<Bound: Comparable & Strideable>: AsyncSequence, AsyncIteratorProtocol where Bound.Stride : SignedInteger {
  var range: Range<Bound>.Iterator
  typealias Element = Bound
  mutating func next() async -> Element? { return range.next() }
  func cancel() { }

  func makeAsyncIterator() -> Self { return self }
}

struct AsyncIntRange<Int> : AsyncSequence, AsyncIteratorProtocol {
  typealias Element = (Int, Int)
  func next() async -> (Int, Int)? {}

  func cancel() { }

  typealias AsyncIterator = AsyncIntRange<Int>
  func makeAsyncIterator() -> AsyncIntRange<Int> { return self }
}

func for_each(r: AsyncRange<Int>, iir: AsyncIntRange<Int>) async { // expected-note {{'r' declared here}}
  var sum = 0

  // Simple foreach loop, using the variable in the body
  for await i in r {
    sum = sum + i
  }
  // Check scoping of variable introduced with foreach loop
  i = 0 // expected-error{{cannot find 'i' in scope; did you mean 'r'?}}

  // For-each loops with two variables and varying degrees of typedness
  for await (i, j) in iir {
    sum = sum + i + j
  }
  for await (i, j) in iir {
    sum = sum + i + j
  }
  for await (i, j) : (Int, Int) in iir {
    sum = sum + i + j
  }

  // Parse errors
  // FIXME: Bad diagnostics; should be just 'expected 'in' after for-each patter'.
  for await i r { // expected-error {{found an unexpected second identifier in constant declaration}}
  }         // expected-note @-1 {{join the identifiers together}}
            // expected-note @-2 {{join the identifiers together with camel-case}}
            // expected-error @-3 {{expected 'in' after for-each pattern}}
            // expected-error @-4 {{expected Sequence expression for for-each loop}}
  for await i in r sum = sum + i; // expected-error{{expected '{' to start the body of for-each loop}}
}