Repository navigation
Description
Make stdlib guards and borrowed-data iterators canonically ephemeral (§5.3)
Summary
§5.3 names the canonical ephemeral types: references, views, lock guards, and iterators over borrowed data. The stdlib's own members of that list are not ephemeral today — the sync lock guards are storable non-generic i64 wrappers, and every collection iterator (VecIter and the lazy adapters) is a plain storable struct. That means user code can store a lock guard or a borrowing iterator in a long-lived struct, which is exactly what the ephemeral system exists to prevent. Mark the stdlib's guards and borrowed-data iterators ephemeral.
Spec
§5.3 Canonical Ephemeral Types (docs/with-specification.md:2250-2255):
- References:
&T - Views:
StrView/&str,&[T] - Lock guards:
MutexGuard[T],RwLockReadGuard[T],RwLockWriteGuard[T] - Iterators over borrowed data
§5.1: ephemeral values may be locals, parameters, and returns (with propagation) but not struct fields, enum variants, heap containers, global storage, or escaping closures. §13.1: "Iterators holding references to collections are ephemeral. They can be used in pipelines within scope but not stored, returned [opaquely], or captured by escaping closures."
Current state
- Lock guards (lib/std/sync.w:11-37):
MutexGuard,MutexGuardMut,RwReadGuard,RwWriteGuardare non-generic structs over a copiedi64value. They carry@[no_await_guard](the §7.9 await rule) but are not markedephemeral— storing one in a struct/Vec compiles today. They also don't match the spec's generic naming (MutexGuard[T],RwLockReadGuard[T],RwLockWriteGuard[T]); real genericMutex[T]/RwLock[T]is Real generic Mutex[T]/RwLock[T] with fiber-aware blocking (§14.17) #471's scope. - Iterators (lib/std/collections.w:95-131):
VecIterPlace[T](:99),VecIter[T](:111),MapIter[I,T,U](:114),FilterIter[I,T](:117),TakeIter[I,T](:120),ZipIter[A,B,T,U](:123),FlatMapIter[I,C,J,T,U](:126) — all hold a rawdata_ptr: i64into the collection (or wrap an iterator that does) and none is markedephemeral. All are storable. - The
ephemeralqualifier works on generic type decls (verified:type W[T] ephemeral { v: T }checks), and storage rejection works ("ephemeral type 'X' cannot be stored in non-ephemeral struct", verified). Stdlib precedent exists:ScopedTask[T](lib/std/task.w:18),ScopedJoinHandle(lib/std/thread.w:17),Context(lib/std/context.w:31),Workspace(lib/std/build.w:281) are all declaredephemeral. - Gap in the checker that bounds what these marks can enforce: §5.2 generic-container propagation is not implemented —
Vec[E]for ephemeralEpasseschecktoday (verified). Struct-field storage is rejected. (Container propagation is Make ephemerality provenance-tracked and modular #362 phase 5.) - Requirements campaign verdicts:
5.3.1.3partial ("guards i64-only, not ephemeral");5.3.1.4partial ("stdlib iterators not ephemeral").
Implementation plan
- Mark the four sync guards
ephemeralin lib/std/sync.w (alongside their existing@[no_await_guard]). They are constructed byMutex.enter()/enter_mut()/RwLock.enter()/enter_mut()and consumed by.exit()/Scoped/ScopedMutimpls — all local-binding patterns that remain legal under §5.1. Leave the generic re-shape (MutexGuard[T], spec namingRwLockReadGuard/RwLockWriteGuard) to Real generic Mutex[T]/RwLock[T] with fiber-aware blocking (§14.17) #471; note in sync.w doc comments that the names converge there. - Mark the borrowed-data iterators
ephemeralin lib/std/collections.w:VecIter[T],VecIterPlace[T], and the adaptersMapIter,FilterIter,TakeIter,ZipIter,FlatMapIter. The adapters are unconditionally ephemeral (conservative): §5.2's conditional propagation ("F[T]ephemeral iff component ephemeral") isn't implemented yet (Make ephemerality provenance-tracked and modular #362/Fix sema generic type instantiation (TY_GENERIC_INST erasure) — prerequisite for stdlib-as-generics #391), and every stdlib adapter wraps an iterator that borrows collection data, so unconditional marking is both sound and accurate for the stdlib's own pipeline use. Revisit conditionality when Make ephemerality provenance-tracked and modular #362 phase 5 lands. - Audit before flipping (the compiler and stdlib are compiled under these marks — a violation breaks the build):
grep -rn "VecIter\|MapIter\|FilterIter\|TakeIter\|ZipIter\|FlatMapIter\|MutexGuard\|RwReadGuard\|RwWriteGuard" src/ lib/ build.w test/and inspect any struct-field/container storage of these types. src/Codegen.w and src/CCodegen.w nameVecIter/VecIterPlacefor layout special-casing only (src/CCodegen.w:1660-1662, :7525; src/Codegen.w:476-477) — ephemerality is a sema-only property and does not alter layout, so codegen is unaffected; confirm.- Pipelines (
|> map() |> collect()) produce adapters as locals/arguments/returns — legal under §5.1 propagation; spec tests spec_ss13_*.w must keep passing.
- Check
with-guard interaction: guardedwith lock.enter_mut() as mut data:blocks (spec_ss07_with_blocks.w:25) bind the payload, not the guard, andcheck_with_expralready rejects ephemeral guarded-with results (src/SemaCheck.w:7371-7374) — marking the guards must not change any passing guarded-with test. - SlotMap surface (§6.2):
SlotMap.getis spec'd "Ephemeral return" — its returned&Tis covered by reference-ephemerality, andSlotMapSlot[T](lib/std/collections.w:62) is awith-scoped handle likeVecSlot[T]; include both in the audit and markSlotMapSlot/VecSlot/HashMapEntryephemeral if the audit shows no storage uses (they are scoped-mutation handles holding rawmap_ptr/data_ptr— same dangling risk as iterators). - Known enforcement limit, stated honestly: until Make ephemerality provenance-tracked and modular #362 phase 5,
Vec[VecIter[i32]](heap-container storage) is not rejected by the container path; struct-field storage is. The tests below assert what is enforceable now and carry a cross-reference comment for the container case.
Test plan
- test/compile_errors/err_lock_guard_struct_storage.w —
//! expect-check-fail: ephemeral type— struct with aMutexGuardfield rejected; sibling case forRwWriteGuard. - test/compile_errors/err_vec_iter_struct_storage.w — struct field of type
VecIter[i32](and one adapter, e.g.FilterIter) rejected. - test/compile_errors/err_iter_escaping_closure.w — escaping closure capturing a live iterator rejected (reuses the escaping-closure-captures-ephemeral machinery).
- test/behavior/behav_guard_local_use.w —
//! expect-stdout: ok— guards as locals:let g = m.enter(); ... g.exit(), guardedwithblocks,Scopedimpl path — all still work. - test/behavior/behav_iter_pipeline_local.w —
//! expect-stdout: ok— iterator + adapters used in pipelines, passed to a helper as a parameter, and returned from a concrete-typed function (§5.1 propagation; ties into Tests: ephemeral iterators coverage (§13) #497's coverage). - Regression: spec_ss07_with_blocks.w, spec_ss07_1_*, spec_ss13_2_iterator_borrowing.w, mut_with_slot.w, mut_with_entry.w unchanged and green.
Acceptance criteria
- All four sync guards and all seven collection iterator types in lib/std are declared
ephemeral. - Storing any of them in a non-ephemeral struct field is a compile error (tests above).
- Local/parameter/return/pipeline use compiles and runs unchanged; no existing spec/behavior test regresses.
- Compiler source itself builds under the new marks (audit complete, fixpoint converges).
-
with build && with build :fixpoint && with build :testpass (andwith build :test-greenrecorded).
Dependencies
- Real generic Mutex[T]/RwLock[T] with fiber-aware blocking (§14.17) #471 (real generic Mutex[T]/RwLock[T]) — supplies the spec-named generic guards; this issue marks today's guards so the safety rule isn't blocked on the rewrite.
- Make ephemerality provenance-tracked and modular #362 (provenance/§5.2 container propagation) — closes the heap-container storage hole.
- Tests: ephemeral iterators coverage (§13) #497 (ephemeral iterator tests) — shares the positive-coverage tests.
- Fix sema generic type instantiation (TY_GENERIC_INST erasure) — prerequisite for stdlib-as-generics #391 (generic instantiation) — conditional adapter ephemerality.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 5.3.1.3, 5.3.1.4.
Activity
ehartford commentedon Jun 12, 2026
Implemented in cb6d37d. Marked std sync guards, borrowed-data iterators/adapters, and scoped borrowed handles as ephemeral; added negative storage/capture coverage and positive local/with/pipeline coverage. Verification passed: with build, with build :fixpoint, with build :test, and with build :test-green.