Skip to content

[6.0] Fix function subtyping rules for sending #74428

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

Merged
merged 9 commits into from
Jun 17, 2024

Conversation

gottesmm
Copy link
Contributor

@gottesmm gottesmm commented Jun 14, 2024

Explanation: This PR fixes function sub typing rules for sending. Specifically:

  • We emit a diagnostic if one attempts to convert a function with a sending parameter to one without a sending parameter.
  • We emit a diagnostic if one attempts to convert a function without a sending result to one with a sending result.
  • We prevent the same sort of mismatches in protocols.

Radars:

  • rdar://129300953
  • rdar://127675288

Original PRs:

Risk: Low. This is improving the compiler since these concurrency holes would result in other weird crashes.
Testing: Added exhaustive tests to the test suite.
Reviewer: N/A

gottesmm added 8 commits June 14, 2024 09:33
…witnessed by a function without a sending result.

This is important since the witness is not promising to return a disconnected
value and the caller of the requirement is going to accept that.

rdar://127675288
(cherry picked from commit 1c9b178)
rdar://127675288
(cherry picked from commit 3c166b5)
… computeFailureTypeWitness.

The problem happens when a type conforms to AsyncIterator.next when next in the
protocol returns its value as sending, but the witness does not have sending.
This results in the function subtyping rules rejecting the witness... but we
still leave in the witness as a nullptr... so we need to handle it here.

rdar://129300953
(cherry picked from commit 83bcf23)
…testing typecheck errors.

(cherry picked from commit ba6c8af)
…rs and results.

I found this while writing tests for the earlier part of this work. Since this
is also type checking work, I am just folding this work into that work.

(cherry picked from commit 2ac874e)
The previous commit fixed things like:

```swift
let x: () -> sending String = { "" }
```

This commit fixes this test case:

```swift
let x = { () -> sending String in "" }
```

(cherry picked from commit 81100ad)
(cherry picked from commit a451fe6)
…when not in swift 6.

This will ensure that we do not break anyone who has adopted APIs like
CheckedContinuation.resume that now have sending parameters.

An example of where this can come up is shown by the ProcessType in SwiftToolsCore:

```swift
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
@discardableResult
public func waitUntilExit() async throws -> ProcessResult {
    try await withCheckedThrowingContinuation { continuation in
        DispatchQueue.processConcurrent.async {
            self.waitUntilExit(continuation.resume(with:))
        }
    }
}
```

This fails to compile since self.waitUntilExit doesn't expect a function that
takes a sending parameter. We want to give people time to fix such issues.

(cherry picked from commit 16d0194)
@gottesmm gottesmm requested a review from a team as a code owner June 14, 2024 16:34
@gottesmm
Copy link
Contributor Author

Original: #74129

@gottesmm
Copy link
Contributor Author

@swift-ci test

For now, just turn off the error in swift 5 mode. I wanted to make this an error
but right now I do not have all of the time to do it so this is good in the
short term so that people can adopt sending on protocols without breaking people
using their framework in executables still compiling in swift 5.

(cherry picked from commit c8d9e18)
@gottesmm
Copy link
Contributor Author

@swift-ci test

@gottesmm gottesmm merged commit b28308d into swiftlang:release/6.0 Jun 17, 2024
5 checks passed
@gottesmm gottesmm deleted the release/6.0-rdar127675288 branch June 17, 2024 20:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants