Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive warning during the code analysis performed by SE-0430 "transferring" parameters and result values #73315

Open
groue opened this issue Apr 28, 2024 · 1 comment
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. concurrency Feature: umbrella label for concurrency language features

Comments

@groue
Copy link

groue commented Apr 28, 2024

Description

The compiler emits false positive warnings whenever a value is transferred twice.

Reproduction

func schedule(_ action: transferring @escaping () -> Void) { }

// OK
func f1(_ closure: transferring @escaping () -> Void) {
    schedule {
        closure()
    }
}

// Compiler warning
func f2(_ closure: transferring @escaping () -> Void) {
    schedule {
        schedule {
//      `- warning: task-isolated value of type '() -> Void'
//         passed as a strongly transferred parameter
            closure()
        }
    }
}

Expected behavior

The compiler emits no diagnostic

Environment

$ /Library/Developer/Toolchains/swift-6.0-DEVELOPMENT-SNAPSHOT-2024-04-22-a.xctoolchain/usr/bin/swift --version
Apple Swift version 6.0-dev (LLVM 94447cf359772fc, Swift f36098a14a0d48b)
Target: arm64-apple-macosx14.0

Additional information

I discovered this issue while exploring the possibilities evoked in this forum post.

I assumed that DispatchQueue.async WILL be modified so that it accepts a transferred closure instead of a Sendable closure.

I have some code that performs double async dispatch in order to avoid thread explosion (technique discussed in this forum post).

And that's how I had to transfer a closure twice, saw a warning, and opened this issue.

@groue groue added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Apr 28, 2024
@groue
Copy link
Author

groue commented Apr 28, 2024

I have similar warnings in (real) code that involves continuation, as below:

// Definition of asyncRead used below
func asyncRead(_ value: transferring @escaping (Result<Database, Error>) -> Void)

public func read<T>(
    _ value: transferring @escaping (Database) throws -> transferring T
) async throws -> transferring T
{
  try await withUnsafeThrowingContinuation { continuation in
    asyncRead { result in
//  `- warning: task-isolated value of type
//     '(Result<Database, any Error>) -> Void' passed
//     as a strongly transferred parameter.
      do {
        try continuation.resume(returning: value(result.get()))
      } catch {
        continuation.resume(throwing: error)
      }
    }
  }
}

I'm playing with swift-6.0-DEVELOPMENT-SNAPSHOT-2024-04-22-a from Xcode, which means that it's difficult to know what's happening. To get accurate compiler diagnostics, I compile in the terminal with /Library/Developer/Toolchains/swift-6.0-DEVELOPMENT-SNAPSHOT-2024-04-22-a.xctoolchain/usr/bin/swift build. But I don't know what is the actual definition of withUnsafeThrowingContinuation.

My goal here is to help users use non-sendable values as freely as possible, both as captured inputs, and output of read. Since read guarantees that its value closure is executed only once, and that it does not use the produced values in any way, I believe that using transferring is correct.

I don't really know if I'm facing compiler issues, or if I misunderstand the transferring feature, or if I need yet another compiler feature that is not there yet.

@hborla hborla added concurrency Feature: umbrella label for concurrency language features and removed triage needed This issue needs more specific labels labels May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. concurrency Feature: umbrella label for concurrency language features
Projects
None yet
Development

No branches or pull requests

3 participants