Repository navigation
Description
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
- Audit
ComptimeTransform.wderive generation and classify each target as sema-checked, codegen-backed, or test-only. UpdateDeriveReqand sema metadata insrc/Sema.wso every built-in derive target is represented semantically. - Implement
Ordderive as lexicographic field comparison using the stdlibOrd.cmpcontract. Ensure generated code handles zero-field types and short-circuits on first non-zero comparison. - Implement enum
Displayderive as variant-name formatting. For payload variants, follow the spec's “variant name as string” rule unless BDFL changes §11.8. - Tighten field validation for
Eq,Hash,Ord,Default, andClone: emit a compile-time diagnostic naming the field and missing trait/method instead of generating code that later fails with an unknown method. - Preserve
derive(all)conservative semantics: derive all eligible traits, never inferCopy, and silently skip ineligible non-required targets only where §11.8 says no error by design. - 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.wtest/compile_errors/err_derive_hash_field_without_hash.wtest/compile_errors/err_derive_default_field_without_default.wif not already sufficienttest/compile_errors/err_derive_unknown_target.wtest/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 enumderive(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 grantsCopy.- User-defined derive has a loud, type-checked generation path with no silent placeholder output.
with build && with build :fixpoint && with build :testpass.
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
ehartford commentedon Jun 13, 2026
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.