Skip to content

Tests: object safety coverage (§11) #512

Closed

Description

@ehartford

Summary

Add object-safety test coverage for generic-method rejection on dyn traits. The implementation rejects generic trait methods for object use, but that rule needs explicit regression coverage.

Spec

§11.3 says dyn trait method calls require object-safe methods. Generic methods are not object-safe because a vtable slot cannot represent open-ended per-instantiation method bodies.

Current state

Sema.ensure_trait_object_safe rejects generic trait methods by checking method flags at src/SemaDecl.w:1865-1867. Parser.parse_trait_decl records the generic method flag when a trait method has type parameters at src/Parser.w:2588-2627. Existing object-safety tests cover shared and mutating receivers (test/spec/spec_ss11_3_object_safety.w:5-42) and separate compile-error tests cover Self in params/returns, but the requirements campaign found generic-method rejection untested.

Implementation plan

  1. Add a compile-error fixture with a trait containing fn id[T](self: &Self, value: T) -> T and a function accepting &dyn Trait.
  2. Assert the current diagnostic substring is generic, or trait '<T>' is not object-safe: method '<m>' is generic if stable.
  3. Add a positive adjacent test where the same trait without method type params can be used as &dyn Trait.
  4. If Object safety: vtable exclusion and Box[dyn] consuming-receiver shim (§11.3) #432 changes object-safety to per-method vtable exclusion, update the expected behavior deliberately: generic methods should either be excluded and uncallable or continue to reject dyn use, but the test must encode the chosen rule.

Test plan

Add:

  • test/compile_errors/err_dyn_trait_generic_method_not_object_safe.w
  • Optional test/spec/spec_ss11_3_object_safety_generic_positive.w for non-generic method dyn use if existing coverage is insufficient.

Acceptance criteria

Dependencies

#432 may change the exact dyn object-safety policy and diagnostic shape.

Traceability

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

Activity

  1. ehartford commented on Jul 3, 2026

    @ehartford
    ContributorAuthor

    Covered by test/compile_errors/err_object_safety_generic_method.w — a trait with fn id[T](self: &Self, value: T) -> T used behind &dyn Container is rejected with trait 'Container' is not object-safe: method 'id' is generic (§11.3). Verified on HEAD; full gate green (native-compile-error-tests 681, native-spec-tests 206, GATES EXIT: 0).

  2. added a commit that references this issue on Jul 4, 2026
    9288f25
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