Skip to content

Implement Rc[T] and Arc[T] (+ Shared[T] alias) (§8.2, §8.4, §14.16) #470

Closed

Description

@ehartford

Summary

Implement explicit reference-counted ownership types: Rc[T], Arc[T], and the Shared[T] = Arc[RwLock[T]] alias. §8 deliberately rejects transparent GC or hidden refcounting, so these types are the explicit shared-ownership escape hatch for data structures, allocator handles, and cross-task sharing.

Spec

§8.2 says reference counting exists only when explicitly used: Rc[T] is single-threaded and Arc[T] is thread-safe (docs/with-specification.md:2889). §8.3 shows owned allocator handles such as Rc.clone(&shared_arena) making containers storable instead of ephemeral (docs/with-specification.md:2919). §8.4 defines type Shared[T] = Arc[RwLock[T]] and says it is usable with with blocks for scoped access (docs/with-specification.md:2960). §14.16's Send table says Arc[T] where T: Send + Sync is Send/ScopedSend, while Rc[T] is neither (docs/with-specification.md:7089).

Required user surface:

let a = Rc.new(value)
let b = a.clone()
assert(a.strong_count() == 2)

let shared: Shared[State] = Shared.new(State {})
with shared.read() as state:
    use(state)

Rc[T] and Arc[T] must not be Copy; cloning must be explicit and must increment the count.

Current state

The original body says neither type exists, and that is still true for the user-facing stdlib. lib/std/collections.w defines Vec, HashMap, HashSet, Handle, and related collection helper types but no Rc/Arc (lib/std/collections.w:8). lib/std/alloc.w contains early Arena/Pool wrappers but no owned allocator-handle support (lib/std/alloc.w:7). lib/std/sync.w currently has non-generic Mutex and RwLock facades that store only an i64, so Shared[T] = Arc[RwLock[T]] cannot be expressed correctly yet (lib/std/sync.w:5, lib/std/sync.w:22).

Sema has a symbol slot for Box but no Rc or Arc builtin symbols (src/Sema.w:145). Send/Sync enforcement is separately tracked because the current table is vacuous without real Rc/Arc witnesses (#473). Generic stdlib implementation is blocked by the same TY_GENERIC_INST erasure described in #391; the handoff specifically calls #391 the keystone for issues including #470.

Implementation plan

  1. Wait for Fix sema generic type instantiation (TY_GENERIC_INST erasure) — prerequisite for stdlib-as-generics #391 or implement it first. Rc[T] and Arc[T] must be real per-instantiation generic types; adding compiler intrinsics around erased generic identities would deepen the current root cause.
  2. Add an owned-pointer module, likely lib/std/rc.w or lib/std/mem.w, and import it from the alloc/std prelude tier. Rc/Arc are heap types, so they belong in alloc/full std, not core-only no_std.
  3. Define Rc[T] as a non-Copy owning handle to a heap allocation containing strong count, optional weak count if Weak is included in scope, and T. Provide Rc.new(value), Rc.clone(self: &Self) -> Rc[T], Rc.strong_count(self: &Self) -> usize, and Deref[T] once Implement user Deref trait for auto-deref (§3.7) #407 exists.
  4. Define Arc[T] with the same API but atomic reference counting. Use or extend the runtime/stdlib atomic substrate rather than the current AtomicI64 facade if Implement generic Atomic[T] per §14.17.1 #394 has not already generalized atomics.
  5. Implement Drop for both types. The last strong reference must drop T exactly once and free the allocation. Clones must not double-drop. If weak references are deferred, make that explicit in docs and data layout.
  6. Integrate trait rules: Rc[T] and Arc[T] must fail Copy; Rc[T] must be rejected by Send/ScopedSend enforcement; Arc[T] is Send/ScopedSend only when T: Send + Sync after Send/Sync enforcement substance (§14.16) #473.
  7. Add type Shared[T] = Arc[RwLock[T]] after RwLock[T] is real. If Real generic Mutex[T]/RwLock[T] with fiber-aware blocking (§14.17) #471 has not completed generic locking, either sequence after Real generic Mutex[T]/RwLock[T] with fiber-aware blocking (§14.17) #471 or land Rc/Arc first and leave Shared[T] behind a loud TODO issue dependency, not a fake alias.
  8. Make auto-deref use Implement user Deref trait for auto-deref (§3.7) #407's user Deref trait. Do not special-case Rc/Arc in Sema.auto_deref_ref_ptr_type.

Test plan

Add behavior tests:

  • test/behavior/behav_rc_basic.w: Rc.new, clone increments count, both handles deref/read the same value, last drop frees without double-drop;
  • test/behavior/behav_arc_basic.w: same API for Arc, including cross-task/thread transfer once Send enforcement is available;
  • test/behavior/behav_shared_alias.w: Shared[T] aliases Arc[RwLock[T]] and works with with read/write blocks once Real generic Mutex[T]/RwLock[T] with fiber-aware blocking (§14.17) #471 is complete.

Add compile-error tests:

  • test/compile_errors/err_rc_is_not_copy.w proves assignment moves or explicit clone is required;
  • test/compile_errors/err_rc_not_send.w proves Rc[T] cannot cross spawn/task boundaries after Send/Sync enforcement substance (§14.16) #473;
  • test/compile_errors/err_arc_requires_send_sync_inner.w proves Arc[NonSend] is not Send;
  • no_std/core-tier tests prove Rc/Arc are not available without alloc.

Run leak/double-drop focused tests for non-Copy payloads with a counter side effect.

Acceptance criteria

  • Rc[T], Arc[T], and Shared[T] are available in the correct stdlib tiers.
  • Cloning is explicit and increments counts; assignment does not copy either type.
  • Last strong drop runs payload Drop exactly once and frees memory.
  • Rc[T] is single-threaded/non-Send; Arc[T] follows §14.16 Send/Sync rules.
  • Rc/Arc field and method auto-deref works through the ordinary Deref trait path.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

#391 is prerequisite for correct generic identity. #407 should provide the auto-deref path. #473 is required for Send/Sync enforcement. #471 is required before the Shared[T] = Arc[RwLock[T]] alias can be fully useful.

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 8.2.1.1, 8.2.1.2, 8.2.1.3, 8.4.1.1.

Activity

  1. ehartford commented on Jun 15, 2026

    @ehartford
    ContributorAuthor

    Implemented in bf5f817. Added real alloc-tier std.rc Rc[T]/Arc[T] with explicit clone, strong_count, Deref, Drop, atomic Arc refcounting, alloc-tier prelude wiring, no_std/core gating, non-Copy checks, drop-count behavior coverage, auto-deref/shadowing coverage, and generic codegen fixes needed by the implementation. Shared[T] traceability is moved to #471 because the spec alias depends on a real generic RwLock[T]; Send/Sync enforcement remains in #473. Verification passed: build, fixpoint, full test suite, and 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