Skip to content

Make stdlib guards and borrowed-data iterators canonically ephemeral (§5.3) #477

Closed

Description

@ehartford

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, RwWriteGuard are non-generic structs over a copied i64 value. They carry @[no_await_guard] (the §7.9 await rule) but are not marked ephemeral — 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 generic Mutex[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 raw data_ptr: i64 into the collection (or wrap an iterator that does) and none is marked ephemeral. All are storable.
  • The ephemeral qualifier 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 declared ephemeral.
  • Gap in the checker that bounds what these marks can enforce: §5.2 generic-container propagation is not implemented — Vec[E] for ephemeral E passes check today (verified). Struct-field storage is rejected. (Container propagation is Make ephemerality provenance-tracked and modular #362 phase 5.)
  • Requirements campaign verdicts: 5.3.1.3 partial ("guards i64-only, not ephemeral"); 5.3.1.4 partial ("stdlib iterators not ephemeral").

Implementation plan

  1. Mark the four sync guards ephemeral in lib/std/sync.w (alongside their existing @[no_await_guard]). They are constructed by Mutex.enter()/enter_mut()/RwLock.enter()/enter_mut() and consumed by .exit() / Scoped/ScopedMut impls — all local-binding patterns that remain legal under §5.1. Leave the generic re-shape (MutexGuard[T], spec naming RwLockReadGuard/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.
  2. Mark the borrowed-data iterators ephemeral in lib/std/collections.w: VecIter[T], VecIterPlace[T], and the adapters MapIter, 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.
  3. 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 name VecIter/VecIterPlace for 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.
  4. Check with-guard interaction: guarded with lock.enter_mut() as mut data: blocks (spec_ss07_with_blocks.w:25) bind the payload, not the guard, and check_with_expr already rejects ephemeral guarded-with results (src/SemaCheck.w:7371-7374) — marking the guards must not change any passing guarded-with test.
  5. SlotMap surface (§6.2): SlotMap.get is spec'd "Ephemeral return" — its returned &T is covered by reference-ephemerality, and SlotMapSlot[T] (lib/std/collections.w:62) is a with-scoped handle like VecSlot[T]; include both in the audit and mark SlotMapSlot/VecSlot/HashMapEntry ephemeral if the audit shows no storage uses (they are scoped-mutation handles holding raw map_ptr/data_ptr — same dangling risk as iterators).
  6. 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 a MutexGuard field rejected; sibling case for RwWriteGuard.
  • 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(), guarded with blocks, Scoped impl 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 :test pass (and with build :test-green recorded).

Dependencies

Traceability

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

Activity

  1. ehartford commented on Jun 12, 2026

    @ehartford
    ContributorAuthor

    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.

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