Skip to content

Complete derive targets: Default, Hash, Ord, Display, derive(all), derive(Builder) (§11.8) #409

Closed

Description

@ehartford

Summary

Bring derive support into alignment with §11.8 and separate stale survey assumptions from real gaps. Several targets originally called absent now exist in the stdlib/tests, but Ord, enum Display, field-bound validation, and user-defined derive/comptime integration still need a complete implementation plan.

Spec

§11.8 says @[derive(...)] may generate Copy, Clone, Default, Eq, Hash, Ord, Debug, and enum Display. @[derive(all)] derives every eligible trait except Copy, which remains explicit opt-in for aggregate types. @[derive(Builder)] generates a builder with optional defaulted fields and MissingField errors. §17.3 covers user-defined derive targets through comptime-generated source that still goes through normal type checking.

Current state

The original survey line that only Copy/Clone/Eq exist is stale. The stdlib declares Ord, Hash, Display, Default, Clone, and related traits at lib/std/traits.w:8-35. Existing derive coverage checks Eq, Hash, Debug, Clone, Default, and derive(all) in test/spec/spec_ss11_8_derive.w:3-78; builder coverage exists in test/spec/spec_ss11_8_derive_builder.w:3-40.

Remaining gaps from the requirements notes are still real: derive(Ord) failed codegen, derive(Display) for enum variants generates nothing, Eq/Hash field validation is incomplete, and there is no general user-defined derive target path. Sema.w:84-87 still has a small DeriveReq enum limited to COPY, CLONE, and EQ, so sema-side validation is not tracking the full §11.8 table even where comptime transformation/codegen implements more.

Implementation plan

  1. Audit ComptimeTransform.w derive generation and classify each target as sema-checked, codegen-backed, or test-only. Update DeriveReq and sema metadata in src/Sema.w so every built-in derive target is represented semantically.
  2. Implement Ord derive as lexicographic field comparison using the stdlib Ord.cmp contract. Ensure generated code handles zero-field types and short-circuits on first non-zero comparison.
  3. Implement enum Display derive as variant-name formatting. For payload variants, follow the spec's “variant name as string” rule unless BDFL changes §11.8.
  4. Tighten field validation for Eq, Hash, Ord, Default, and Clone: emit a compile-time diagnostic naming the field and missing trait/method instead of generating code that later fails with an unknown method.
  5. Preserve derive(all) conservative semantics: derive all eligible traits, never infer Copy, and silently skip ineligible non-required targets only where §11.8 says no error by design.
  6. Design user-defined derives as comptime functions that receive type metadata and return With source/AST to be checked normally. Do not introduce token macros or unchecked textual stubs.

Test plan

Extend test/spec/spec_ss11_8_derive.w with Ord ordering, enum Display, field validation, and derive(all) skip/no-skip behavior. Keep test/spec/spec_ss11_8_derive_builder.w for builder behavior and add a generic builder negative for a missing required field if absent.

Add compile-error cases:

  • test/compile_errors/err_derive_ord_field_without_ord.w
  • test/compile_errors/err_derive_hash_field_without_hash.w
  • test/compile_errors/err_derive_default_field_without_default.w if not already sufficient
  • test/compile_errors/err_derive_unknown_target.w
  • test/compile_errors/err_user_derive_bad_output.w

Acceptance criteria

  • Every §11.8 built-in derive target is represented in sema validation and generated output.
  • derive(Ord) and enum derive(Display) compile and run.
  • Field-bound failures are diagnosed at the derive site with the target and field name.
  • derive(all) remains conservative and never silently grants Copy.
  • User-defined derive has a loud, type-checked generation path with no silent placeholder output.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

#391 helps user-defined/generic derive output type-check reliably. #494 covers additional derive test gaps and should be closed by the test additions here or updated to avoid duplication.

Activity

  1. ehartford commented on Jun 13, 2026

    @ehartford
    ContributorAuthor

    Implemented in e71549b. Completed the remaining derive surface: Ord, enum Display, conservative derive(all), concrete-field validation for explicit derives, user-defined derives via comptime-generated source parsed back through normal sema, and visible Builder-chain missing-required-field diagnostics. 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