Skip to content

Object safety: vtable exclusion and Box[dyn] consuming-receiver shim (§11.3) #432

Closed

Description

@ehartford

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

  1. Change sema object-safety from trait-wide rejection to per-method vtable eligibility. &dyn Trait should allow the trait if all callable-through-reference methods are safe; move self: Self methods remain present in trait metadata but are not callable on &dyn Trait.
  2. Add metadata for excluded methods in Sema/CodegenTraits: method flags should distinguish generic, consumes-self, mentions-Self, and vtable-included status.
  3. Update Sema.check_dyn_trait_method_call to reject calls to excluded consuming methods on &dyn Trait with a diagnostic pointing to Box[dyn Trait].
  4. 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.
  5. 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.
  6. 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: &Self and move self: Self
  • &dyn Trait can call the shared method
  • calling consuming method on &dyn Trait is 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:

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 :test pass.

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

  1. ehartford commented on Jun 13, 2026

    @ehartford
    ContributorAuthor

    Close-out from the docs/finish.md audit:

    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.md still promises the Box[dyn Trait] consuming-receiver shim.
    • docs/requirements.md marks the relevant Box[dyn Trait] consuming-receiver requirements complete.
    • test/spec/spec_ss11_3_object_safety.w still says the shim does not exist yet and is tracked separately.
    • #475 still tracks the underlying user-facing Box[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:

    1. the consuming-receiver shim is implemented and the stale test comment is replaced by executable coverage, or
    2. the spec/requirements are corrected to say the shim is roadmap/deferred.

    Acceptance addition from this audit: remove the contradiction between spec, requirements, and test/spec/spec_ss11_3_object_safety.w.

  2. ehartford commented on Jun 19, 2026

    @ehartford
    ContributorAuthor

    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: Self method; consuming dyn calls are rejected on &dyn Trait with 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.

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