From 929132c81d4c25e554e5e3f99a01b103f2585b34 Mon Sep 17 00:00:00 2001 From: "A.Shakhmatov" Date: Sun, 4 Oct 2026 16:57:59 +0300 Subject: [PATCH] Phase 3: owner decisions on RFC 0012 implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - spec 04 §40: mutex always linear, never copyable; mutex.take is the only way to dismantle it and needs no live borrow (hence no guard and no waiter); no unlock or destructor - P3-10 (async buffer claims, E4303) moved to P7-10 with task-aware I/O; E4303 stays reserved; Phase 3 scope closed - RFC 0012 erratum: consumption after send is E2002, use is E2001 - tests: take while borrowed (E2101), use after take (E3001) --- docs/plan/README.md | 2 +- docs/plan/conformance-suite.md | 2 +- docs/plan/phase-3-structured-concurrency.md | 7 ++++--- docs/plan/phase-7-native-backend.md | 1 + .../0012-channels-mutex-select-cancellation.md | 6 ++++-- docs/spec/04-concurrency-and-atomics.md | 4 +++- .../phase3/reject/sync_take_while_borrowed.air | 18 ++++++++++++++++++ .../phase3/reject/sync_use_after_take.air | 18 ++++++++++++++++++ 8 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 tests/conformance/phase3/reject/sync_take_while_borrowed.air create mode 100644 tests/conformance/phase3/reject/sync_use_after_take.air diff --git a/docs/plan/README.md b/docs/plan/README.md index 5a04467..572a897 100644 --- a/docs/plan/README.md +++ b/docs/plan/README.md @@ -64,7 +64,7 @@ Every registry code (`../reference/error-codes.md`) has a phase that must implem | E2102 borrow.outlives_owner | P1 | | E4201 atomic.invalid_order | P5 | | E2103 memory.out_of_bounds | P1 | | E4301 task.live_child_escape | P3 | | E2104 memory.region_generation | P2 | | E4302 task.borrow_escape | P3 | -| E2110 island.escape | P4 | | E4303 io.buffer_in_flight | P3 | +| E2110 island.escape | P4 | | E4303 io.buffer_in_flight | P7 (reserved) | | E2111 island.foreign_mutable_edge | P4 | | E5001 air.invalid_ssa | P0 | | E3001 resource.use_after_consume | P2 | | E5002 air.invalid_cfg | P0 | | E3002 resource.leak | P2 | | E5101 verifier.resource_limit | P0 | diff --git a/docs/plan/conformance-suite.md b/docs/plan/conformance-suite.md index d0d2ced..2395f06 100644 --- a/docs/plan/conformance-suite.md +++ b/docs/plan/conformance-suite.md @@ -67,7 +67,7 @@ Must include: - non-Send transfer; - non-Share concurrent read; - task escape; -- async buffer use while in flight; +- async buffer use while in flight (P7, with task-aware I/O); - invalid atomic ordering; - missing capability; - undeclared effect; diff --git a/docs/plan/phase-3-structured-concurrency.md b/docs/plan/phase-3-structured-concurrency.md index 4ec5ddb..be0b6a8 100644 --- a/docs/plan/phase-3-structured-concurrency.md +++ b/docs/plan/phase-3-structured-concurrency.md @@ -4,6 +4,8 @@ Non-normative. Part of the [implementation plan](README.md). **Goal:** tasks that cannot outlive their scopes, ownership transfer through bounded channels, synchronized shared state, cooperative cancellation, and a deterministic scheduler. +**Out of scope (owner decision):** async I/O and its buffer claims (`E4303` stays reserved; P7-10); `RwLock`, several receivers and atomics (later RFCs). No further features are added to Phase 3. + **Exit criterion:** `parallel_map` and `channel_transfer` examples run under deterministic and randomized schedules. **Spec inputs:** spec 04 §1–19, 11 §3, §5–6. RFC 0002 is proposed for acceptance (owner decision); acceptance integrates it into the spec. @@ -15,7 +17,6 @@ Non-normative. Part of the [implementation plan](README.md). - no child task outlives its scope; no silent detachment; - values moved into migratable tasks are `Send`; concurrently read values are `Share`; - concurrent mutable borrows are proven disjoint; -- buffers owned by in-flight async operations are inaccessible; - every interpreter run is reproducible from its seed. ## Tasks @@ -32,7 +33,7 @@ Non-normative. Part of the [implementation plan](README.md). | P3-7 | done | Interpreter tasks: spawn, join, cancel, cancellation points, shields, task failure | P3-3 | `src/interp/` | cancellation observed only at defined points; shield defers it | | P3-8 | done | Bounded and rendezvous channels with ownership transfer | P3-7 | interpreter + verifier | sender cannot use moved message (E2001); capacity 0 rendezvous test | | P3-9 | done | Mutex with linear guards and the critical-section rule (E4304); RwLock deferred | P3-7 | interpreter + verifier | guard not `Send`; unlock consumes guard; E3002 on leaked guard | -| P3-10 | todo | Async I/O buffer claims (depends on task-aware I/O, spec 04 §36.1; deferral proposed to the owner) | P3-7 | verifier + runtime | E4303 accept/reject | +| P3-10 | moved | Async I/O buffer claims: moved to P7-10 (owner decision; needs task-aware I/O) | P3-7 | verifier + runtime | E4303 accept/reject | | P3-11 | todo | Blocking effect and blocking pool policy | P3-7 | spec 04 §18, runtime | `thread.block` from ordinary task handled per spec | | P3-12 | doing | Runner: `seed:`/`schedules:`/`quantum:` headers (schedule budget kept separate from fuel); stress modes (1/2/N workers, random, forced yield, slow consumer, cancellation during waits, I/O reordering) | P3-3 | `tools/conformance.zig` | stress modes selectable; failing seed printed for replay | | P3-13 | todo | Reference programs: parallel-map, channel transfer, tcp-echo with tasks | P3-8, P3-9 | `tests/programs/` | pass under all stress modes | @@ -40,4 +41,4 @@ Non-normative. Part of the [implementation plan](README.md). ## Conformance tests to add -Negative corpus entries owned by this phase: non-Send transfer, non-Share concurrent read, task escape, async buffer use while in flight. Plus E4101, E4302. Scheduler tests for every synchronization primitive (testing policy in AGENTS.md). +Negative corpus entries owned by this phase: non-Send transfer, non-Share concurrent read, task escape. Plus E4101, E4302. Scheduler tests for every synchronization primitive (testing policy in AGENTS.md). diff --git a/docs/plan/phase-7-native-backend.md b/docs/plan/phase-7-native-backend.md index 46802e9..877accd 100644 --- a/docs/plan/phase-7-native-backend.md +++ b/docs/plan/phase-7-native-backend.md @@ -22,4 +22,5 @@ Non-normative. Part of the [implementation plan](README.md). | P7-6 | todo | FFI (`extern "C"`), `@repr(C)`, `@align`, packed fields | P7-3 | backend | E6101 accept/reject; ABI tests against C | | P7-7 | todo | Targets: Linux aarch64, macOS aarch64 | P7-5 | CI matrix | corpus passes on all three targets | | P7-8 | todo | Parity runner: interpreter vs native on the full corpus | P7-5 | `zig build parity` | identical stdout and trap records | +| P7-10 | todo | Task-aware I/O (`io.read_start`, `io.write_start`, `io.wait`) and async buffer claims: buffer state `available → IN_FLIGHT → available`; access while `IN_FLIGHT` is `E4303` (moved from P3-10; needs an RFC) | P7-5 | RFC, verifier + runtime | E4303 accept/reject; deterministic completion order | | P7-9 | todo | Phase exit review | all | plan status | exit criterion passes | diff --git a/docs/rfcs/0012-channels-mutex-select-cancellation.md b/docs/rfcs/0012-channels-mutex-select-cancellation.md index 5ab505b..74d0ef4 100644 --- a/docs/rfcs/0012-channels-mutex-select-cancellation.md +++ b/docs/rfcs/0012-channels-mutex-select-cancellation.md @@ -5,7 +5,8 @@ Scope: AIR format 0, features `cancellation`, `channels`, `sync` Language version: 0.1 Measured Agent Reference Bundle growth on integration: +15.8 % tokens (estimate: 12–16 %). Integrated into: spec 04 §37–§41; spec 04 §35; spec 05 §9; spec 07 §8.11; spec 07a; spec 08 §14; spec 11 §8; `reference/air-features.md`, `effects.md`, `error-codes.md`; `docs/design/decisions.md` DD-014; `docs/agent/bundle.txt` -Supersedes: — +Supersedes: — +Erratum (owner decision after implementation): rejected example 3 corrected to `E2002` (consumption after `send`); `E2001` is a non-consuming use. `mutex` is always linear (spec 04 §40). Resolves: Q-013 (deterministic `select` and the scheduler's choice points). Realizes spec 04 §7–§9, §12, §14–§16 for AIR. Builds on RFC 0011 (spec 04 §33–§36). @@ -364,7 +365,8 @@ locked(%g: guard): ```text channel.send %txb, %v, ok -> @sent, closed -> @closed, cancelled -> @c sent: - %w = own.take %v ; E2001: %v was consumed by the send + %w = own.take %v ; E2002: %v was already consumed by the send + ; (a non-consuming use, e.g. borrow.ref %v, is E2001) ``` ```text diff --git a/docs/spec/04-concurrency-and-atomics.md b/docs/spec/04-concurrency-and-atomics.md index 1320d65..d5be6b2 100644 --- a/docs/spec/04-concurrency-and-atomics.md +++ b/docs/spec/04-concurrency-and-atomics.md @@ -658,13 +658,15 @@ Decision record: RFC 0012. Depends on `cancellation`. `RwLock` is deferred (rese | Instruction | Operands → result | Effect | Verifier | Runtime | |---|---|---|---|---| | `%m = mutex.make %v` | `T` (consumed) → `mutex` | — | element rules | unlocked; no allocation | -| `%v = mutex.take %m` | `mutex` (consumed) → `T` | — | move rules | — | +| `%v = mutex.take %m` | `mutex` (consumed) → `T` | — | no live borrow of `%m` (`E2101`; `E4101` if lent to a live task) | returns the contents; no unlock, no destructor | | `mutex.lock %mr, ok -> @ok, cancelled -> @c` (terminator) | `ref>`; `@ok(guard)`, `@c()` | `task.wait` | the guard is a child of `%mr` (ending `%mr` while it is live is `E2102`); §41 | waiting operation; ready when unlocked; acquire | | `%p = mutex.borrow %g` | `guard` (not consumed) → `mut` | — | child borrow of `%g`, ended before `mutex.unlock %g` (`E2102`) | — | | `mutex.unlock %g` | `guard` (consumed) | — | — | release; all tasks waiting on this mutex become runnable and the first scheduled takes it; a scheduling point | No fairness is promised (§32). There is no poisoning and no implicit unlock; an unconsumed guard is `E3002`. +`mutex` is always linear and never copyable, whatever `T` is; `mutex.take` is the only way to dismantle it. It requires that no borrow of the mutex is live. A guard is a child of the `ref>` it was locked through (ending that borrow while the guard lives is `E2102`), and a task waiting in `mutex.lock` holds such a borrow, so no guard and no waiter can exist at `mutex.take`. Afterwards the mutex is dead, by the ordinary rules for a dismantled linear value (as after `own.take`): a further use is `E3001` and a further consumption `E2002`. + ## 41. The critical-section rule On every path where a `guard` is live, a function MUST NOT execute an instruction that requires the effect `task.wait` or `task.spawn`, nor call a function whose declared effects include `task.wait` or `task.spawn` (`E4304`, `sync.wait_while_locked`, naming the guard and the instruction). diff --git a/tests/conformance/phase3/reject/sync_take_while_borrowed.air b/tests/conformance/phase3/reject/sync_take_while_borrowed.air new file mode 100644 index 0000000..a407d65 --- /dev/null +++ b/tests/conformance/phase3/reject/sync_take_while_borrowed.air @@ -0,0 +1,18 @@ +; mutex.take needs no live borrow of the mutex, so no guard or waiter can exist. +; expect: reject E2101 +; phase: 3 +; spec: docs/spec/04-concurrency-and-atomics.md +module test.sync_take_while_borrowed +air 0 +lang 0.1 +features [cancellation, effects, ownership, sync, tasks] + +fn @f() -> u64 +effects [] +entry: + %z = const.u64 0 + %m = mutex.make %z + %mr = borrow.ref %m + %v = mutex.take %m + borrow.end %mr + ret %v diff --git a/tests/conformance/phase3/reject/sync_use_after_take.air b/tests/conformance/phase3/reject/sync_use_after_take.air new file mode 100644 index 0000000..5f0397e --- /dev/null +++ b/tests/conformance/phase3/reject/sync_use_after_take.air @@ -0,0 +1,18 @@ +; After mutex.take the mutex is dead: borrowing it again is a use after consumption (as after own.take). +; expect: reject E3001 +; phase: 3 +; spec: docs/spec/04-concurrency-and-atomics.md +module test.sync_use_after_take +air 0 +lang 0.1 +features [cancellation, effects, ownership, sync, tasks] + +fn @f() -> u64 +effects [] +entry: + %z = const.u64 0 + %m = mutex.make %z + %v = mutex.take %m + %mr = borrow.ref %m + borrow.end %mr + ret %v