<commit_before>// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency %import-libdispatch -parse-as-library) | %FileCheck %s --dump-input=always

// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: libdispatch

struct Boom: Error {}
struct IgnoredBoom: Error {}

func one() async -> Int { 1 }

func boom() async throws -> Int {
  throw Boom()
}

func test_taskGroup_throws() async {
  do {
    print("start \(#function)")
    let got = try! await Task.withGroup(resultType: Int.self) {
      group async throws -> Int in
      await group.add { await one() }
      await group.add { try await boom()  }

      do {
        while let r = try await group.next() {
          print("next: \(r)")
        }
      } catch {
        print("error caught in group: \(error)")

        await group.add { () async -> Int in
          let c = await Task.__unsafeCurrentAsync().isCancelled
          print("task 3 (cancelled: \(c))")
          return 3
        }

        guard let got = try! await group.next() else {
          print("task group failed to get 3 (:\(#line))")
          return 0
        }

        print("task group next: \(got)")

        if got == 1 {
          // the previous 1 completed before the 3 we just submitted,
          // we still want to see that three so let's await for it
          guard let third = try! await group.next() else {
            print("task group failed to get 3 (:\(#line))")
            return got
          }

          print("task group returning normally: \(third)")
          return third
        } else {
          print("task group returning normally: \(got)")
          return got
        }
      }

      fatalError("Should have thrown and handled inside the catch block")
    }

    // Optionally, we may get the first result back before the failure:
    // COM: next: 1
    // CHECK: error caught in group: Boom()
    // CHECK: task 3 (cancelled: false)
    // CHECK: task group returning normally: 3
    // CHECK: got: 3

    print("got: \(got)")
  } catch {
    print("rethrown: \(error)")
    fatalError("Expected recovered result, but got error: \(error)")
  }
}


@main struct Main {
  static func main() async {
    await test_taskGroup_throws()
  }<commit_msg>[TaskGroup] simplify throw_recover test a little bit<commit_after>// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency %import-libdispatch -parse-as-library) | %FileCheck %s --dump-input=always

// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: libdispatch

struct Boom: Error {}
struct IgnoredBoom: Error {}

func one() async -> Int { 1 }

func boom() async throws -> Int {
  throw Boom()
}

func test_taskGroup_throws() async {
  let got: Int = try! await Task.withGroup(resultType: Int.self) { group in
    await group.add { try await boom()  }

    do {
      while let r = try await group.next() {
        print("next: \(r)")
      }
    } catch {
      print("error caught in group: \(error)")

      let gc = group.isCancelled
      print("group cancelled: \(gc)")

      await group.add { () async -> Int in
        let c = await Task.__unsafeCurrentAsync().isCancelled
        print("task 3 (cancelled: \(c))")
        return 3
      }

      guard let third = try! await group.next() else {
        print("task group failed to get 3")
        return 0 
      }

      print("task group returning normally: \(third)")
      return third
    }

    fatalError("Should have thrown and handled inside the catch block")
  }

  // Optionally, we may get the first result back before the failure:
  // CHECK: error caught in group: Boom()
  // CHECK: group cancelled: false
  // CHECK: task 3 (cancelled: false)
  // CHECK: task group returning normally: 3
  // CHECK: got: 3

  print("got: \(got)")
}


@main struct Main {
  static func main() async {
    await test_taskGroup_throws()
  }
{'fraction_non_alphanumeric': 0.12400308721378955, 'fraction_numerical': 0.005402624131721122, 'mean_word_length': 2.3202391118701966, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2651221', 'n_tokens_mistral': 1193, 'n_tokens_neox': 1141, 'n_words': 398}