Repository navigation
Description
Summary
Align object safety with §11.3 by excluding consuming receiver methods from reference-object vtables and adding the Box[dyn Trait] consuming-receiver shim once Box exists. The current implementation rejects the whole trait object when any method uses move self: Self.
Spec
§11.3 says self: &Self and mut self: Self methods are object-safe. move self: Self methods are excluded from &dyn Trait vtables because dyn Trait is unsized, while Box[dyn Trait] can support a consuming receiver shim.
Current state
Sema.ensure_trait_object_safe currently walks every method and emits an object-safety error for any move self: Self method at src/SemaDecl.w:1817-1878, specifically src/SemaDecl.w:1857-1860. Type validation calls that checker for ordinary dyn Trait at src/SemaDecl.w:1933-1940. Existing spec coverage notes the gap directly: test/spec/spec_ss11_3_object_safety.w:44-45 says consuming move self: Self through Box[dyn Trait] is tracked separately because Box[T] and shims do not exist.
Codegen currently collects every trait method into vtable metadata (src/CodegenTraits.w:41-73) and emits one field per method. There is no metadata bit saying “excluded from reference dyn vtable,” and #475 tracks the missing standard Box[T].
Implementation plan
- Change sema object-safety from trait-wide rejection to per-method vtable eligibility.
&dyn Traitshould allow the trait if all callable-through-reference methods are safe;move self: Selfmethods remain present in trait metadata but are not callable on&dyn Trait. - Add metadata for excluded methods in
Sema/CodegenTraits: method flags should distinguish generic, consumes-self, mentions-Self, and vtable-included status. - Update
Sema.check_dyn_trait_method_callto reject calls to excluded consuming methods on&dyn Traitwith a diagnostic pointing toBox[dyn Trait]. - Update vtable layout generation so excluded methods do not occupy callable slots, or occupy null slots with sema preventing calls. Prefer a deterministic compact layout keyed by included methods only if it does not destabilize existing dyn dispatch.
- After Implement Box[T] (§3.7, §3.9, §8, §18.7) #475 lands, implement
Box[dyn Trait]layout and consuming shim: move the boxed allocation into the concrete method, run the correct drop/deallocation path, and invalidate the box. - Add migration notes for existing tests that expected whole-trait rejection.
Test plan
Add or extend test/spec/spec_ss11_3_object_safety.w:
- trait with both
self: &Selfandmove self: Self &dyn Traitcan call the shared method- calling consuming method on
&dyn Traitis a compile error - after Implement Box[T] (§3.7, §3.9, §8, §18.7) #475,
Box[dyn Trait]can call the consuming method and consumes the box
Add compile-error tests:
test/compile_errors/err_dyn_ref_consuming_method_call.wtest/compile_errors/err_dyn_generic_method_excluded.wif not covered by Tests: object safety coverage (§11) #512
Acceptance criteria
- A trait is not rejected wholesale just because it has a consuming receiver method.
- Reference dyn vtables exclude or block consuming receiver calls deterministically.
- Diagnostics tell the user to use
Box[dyn Trait]for consuming trait-object calls. - Box consuming shim is implemented once Implement Box[T] (§3.7, §3.9, §8, §18.7) #475 provides
Box[T]. with build && with build :fixpoint && with build :testpass.
Dependencies
#475 is required for the Box shim. #431 should share dyn vtable metadata changes. #512 covers missing object-safety tests.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 11.3.1.6, 11.3.1.7, 11.3.1.8.
Activity
ehartford commentedon Jun 13, 2026
Close-out from the
docs/finish.mdaudit:This remains the right tracker for the live object-safety item. The stale checklist called this "partial"; current repo state is sharper:
docs/with-specification.mdstill promises theBox[dyn Trait]consuming-receiver shim.docs/requirements.mdmarks the relevantBox[dyn Trait]consuming-receiver requirements complete.test/spec/spec_ss11_3_object_safety.wstill says the shim does not exist yet and is tracked separately.#475still tracks the underlying user-facingBox[T]type, which this shim depends on.So the immediate issue is not just implementation work; it is also state drift. This should not close until either:
Acceptance addition from this audit: remove the contradiction between spec, requirements, and
test/spec/spec_ss11_3_object_safety.w.ehartford commentedon Jun 19, 2026
Implemented in 2ce60a7, with supporting self-host scheduler fixture stabilization in 4809355. Object safety no longer rejects an entire dyn trait solely because it has a
move self: Selfmethod; consuming dyn calls are rejected on&dyn Traitwith a Box[dyn Trait] diagnostic, and Box[dyn Trait] consuming calls now use a generated wrapper that transfers the concrete value and frees the boxed allocation. Added positive coverage for traits with both reference and consuming methods, Box[dyn] consuming dispatch, and negative coverage for consuming calls through &dyn plus use-after-move of a consumed Box[dyn]. Verification on rebased main passed: build, fixpoint, full test suite, and explicit test-green.