Repository navigation
Description
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
- Wait for Fix sema generic type instantiation (TY_GENERIC_INST erasure) — prerequisite for stdlib-as-generics #391 or implement it first.
Rc[T]andArc[T]must be real per-instantiation generic types; adding compiler intrinsics around erased generic identities would deepen the current root cause. - Add an owned-pointer module, likely
lib/std/rc.worlib/std/mem.w, and import it from the alloc/std prelude tier.Rc/Arcare heap types, so they belong in alloc/full std, not core-only no_std. - Define
Rc[T]as a non-Copyowning handle to a heap allocation containing strong count, optional weak count ifWeakis included in scope, andT. ProvideRc.new(value),Rc.clone(self: &Self) -> Rc[T],Rc.strong_count(self: &Self) -> usize, andDeref[T]once Implement user Deref trait for auto-deref (§3.7) #407 exists. - Define
Arc[T]with the same API but atomic reference counting. Use or extend the runtime/stdlib atomic substrate rather than the currentAtomicI64facade if Implement generic Atomic[T] per §14.17.1 #394 has not already generalized atomics. - Implement
Dropfor both types. The last strong reference must dropTexactly once and free the allocation. Clones must not double-drop. If weak references are deferred, make that explicit in docs and data layout. - Integrate trait rules:
Rc[T]andArc[T]must failCopy;Rc[T]must be rejected by Send/ScopedSend enforcement;Arc[T]is Send/ScopedSend only whenT: Send + Syncafter Send/Sync enforcement substance (§14.16) #473. - Add
type Shared[T] = Arc[RwLock[T]]afterRwLock[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 landRc/Arcfirst and leaveShared[T]behind a loud TODO issue dependency, not a fake alias. - Make auto-deref use Implement user Deref trait for auto-deref (§3.7) #407's user
Dereftrait. Do not special-caseRc/ArcinSema.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 forArc, including cross-task/thread transfer once Send enforcement is available;test/behavior/behav_shared_alias.w:Shared[T]aliasesArc[RwLock[T]]and works withwithread/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.wproves assignment moves or explicit clone is required;test/compile_errors/err_rc_not_send.wprovesRc[T]cannot crossspawn/task boundaries after Send/Sync enforcement substance (§14.16) #473;test/compile_errors/err_arc_requires_send_sync_inner.wprovesArc[NonSend]is not Send;- no_std/core-tier tests prove
Rc/Arcare 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], andShared[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
Dropexactly once and frees memory. Rc[T]is single-threaded/non-Send;Arc[T]follows §14.16 Send/Sync rules.Rc/Arcfield and method auto-deref works through the ordinaryDereftrait path.with build && with build :fixpoint && with build :testpass.
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
Requirements triage campaign: checkbox, verdict, and issue-link every…
ehartford commentedon Jun 15, 2026
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.