Skip to content

Bug: channel send does not consume; non-integer element types mistyped (§14.15) #419

Closed

Description

@ehartford

Summary

Fix channel typing and ownership so Sender[T].send moves a T, Receiver[T].recv returns that same T, and queued non-Copy values are dropped correctly when a channel is destroyed. The runtime and codegen are already mostly element-size generic; the remaining blockers are stale sema restrictions, missing consume effects, and missing drop/close ownership semantics.

Spec

§14.15 says channels transfer ownership: sending moves the value. §14.16 requires values crossing concurrency boundaries to satisfy Send. §16.3d provides the explicit consume effect model for intrinsic-backed declarations where there is no body to infer from.

let (tx, rx) = chan[Vec[i32]](1)
let v = Vec[i32].new()
tx.send(move v)
v.len()          // error: use after move
let got = rx.recv()   // Vec[i32]

Current state

The low-level channel runtime is already element-size generic. The channel layout stores elem_size (rt/channel_runtime.w:18, rt/channel_runtime.w:29), grow/send/recv copy elem_size bytes (rt/channel_runtime.w:65, rt/channel_runtime.w:137, rt/channel_runtime.w:159), and create stores the provided element size (rt/channel_runtime.w:95, rt/channel_runtime.w:109).

LLVM codegen also passes typed sizes/slots: CHAN_SEND allocates a stack slot of the sent value type (src/CodegenDispatch.w:7514, src/CodegenDispatch.w:7522), CHAN_CREATE computes abi_size_of(elem) from Channel[T] (src/CodegenDispatch.w:7540, src/CodegenDispatch.w:7556), and CHAN_RECV allocates/loads the destination type (src/CodegenDispatch.w:7578, src/CodegenDispatch.w:7584).

The main type blocker is sema. Receiver return types are generic-aware in builtin_intrinsic_method_return_type (src/SemaCheck.w:9861), and send expected-arg type uses Sender[T] (src/SemaCheck.w:9967), but the low-level send intrinsic path still rejects non-integer payloads (src/SemaCheck.w:11240, src/SemaCheck.w:11242). Sender.send is not wired into the call-site consume-effect enforcement used for declared effects (src/SemaCheck.w:8177). The runtime destroy path only frees the buffer and channel object (rt/channel_runtime.w:186), so queued non-Copy elements with Drop are leaked logically.

Implementation plan

  1. Replace the stale payload_kind != TY_INT guard in src/SemaCheck.w:11240 with a check that the payload type is compatible with the channel element type. Keep the existing Send/ephemeral rejection, but base it on real T: Send once Send/Sync enforcement substance (§14.16) #473 is available.
  2. Treat Sender[T].send as consuming argument 0. Either attach a compiler-known EFF_CONSUME to the intrinsic method or route it through the same effect table used by @[effect] so SemaCheck.w:8177 marks the argument moved and requires move/copy for non-Copy values.
  3. Ensure method-call sema and the low-level intrinsic path agree: Sender[T].send(value) should typecheck only for value: T, and Receiver[T].recv() should return T without falling back to i32.
  4. Add drop glue for queued non-Copy elements. Extend with_channel_create to accept an optional drop-function pointer plus element alignment if needed, store it in the channel layout, and have with_channel_destroy iterate live elements and call the drop function before freeing the buffer.
  5. Add Sender/Receiver close-on-drop semantics: dropping the final sender or receiver should close the channel or decrement endpoint counts, so blocked peers wake and observe closure. This may require endpoint reference counts in the runtime layout.
  6. Add channel send/recv to Implement compiler-visible current-fiber suspension model #354's current-fiber suspension set because bounded send and empty recv can yield (rt/channel_runtime.w:121, rt/channel_runtime.w:148).
  7. Runtime layout changes in rt/channel_runtime.w are bootstrap-sensitive. Do not combine Link/runtime-object changes with unrelated compiler changes; follow AGENTS.md runtime migration rules if object layout or exported symbols change.

Test plan

Add test/spec/spec_ss14_15_channel_generic_payloads.w with chan[Vec[i32]], chan[str], and a small struct payload round trip.

Add test/compile_errors/err_channel_send_consumes_value.w proving a non-Copy value cannot be used after tx.send(move v) and that tx.send(v) reports the consume-effect diagnostic.

Add test/compile_errors/err_channel_send_wrong_type.w for Sender[i32].send("x").

Add test/compile_errors/err_channel_send_requires_send.w or extend test/spec/spec_ss14_15_channel_send_requires_send.w once #473 provides a concrete non-Send witness.

Add a behavior test with a Drop-counted payload queued in a channel that is destroyed without recv, proving queued drops run exactly once.

Acceptance criteria

  • chan[T] send/recv preserve T for non-integer payloads.
  • Sending a non-Copy value consumes it and later use is rejected.
  • Send rejects non-Send payloads once non-Send witnesses exist.
  • Destroying or closing channels drops queued non-Copy elements exactly once.
  • Bounded send/empty recv are represented as may_suspend primitives.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

Depends on #473 for substantive Send checks and overlaps #354 for suspension classification. Drop glue for generic non-Copy payloads may depend on #391 if type-specific drop function identity is blocked by generic erasure.

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 14.15.1.1.

Activity

  1. ehartford commented on Jun 17, 2026

    @ehartford
    ContributorAuthor

    Implemented in 013c4ff. Channel endpoints are now non-Copy ownership values, Sender[T].send consumes the payload, generic channel endpoint layout/codegen handles non-integer payloads, queued payloads run drop callbacks when the channel is released, and sender/receiver endpoint drops release runtime references. Added positive tests for str, struct, Vec[i32], and queued payload drops; added negative tests for use-after-send and wrong payload type. Verification passed with project seed/release commands: build, build :fixpoint, build :test, and build :test-green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions