<commit_before>// RUN: %target-typecheck-verify-swift -enable-experimental-concurrency
// REQUIRES: concurrency

enum PictureData {
  case value(String)
  case failedToLoadImagePlaceholder
}

func test_cancellation_checkCancellation() async throws {
  await try Task.checkCancellation()
}

func test_cancellation_guard_isCancelled(_ any: Any) async -> PictureData {
  guard await !Task.isCancelled() else {
    return PictureData.failedToLoadImagePlaceholder
  }

  return PictureData.value("...")
}

struct SomeFile {
  func close() {}
}

func test_cancellation_withCancellationHandler(_ anything: Any) async -> PictureData {
  let handle = Task.runDetached { () -> PictureData in
    let file = SomeFile()

    return await try Task.withCancellationHandler(
      handler: { file.close() },
      operation: {
      await test_cancellation_guard_isCancelled(file)
    })
  }

  handle.cancel()
}

func test_cancellation_loop() async -> Int {
  struct SampleTask { func process() async {} }

  let tasks = [SampleTask(), SampleTask()]
  var processed = 0
  for t in tasks where await !Task.isCancelled() {
    await t.process()
    processed += 1
  }
  return processed
}

// ==== Deadlines --------------------------------------------------------------

func int() async -> Int { 42 }

func test_cancellation_withDeadline_in() async throws -> Int {
  /* missing await */ Task.withDeadline(in: .seconds(5), operation: { // FIXME: rdar://70751405 async rethrows functions are not detected as async
    await int()
  })
}

func test_cancellation_withDeadline(specificDeadline: Task.Deadline) async -> Int {
  /* missing `await` */
  Task.withDeadline(specificDeadline) { // FIXME: rdar://70751405 async rethrows functions are not detected as async
    await int()
  }
}<commit_msg>update test now that rdar://70751405 is fixed<commit_after>// RUN: %target-typecheck-verify-swift -enable-experimental-concurrency
// REQUIRES: concurrency

enum PictureData {
  case value(String)
  case failedToLoadImagePlaceholder
}

func test_cancellation_checkCancellation() async throws {
  await try Task.checkCancellation()
}

func test_cancellation_guard_isCancelled(_ any: Any) async -> PictureData {
  guard await !Task.isCancelled() else {
    return PictureData.failedToLoadImagePlaceholder
  }

  return PictureData.value("...")
}

struct SomeFile {
  func close() {}
}

func test_cancellation_withCancellationHandler(_ anything: Any) async -> PictureData {
  let handle = Task.runDetached { () -> PictureData in
    let file = SomeFile()

    return await try Task.withCancellationHandler(
      handler: { file.close() },
      operation: {
      await test_cancellation_guard_isCancelled(file)
    })
  }

  handle.cancel()
}

func test_cancellation_loop() async -> Int {
  struct SampleTask { func process() async {} }

  let tasks = [SampleTask(), SampleTask()]
  var processed = 0
  for t in tasks where await !Task.isCancelled() {
    await t.process()
    processed += 1
  }
  return processed
}

// ==== Deadlines --------------------------------------------------------------

func int() async -> Int { 42 }

func test_cancellation_withDeadline_in() async throws -> Int {
  await Task.withDeadline(in: .seconds(5), operation: {
    await int()
  })
}

func test_cancellation_withDeadline(specificDeadline: Task.Deadline) async -> Int {
  await Task.withDeadline(specificDeadline) {
    await int()
  }
}
{'fraction_non_alphanumeric': 0.14298297122724604, 'fraction_numerical': 0.009982384028185555, 'mean_word_length': 3.307206068268015, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '4727791', 'n_tokens_mistral': 1053, 'n_tokens_neox': 1034, 'n_words': 260}