← Back to Pull requests

Allow noncopyable elements in AsyncStreaming's collect, pipe, forEachBuffer and writer adapters #477

Open
unknown opened this pull request with 622 lines diff across 7 files
View on GitHub
unknown unknown commented on Nov 19, 2026 23:19 UTC
Author

This fixes #471. collect, pipe, forEachBuffer, the writers' finish() conveniences and both writer adapters didn't compile with a noncopyable element type or a noncopyable FinalElement, even though the protocols declare both ~Copyable. This makes them all accept noncopyable types, and changes the existing AsyncStreaming tests to use a noncopyable element so that case is always covered.

<details><summary>Changes, tests and what to scrutinise (Claude Opus 5.5)</summary>

Cause. Under SE-0503, a ~Copyable primary associated type defaults to Copyable wherever its protocol appears in a generic signature, unless that same declaration suppresses it. An inverse on the enclosing extension doesn't cancel the default. So in collect<Container: RangeReplaceableContainer<ReadElement> & ~Copyable & ~Escapable>, Container.Element became Copyable, and Container.Element == ReadElement then forced ReadElement: Copyable. FinalElement had the same problem, and the bodies also copied it (== nil, if let, !). #471 has the full analysis.

Commits

1. Suppress Copyable on element types (8 files). Adds where Container.Element: ~Copyable, Writer.WriteElement: ~Copyable and ReadElement: ~Copyable / WriteElement: ~Copyable clauses to collect, pipe and its drain helper, forEachBuffer, the finish() extensions and both adapters, in the style CallerAsyncReader.read and CallerAsyncWriter.write already use.
2. Use noncopyable elements in the existing AsyncStreaming tests (17 files). The shared test readers and writers now carry NoncopyableInt, a noncopyable wrapper around Int, and are generic over FinalElement (inferred as Void). Writers record what they received in a WriteSink, because finish and pipe consume them. Every test in the reader, writer, collect, forEachBuffer, pipe and adapter files uses them (76 tests once commit 3 is in). The pipe and adapter tests used DuplexAsyncChannel only as a convenient reader and writer; the channels need copyable Sendable elements, so those tests now use the helpers, and the channel test files are unchanged. The first version's separate NoncopyableElementTests.swift is folded into the matching suites.
3. Allow a noncopyable FinalElement (13 files). Adds FinalElement: ~Copyable (and Writer.FinalElement: ~Copyable) to the same APIs. The bodies track completion with a done flag instead of comparing with nil, move the final element out with switch consume and take(), and declare the read closures' parameter consuming FinalElement?. Each API gets a test that passes a noncopyable final element through and checks it arrives.
4. Show the noncopyable constraints in the proposal (1 file). The forEachBuffer, collect and pipe signatures in NNNN-async-streaming.md now carry the same ~Copyable clauses as the code.
5. Drop LifetimeTracker and the separate FinalElement suites (12 files, from review). NoncopyableInt is a plain wrapper around Int, the tests no longer count live elements, and the FinalElement tests now sit in the existing suites. Same 115 tests, in 15 suites instead of 21.

These only remove Copyable requirements, so existing callers with copyable types are unaffected. The adapter types' generic signatures change, which changes their mangled names, but the module is behind the UnstableAsyncStreaming trait.

Results (macOS only: Apple Swift 6.4.2 from Xcode 27, macOS 27.2 SDK):

  • Each commit builds and passes on its own. With --traits UnstableAsyncStreaming: 361 XCTest tests, plus 94, 104 and 115 Swift Testing tests after commits 1, 2 and 3. Commit 4 only touches the proposal, and commit 5 keeps the same 115 tests. With default traits: 361 tests.

  • swift format lint --strict is clean on every changed file.

  • I haven't run the tests on Linux or Windows.
  • What to scrutinise

  • Tests moved off DuplexAsyncChannel. The pipe and adapter tests no longer run against a real channel. Where a test depended on how many writes happen, the helpers now make the split explicit (maximumChunkSize, bufferCapacity) and the tests assert the write count.

  • ~Escapable writers. After commit 3, all four pipe methods also compile with Writer: ... & ~Escapable on 6.4, which the TODO in the pipe files says isn't possible. As agreed in review, this PR leaves the constraint and the TODO as they are.

  • NNNN-async-streaming.md now matches the new signatures, apart from two differences that were already on main: pipe is shown as async throws rather than with typed throws, and the non-throwing forEachBuffer overload isn't listed.

  • CI. The Windows 6.4 and Linux/Windows nightly-6.4.x jobs fail on main with a SIL ownership verifier crash while building AsyncStreamingTests (swiftlang/swift#89639), so they'll stay red here too.
  • </details>

    Resolves #471.

    🤖 Generated with Claude Code

    Diff: 622 lines (+466 / -156) across 7 files Reviewer: FranzBusch Review duration: 16009m
    All Vouch review confidence checks passed
    Residual risk 0.32 = Change Risk (0.55) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.55 · 622 lines diff across 7 files
    0.55
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.385
    Shallow Review Depth shallow_review_depth
    0.344
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #477"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 55.3% (MEDIUM) · Higher = larger blast radius
    55.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 43.0% · Lower = rushed or shallow review
    43.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 31.5% · Safe to merge
    31.5%