Skip to content

Added Hashable conformance to Async(Throwing)Stream.Continuation #79457

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions stdlib/public/Concurrency/AsyncStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,22 @@ extension AsyncStream: @unchecked Sendable where Element: Sendable { }
@available(SwiftStdlib 5.1, *)
extension AsyncStream.Continuation.YieldResult: Sendable where Element: Sendable { }

@available(SwiftStdlib 6.2, *)
extension AsyncStream.Continuation: Hashable {
@available(SwiftStdlib 6.2, *)
public func hash(into hasher: inout Hasher) {
return hasher.combine(ObjectIdentifier(storage))
}
@available(SwiftStdlib 6.2, *)
public var hashValue: Int {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why that, isn't the hash function enough?

Copy link
Contributor Author

@nickolas-pohilets nickolas-pohilets Apr 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently no. If omitted it gets synthesised as expected, but then test complains that it does not have availability annotations. I'm not sure if this is expected or a bug in code gen of the Hashable conformance.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this expected @lorentey ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely is a bug! Conformances with availability are a relatively recent development, and evidently we have not yet tried to combine them with synthesized requirements. The expected behavior is that the synthesized requirement would match the availability of the conformance itself.

It feels like this might be a good candidate for a starter task in compiler development.

(The hashValue as implemented matches the synthesized one; when the compiler issue is resolved, then it can simply be deleted to have it be replaced with the implicit default.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh hang on -- can you tell me what test is failing?

The compiler itself seems fine with it; this compiles with no diagnostic:

@available(macOS 11, *)
public struct Foo {
  public var value: Int
}
@available(macOS 12, *)
extension Foo: Hashable {
  @available(macOS 12, *)
  public func hash(into hasher: inout Hasher) {
    hasher.combine(value)
  }
}

The generated .swiftinterface indeed doesn't set availability directly on the synthesized members (== and hashValue), but they still inherit that from the extension's own context:

import Swift
import _Concurrency
import _StringProcessing
import _SwiftConcurrencyShims
@available(macOS 11, *)
public struct Foo {
  public var value: Swift.Int
}
@available(macOS 12, *)
extension foo.Foo : Swift.Hashable {
  @available(macOS 12, *)
  public func hash(into hasher: inout Swift.Hasher)
  public static func == (a: foo.Foo, b: foo.Foo) -> Swift.Bool
  public var hashValue: Swift.Int {
    get
  }
}

This sounds like it might actually be due to an overzealous test that attempts to analyze the interface. (If my diagnosis is correct, then the choice is between seeing if the test can be adjusted, or changing the compiler to generate the extra @available attributes anyway.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The failing test was test/api-digester/stability-concurrency-abi.test

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, that makes sense.

return _hashValue(for: self)
}
@available(SwiftStdlib 6.2, *)
public static func == (lhs: Self, rhs: Self) -> Bool {
return lhs.storage === rhs.storage
}
}

#else
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
Expand Down
16 changes: 16 additions & 0 deletions stdlib/public/Concurrency/AsyncThrowingStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,22 @@ extension AsyncThrowingStream: @unchecked Sendable where Element: Sendable { }
@available(SwiftStdlib 5.1, *)
extension AsyncThrowingStream.Continuation.YieldResult: Sendable where Element: Sendable { }

@available(SwiftStdlib 6.2, *)
extension AsyncThrowingStream.Continuation: Hashable {
@available(SwiftStdlib 6.2, *)
public func hash(into hasher: inout Hasher) {
return hasher.combine(ObjectIdentifier(storage))
}
@available(SwiftStdlib 6.2, *)
public var hashValue: Int {
return _hashValue(for: self)
}
@available(SwiftStdlib 6.2, *)
public static func == (lhs: Self, rhs: Self) -> Bool {
return lhs.storage === rhs.storage
}
}

#else
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
Expand Down
18 changes: 18 additions & 0 deletions test/Concurrency/Runtime/async_stream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,24 @@ class NotSendable {}
expectTrue(expectation.fulfilled)
}

tests.test("continuation equality") {
let (_, continuation1) = AsyncStream<Int>.makeStream()
let (_, continuation2) = AsyncStream<Int>.makeStream()
expectTrue(continuation1 == continuation1)
expectTrue(continuation1 != continuation2)
expectTrue(continuation1.hashValue == continuation1.hashValue)
expectTrue(continuation1.hashValue != continuation2.hashValue)
}

tests.test("throwing continuation equality") {
let (_, continuation1) = AsyncThrowingStream<Int, Error>.makeStream()
let (_, continuation2) = AsyncThrowingStream<Int, Error>.makeStream()
expectTrue(continuation1 == continuation1)
expectTrue(continuation1 != continuation2)
expectTrue(continuation1.hashValue == continuation1.hashValue)
expectTrue(continuation1.hashValue != continuation2.hashValue)
}

// MARK: - Multiple consumers

tests.test("finish behavior with multiple consumers") {
Expand Down
12 changes: 12 additions & 0 deletions test/abi/Inputs/macOS/arm64/concurrency/baseline
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,18 @@ _$sScS12ContinuationV15BufferingPolicyO15bufferingOldestyADyx__GSicAFmlFWC
_$sScS12ContinuationV15BufferingPolicyO9unboundedyADyx__GAFmlFWC
_$sScS12ContinuationV15BufferingPolicyOMa
_$sScS12ContinuationV15BufferingPolicyOMn
_$sScS12ContinuationV2eeoiySbAByx_G_ADtFZ
_$sScS12ContinuationV4hash4intoys6HasherVz_tF
_$sScS12ContinuationV5yield4withAB11YieldResultOyx__Gs0E0Oyxs5NeverOG_tF
_$sScS12ContinuationV5yieldAB11YieldResultOyyt__GyytRszlF
_$sScS12ContinuationV5yieldyAB11YieldResultOyx__GxnF
_$sScS12ContinuationV6finishyyF
_$sScS12ContinuationV9hashValueSivg
_$sScS12ContinuationV9hashValueSivpMV
_$sScS12ContinuationVMa
_$sScS12ContinuationVMn
_$sScS12ContinuationVyx_GSHsMc
_$sScS12ContinuationVyx_GSQsMc
_$sScS17makeAsyncIteratorScS0C0Vyx_GyF
_$sScS8IteratorV4nextxSgyYaF
_$sScS8IteratorV4nextxSgyYaFTu
Expand Down Expand Up @@ -359,12 +365,18 @@ _$sScs12ContinuationV15BufferingPolicyO15bufferingOldestyADyxq___GSicAFms5ErrorR
_$sScs12ContinuationV15BufferingPolicyO9unboundedyADyxq___GAFms5ErrorR_r0_lFWC
_$sScs12ContinuationV15BufferingPolicyOMa
_$sScs12ContinuationV15BufferingPolicyOMn
_$sScs12ContinuationV2eeoiySbAByxq__G_ADtFZ
_$sScs12ContinuationV4hash4intoys6HasherVz_tF
_$sScs12ContinuationV5yield4withAB11YieldResultOyxs5Error_p__Gs0E0OyxsAG_pG_tsAG_pRs_rlF
_$sScs12ContinuationV5yieldAB11YieldResultOyytq___GyytRszrlF
_$sScs12ContinuationV5yieldyAB11YieldResultOyxq___GxnF
_$sScs12ContinuationV6finish8throwingyq_Sgn_tF
_$sScs12ContinuationV9hashValueSivg
_$sScs12ContinuationV9hashValueSivpMV
_$sScs12ContinuationVMa
_$sScs12ContinuationVMn
_$sScs12ContinuationVyxq__GSHsMc
_$sScs12ContinuationVyxq__GSQsMc
_$sScs17makeAsyncIteratorScs0C0Vyxq__GyF
_$sScs8IteratorV4nextxSgyYaKF
_$sScs8IteratorV4nextxSgyYaKFTu
Expand Down
12 changes: 12 additions & 0 deletions test/abi/Inputs/macOS/arm64/concurrency/baseline-asserts
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,18 @@ _$sScS12ContinuationV15BufferingPolicyO15bufferingOldestyADyx__GSicAFmlFWC
_$sScS12ContinuationV15BufferingPolicyO9unboundedyADyx__GAFmlFWC
_$sScS12ContinuationV15BufferingPolicyOMa
_$sScS12ContinuationV15BufferingPolicyOMn
_$sScS12ContinuationV2eeoiySbAByx_G_ADtFZ
_$sScS12ContinuationV4hash4intoys6HasherVz_tF
_$sScS12ContinuationV5yield4withAB11YieldResultOyx__Gs0E0Oyxs5NeverOG_tF
_$sScS12ContinuationV5yieldAB11YieldResultOyyt__GyytRszlF
_$sScS12ContinuationV5yieldyAB11YieldResultOyx__GxnF
_$sScS12ContinuationV6finishyyF
_$sScS12ContinuationV9hashValueSivg
_$sScS12ContinuationV9hashValueSivpMV
_$sScS12ContinuationVMa
_$sScS12ContinuationVMn
_$sScS12ContinuationVyx_GSHsMc
_$sScS12ContinuationVyx_GSQsMc
_$sScS17makeAsyncIteratorScS0C0Vyx_GyF
_$sScS8IteratorV4nextxSgyYaF
_$sScS8IteratorV4nextxSgyYaFTu
Expand Down Expand Up @@ -359,12 +365,18 @@ _$sScs12ContinuationV15BufferingPolicyO15bufferingOldestyADyxq___GSicAFms5ErrorR
_$sScs12ContinuationV15BufferingPolicyO9unboundedyADyxq___GAFms5ErrorR_r0_lFWC
_$sScs12ContinuationV15BufferingPolicyOMa
_$sScs12ContinuationV15BufferingPolicyOMn
_$sScs12ContinuationV2eeoiySbAByxq__G_ADtFZ
_$sScs12ContinuationV4hash4intoys6HasherVz_tF
_$sScs12ContinuationV5yield4withAB11YieldResultOyxs5Error_p__Gs0E0OyxsAG_pG_tsAG_pRs_rlF
_$sScs12ContinuationV5yieldAB11YieldResultOyytq___GyytRszrlF
_$sScs12ContinuationV5yieldyAB11YieldResultOyxq___GxnF
_$sScs12ContinuationV6finish8throwingyq_Sgn_tF
_$sScs12ContinuationV9hashValueSivg
_$sScs12ContinuationV9hashValueSivpMV
_$sScs12ContinuationVMa
_$sScs12ContinuationVMn
_$sScs12ContinuationVyxq__GSHsMc
_$sScs12ContinuationVyxq__GSQsMc
_$sScs17makeAsyncIteratorScs0C0Vyxq__GyF
_$sScs8IteratorV4nextxSgyYaKF
_$sScs8IteratorV4nextxSgyYaKFTu
Expand Down
12 changes: 12 additions & 0 deletions test/abi/Inputs/macOS/x86_64/concurrency/baseline
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,18 @@ _$sScS12ContinuationV15BufferingPolicyO15bufferingOldestyADyx__GSicAFmlFWC
_$sScS12ContinuationV15BufferingPolicyO9unboundedyADyx__GAFmlFWC
_$sScS12ContinuationV15BufferingPolicyOMa
_$sScS12ContinuationV15BufferingPolicyOMn
_$sScS12ContinuationV2eeoiySbAByx_G_ADtFZ
_$sScS12ContinuationV4hash4intoys6HasherVz_tF
_$sScS12ContinuationV5yield4withAB11YieldResultOyx__Gs0E0Oyxs5NeverOG_tF
_$sScS12ContinuationV5yieldAB11YieldResultOyyt__GyytRszlF
_$sScS12ContinuationV5yieldyAB11YieldResultOyx__GxnF
_$sScS12ContinuationV6finishyyF
_$sScS12ContinuationV9hashValueSivg
_$sScS12ContinuationV9hashValueSivpMV
_$sScS12ContinuationVMa
_$sScS12ContinuationVMn
_$sScS12ContinuationVyx_GSHsMc
_$sScS12ContinuationVyx_GSQsMc
_$sScS17makeAsyncIteratorScS0C0Vyx_GyF
_$sScS8IteratorV4nextxSgyYaF
_$sScS8IteratorV4nextxSgyYaFTu
Expand Down Expand Up @@ -359,12 +365,18 @@ _$sScs12ContinuationV15BufferingPolicyO15bufferingOldestyADyxq___GSicAFms5ErrorR
_$sScs12ContinuationV15BufferingPolicyO9unboundedyADyxq___GAFms5ErrorR_r0_lFWC
_$sScs12ContinuationV15BufferingPolicyOMa
_$sScs12ContinuationV15BufferingPolicyOMn
_$sScs12ContinuationV2eeoiySbAByxq__G_ADtFZ
_$sScs12ContinuationV4hash4intoys6HasherVz_tF
_$sScs12ContinuationV5yield4withAB11YieldResultOyxs5Error_p__Gs0E0OyxsAG_pG_tsAG_pRs_rlF
_$sScs12ContinuationV5yieldAB11YieldResultOyytq___GyytRszrlF
_$sScs12ContinuationV5yieldyAB11YieldResultOyxq___GxnF
_$sScs12ContinuationV6finish8throwingyq_Sgn_tF
_$sScs12ContinuationV9hashValueSivg
_$sScs12ContinuationV9hashValueSivpMV
_$sScs12ContinuationVMa
_$sScs12ContinuationVMn
_$sScs12ContinuationVyxq__GSHsMc
_$sScs12ContinuationVyxq__GSQsMc
_$sScs17makeAsyncIteratorScs0C0Vyxq__GyF
_$sScs8IteratorV4nextxSgyYaKF
_$sScs8IteratorV4nextxSgyYaKFTu
Expand Down
12 changes: 12 additions & 0 deletions test/abi/Inputs/macOS/x86_64/concurrency/baseline-asserts
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,18 @@ _$sScS12ContinuationV15BufferingPolicyO15bufferingOldestyADyx__GSicAFmlFWC
_$sScS12ContinuationV15BufferingPolicyO9unboundedyADyx__GAFmlFWC
_$sScS12ContinuationV15BufferingPolicyOMa
_$sScS12ContinuationV15BufferingPolicyOMn
_$sScS12ContinuationV2eeoiySbAByx_G_ADtFZ
_$sScS12ContinuationV4hash4intoys6HasherVz_tF
_$sScS12ContinuationV5yield4withAB11YieldResultOyx__Gs0E0Oyxs5NeverOG_tF
_$sScS12ContinuationV5yieldAB11YieldResultOyyt__GyytRszlF
_$sScS12ContinuationV5yieldyAB11YieldResultOyx__GxnF
_$sScS12ContinuationV6finishyyF
_$sScS12ContinuationV9hashValueSivg
_$sScS12ContinuationV9hashValueSivpMV
_$sScS12ContinuationVMa
_$sScS12ContinuationVMn
_$sScS12ContinuationVyx_GSHsMc
_$sScS12ContinuationVyx_GSQsMc
_$sScS17makeAsyncIteratorScS0C0Vyx_GyF
_$sScS8IteratorV4nextxSgyYaF
_$sScS8IteratorV4nextxSgyYaFTu
Expand Down Expand Up @@ -359,12 +365,18 @@ _$sScs12ContinuationV15BufferingPolicyO15bufferingOldestyADyxq___GSicAFms5ErrorR
_$sScs12ContinuationV15BufferingPolicyO9unboundedyADyxq___GAFms5ErrorR_r0_lFWC
_$sScs12ContinuationV15BufferingPolicyOMa
_$sScs12ContinuationV15BufferingPolicyOMn
_$sScs12ContinuationV2eeoiySbAByxq__G_ADtFZ
_$sScs12ContinuationV4hash4intoys6HasherVz_tF
_$sScs12ContinuationV5yield4withAB11YieldResultOyxs5Error_p__Gs0E0OyxsAG_pG_tsAG_pRs_rlF
_$sScs12ContinuationV5yieldAB11YieldResultOyytq___GyytRszrlF
_$sScs12ContinuationV5yieldyAB11YieldResultOyxq___GxnF
_$sScs12ContinuationV6finish8throwingyq_Sgn_tF
_$sScs12ContinuationV9hashValueSivg
_$sScs12ContinuationV9hashValueSivpMV
_$sScs12ContinuationVMa
_$sScs12ContinuationVMn
_$sScs12ContinuationVyxq__GSHsMc
_$sScs12ContinuationVyxq__GSQsMc
_$sScs17makeAsyncIteratorScs0C0Vyxq__GyF
_$sScs8IteratorV4nextxSgyYaKF
_$sScs8IteratorV4nextxSgyYaKFTu
Expand Down