Repository: apple/swift
File: stdlib/public/Concurrency/Task.swift (Swift)

Code before:
      do {
        return try await _taskFutureGetThrowing(_task)
      } catch {
        throw (error as! Failure) // as!-safe, because typed throw on the operation closure
      }
    }
  }

  /// The result or error from a throwing task, after it completes.
  ///
  /// If the task hasn't completed,
  /// accessing this property waits for it to complete
  /// and its priority increases to that of the current task.
  /// Note that this might not be as effective as
  /// creating the task with the correct priority,
  /// depending on the executor's scheduling details.
  ///
  /// - Returns: If the task succeeded,
  ///   `.success` with the task's result as the associated value;
  ///   otherwise, `.failure` with the error as the associated value.
  public var result: Result<Success, Failure> {
    get async {
      do {
        return .success(try await value)
      } catch {
        return .failure(error) // as!-safe, guaranteed to be Failure
      }
    }
  }

  /// Cancels this task.
  ///
  /// Cancelling a task has three primary effects:
  ///
  /// - It flags the task as canceled.
  /// - It causes any active cancellation handlers on the task to run, once.
  /// - It cancels any child tasks and task groups of the task, including
  ///   those created in the future. If those tasks have cancellation handlers,
  ///   they also are triggered.
  ///
  /// Task cancellation is cooperative and idempotent.
  ///
  /// Cancelling a task does not automatically cause arbitrary functions on the task
  /// to stop running or throw errors. A function _may_ choose to react
  /// to cancellation by ending its work early, and it is conventional to
  /// signal that to callers by throwing `CancellationError`. However,
  /// a function that doesn't specifically check for cancellation will
  /// run to completion normally, even if the task it is running on is
  /// canceled. However, that function might still end early if it calls
  /// other code that handles cancellation by throwing and that function doesn't
  /// handle the error.

Review comment (suggestion):
Comment now refers to a force cast that has moved elsewhere.

Code after:
      do {
        return try await _taskFutureGetThrowing(_task)
      } catch {
        throw (error as! Failure) // as!-safe, because typed throw on the operation closure
      }
    }
  }

  /// The result or error from a throwing task, after it completes.
  ///
  /// If the task hasn't completed,
  /// accessing this property waits for it to complete
  /// and its priority increases to that of the current task.
  /// Note that this might not be as effective as
  /// creating the task with the correct priority,
  /// depending on the executor's scheduling details.
  ///
  /// - Returns: If the task succeeded,
  ///   `.success` with the task's result as the associated value;
  ///   otherwise, `.failure` with the error as the associated value.
  public var result: Result<Success, Failure> {
    get async {
      do {
        return .success(try await value)
      } catch {
        return .failure(error)
      }
    }
  }

  /// Cancels this task.
  ///
  /// Cancelling a task has three primary effects:
  ///
  /// - It flags the task as canceled.
  /// - It causes any active cancellation handlers on the task to run, once.
  /// - It cancels any child tasks and task groups of the task, including
  ///   those created in the future. If those tasks have cancellation handlers,
  ///   they also are triggered.
  ///
  /// Task cancellation is cooperative and idempotent.
  ///
  /// Cancelling a task does not automatically cause arbitrary functions on the task
  /// to stop running or throw errors. A function _may_ choose to react
  /// to cancellation by ending its work early, and it is conventional to
  /// signal that to callers by throwing `CancellationError`. However,
  /// a function that doesn't specifically check for cancellation will
  /// run to completion normally, even if the task it is running on is
  /// canceled. However, that function might still end early if it calls
  /// other code that handles cancellation by throwing and that function doesn't
  /// handle the error.