Skip to content

Add ArenaScope guard type with @[no_await_guard] (§7.9, §8.3a) #411

Closed

Description

@ehartford

Add ArenaScope guard type with @[no_await_guard] (§7.9, §8.3a)

Summary

§7.9 names ArenaScope as one of the four stdlib types that carry @[no_await_guard], and its rule table lists "Arena scope across await" as a compile error. No ArenaScope type exists in std.alloc today, so the rule is unimplementable. The enforcement machinery itself (NLL-based E0701) is already built and tested — this issue adds the missing stdlib type, annotates it, and locks the rule in with tests.

Spec

§7.9 (docs/with-specification.md:2770-2862), §8.3a (docs/with-specification.md:2928-2960).

  • §7.9 table (docs/with-specification.md:2840-2846): Arena scope | Yes (@[no_await_guard]) | .await in with → **Error**.
  • §7.9 (docs/with-specification.md:2849-2852): "Standard library types that carry @[no_await_guard]: MutexGuard, ReadGuard, WriteGuard, and ArenaScope."
  • §7.9 (docs/with-specification.md:2789-2792): the compiler rejects any same-fiber operation that may suspend while a @[no_await_guard] value is live in the NLL sense — whether created via with or a plain let.
  • §20b.1 (docs/with-specification.md:10354-10362): same rule restated; diagnostic code E0701.
  • §8.3a context: Arena is the long-lived, user-controlled-reset region allocator; a scope of an arena is a lexically-bounded allocation region.

Current state

  • Enforcement exists and is NLL-based: @[no_await_guard] parses into AstPool.mark_no_await_guard_type (src/Ast.w:1221-1226), sema tracks annotated types in no_await_guard_types (src/Sema.w:355) and checks liveness across may-suspend operations (type_is_no_await_guard src/SemaCheck.w:1934-1955, fn_symbol_may_suspend/expr_may_suspend :2058+, guard-scope depth :1959). Diagnostic code E0701. Tested by test/spec/spec_ss07_9_nll_based_no_await_guard.w, test/compile_errors/err_may_suspend_call_under_no_await_guard.w, err_suspend_guard_derived_ref*.w.
  • The stdlib annotates its lock guards: MutexGuard, MutexGuardMut, RwReadGuard, RwWriteGuard (lib/std/sync.w:11-37).
  • lib/std/alloc.w (66 lines) has Arena (a block_size-only malloc passthrough), TempArena (real: records allocations, frees on reset/drop), and Pool. No ArenaScope type exists anywhere (grep -rn ArenaScope lib/ src/ → only spec hits). So requirement 7.9.1.10 has nothing to attach to, and 7.9.1.14's stdlib set is missing one of its four members.
  • Requirements campaign verdicts: 7.9.1.10 absent ("no ArenaScope guard type"); 7.9.1.14 partial ("ArenaScope missing from set").

Implementation plan

  1. Add ArenaScope to lib/std/alloc.w, modeled on TempArena's real semantics so it is not a stub:
    /// Scoped allocation region over an Arena. All allocations made
    /// through the scope are released when the scope ends.
    @[no_await_guard]
    pub type ArenaScope {
        allocations: Vec[i64],
    }
    
    With methods ArenaScope.alloc(mut self, size: i32) -> *i8, ArenaScope.alloc_zeroed(mut self, count: i32, size: i32) -> *i8 (record pointers like TempArena does), and ArenaScope.drop(mut self) freeing recorded allocations. Until First-class allocators: real Arena/FrameArena/Pool, Vec.new_in, ephemeral virality (§8.3) #437 lands a real region Arena, the scope's release mechanism is per-allocation free (same honest interim semantics TempArena already uses); First-class allocators: real Arena/FrameArena/Pool, Vec.new_in, ephemeral virality (§8.3) #437 replaces the internals with a watermark (mark/reset_to(mark)) without changing the type's surface or annotation.
  2. Constructor: pub fn Arena.scope(self: &Arena) -> ArenaScope in lib/std/alloc.w. The guard is produced from the arena and used either as a plain binding (let s = arena.scope()) or via with arena.scope() as mut s: (Form 2 binding — the NLL check covers both per §7.9; no Scoped impl is required for E0701 to apply, and lib/std/sync.w shows the optional Scoped/ScopedMut pattern if guarded-with ergonomics are wanted later).
  3. No compiler changes are needed: the annotation machinery is data-driven off @[no_await_guard] in the type decl. Verify type_is_no_await_guard resolves the stdlib type through the prelude/module path the same way MutexGuard does (it keys off the type symbol; sync.w proves the pattern).
  4. Bootstrap note: this only touches lib/std (embedded into the compiler binary at build time). No rt/Link.w hazard; normal with build + :fixpoint chain applies.
  5. Spec cross-check: §7.9 calls the lock guard types ReadGuard/WriteGuard while lib/std/sync.w spells them RwReadGuard/RwWriteGuard — out of scope here (Make stdlib guards and borrowed-data iterators canonically ephemeral (§5.3) #477/Real generic Mutex[T]/RwLock[T] with fiber-aware blocking (§14.17) #471 territory), but keep ArenaScope spelled exactly as the spec names it.

Test plan

  • test/compile_errors/err_arena_scope_across_await.w — //! expect-error: E0701 (match the existing fixture style of err_may_suspend_call_under_no_await_guard.w): create let s = arena.scope(), keep it live (use it after) across a task.await → error.
  • test/compile_errors/err_arena_scope_may_suspend_call.w — calling a may-suspend helper (a fn containing .await internally) while an ArenaScope is live → E0701.
  • test/spec/spec_ss07_9_arena_scope_guard.w — //! expect-stdout: ok, positive cases mirroring spec_ss07_9_nll_based_no_await_guard.w: (a) last use of the scope before the await → OK; (b) explicit drop(s) before await → OK; (c) with arena.scope() as mut s: block ends before the await → OK; (d) allocations through the scope actually work (s.alloc(64) non-null, write/read through it).
  • test/behavior/behav_arena_scope.w — //! expect-stdout: ok: scope releases its allocations at end of scope (smoke: allocate, drop scope, allocate again; with First-class allocators: real Arena/FrameArena/Pool, Vec.new_in, ephemeral virality (§8.3) #437's watermark this upgrades to a reuse assertion).

Acceptance criteria

Dependencies

Traceability

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

Activity

  1. ehartford commented on Jun 12, 2026

    @ehartford
    ContributorAuthor

    Implemented in 89f4905.

    Summary:

    • Added public Arena and @[no_await_guard] ephemeral ArenaScope to std.alloc.
    • Added Arena.scope() plus ArenaScope.alloc, alloc_zeroed, reset, drop, and allocation_count using the existing TempArena-style allocation tracking until First-class allocators: real Arena/FrameArena/Pool, Vec.new_in, ephemeral virality (§8.3) #437 replaces the internals.
    • Added behavior/spec coverage for allocation smoke, explicit drop before await, and with arena.scope() as mut ending before await.
    • Added E0701 compile-error coverage for plain ArenaScope bindings, may-suspend calls, and with blocks held across suspension.

    Verification passed:

    • with build
    • with build :fixpoint
    • with build :test
    • 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