Help addressing a crash related to Task creation API

Hello All!

I’m attempting to make forward progress on this PR: https://github.com/swiftlang/swift/pull/86358. However, the changes have exposed a compiler crash. This is well outside of my comfort zone, so I’m hoping to recruit some help.

It’s kind of an annoying one to reproduce. I’ve been unable to transform this problem into an issue that is independent of those Task changes, even though it feels like it should be a more general problem.

I’ve captured the issue in a compiler branch, the details of which are in the issue I just filed. Task typed throws API change triggers crash in CapturePromotion.cpp · Issue #86391 · swiftlang/swift · GitHub

Just to save a click, here’s the reduced code:

private protocol AnyTask: Sendable {
  func waitForCompletion() async
}

extension Task: AnyTask {
  func waitForCompletion() async {
  }
}

struct PendingTask: Sendable {
  fileprivate let task: any AnyTask
}

public final class AsyncQueue: Sendable {
  public init() {}

  public func asyncThrowing<Success: Sendable>(
    @_inheritActorContext operation: @escaping @Sendable () async throws -> Success
  ) -> Void {
      var dependencies: [PendingTask] = []

      Task {
        for dependency in dependencies {
          await dependency.task.waitForCompletion()
        }

        let result = try await operation()

        return result
    }
  }
}
Assertion failed: (calleeConv.getSILType(cpInfo, builder.getTypeExpansionContext()) == box->getType() && "SILType of parameter info does not match type of parameter"), function processPartialApplyInst, file CapturePromotion.cpp, line 1553.

I’ve tried a number of other modifications, including removing the fileprivate there, and all make the issue go away. The Task API changes are visible in the PR, but are mostly around adopting typed throws.

Is anyone up for helping me out here?

1 Like

@Slava_Pestov is way ahead of you by the looks :)

2 Likes

It looks like the CapturePromotion pass was never updated for typed throws, and it would assert if a closure with an indirect error result happened to have promotable captures. I pushed a fix: SILOptimizer: Fix CapturePromotion crash with indirect error result by slavapestov · Pull Request #86400 · swiftlang/swift · GitHub

3 Likes