Repository navigation
Description
Summary
Fix default trait methods so calls resolve through trait impls when an impl omits a method body. Parsing and codegen contain pieces of default-method support, but sema method lookup still reports “unknown method” in the verified failure case.
Spec
§11.6 says default methods in traits are supported in v1.0. An impl may omit a method with a default body, and calls should resolve as if the impl had that method, with Self, trait parameters, and associated types substituted for the concrete impl.
Current state
Parser stores trait method default bodies: Parser.parse_trait_decl parses : or braced method bodies at src/Parser.w:2607-2614 and writes default_body into trait metadata at src/Parser.w:2649-2655. Sema collection records only method names/counts (src/SemaDecl.w:1454-1477), not callable default method symbols for implementors.
Codegen has a substantial default-method generation path: Codegen.collect_trait_info stores default bodies at src/CodegenTraits.w:48-70; generate_default_trait_method_for_impl synthesizes Type.method functions at src/CodegenTraits.w:373-430 and lowers default bodies through MIR at src/CodegenTraits.w:533-650; generate_default_trait_methods runs over impls at src/CodegenTraits.w:697-701. The bug is earlier: Sema.check_method_call falls through to unknown method '<name>' for type '<type>' when method lookup has no concrete impl method (src/SemaCheck.w:11173-11196), so default methods never become sema-visible callable methods.
Implementation plan
- Extend sema trait metadata to store each method's default body, signature, and method flags, not just names.
- During impl collection, for each trait method omitted by the impl but having a default body, register a synthetic method signature for the impl target type. Use the same symbol spelling codegen expects (
Type.method) or introduce an explicit sema->codegen map. - Substitute
Self, trait type params, impl trait args, and associated types in default method signatures and bodies at sema time enough for type checking. - Ensure explicit impl methods override defaults and duplicate explicit/default registrations do not trigger duplicate impl errors.
- Update codegen to consume the sema-registered synthetic symbols rather than rediscovering defaults independently where possible. At minimum, keep symbol names aligned.
- Add diagnostics for invalid default bodies at the trait definition or first instantiation, not as unknown method calls.
Test plan
Add test/spec/spec_ss11_6_trait_default_methods.w:
- trait with a default method
- impl omits the method
- call resolves and runs default body
- explicit impl method overrides default
- generic trait default method substitutes type args
- default method calls another required trait method on
self
Add compile-error cases:
- default body references a method not required/provided
- invalid return type in default body
- generic/default interaction that should fail at instantiation.
Acceptance criteria
- Omitted trait methods with default bodies are sema-visible on implementing types.
- Calls to default methods type-check and codegen correctly.
- Explicit impl methods override defaults.
- Default bodies participate in generic substitution and normal diagnostics.
- No silent generated stubs or placeholder functions are emitted.
with build && with build :fixpoint && with build :testpass.
Dependencies
#391 is important for robust generic default methods. #533 should add sealed/coherence tests around trait behavior.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 11.6.1.1.
Activity
ehartford commentedon Jun 13, 2026
Implemented in 74f208d. Trait default methods now register sema method signatures for omitted impl methods, validate default bodies with Self/trait-arg substitution after ordinary impl method signatures are collected, and codegen default-method MIR lowering now uses the normal memory-local scan. Added positive coverage for omitted defaults, explicit overrides, required-method calls, and generic trait defaults; added negative coverage for unknown method references and return-type mismatches in defaults. Verification passed: build, fixpoint, full test suite, and test-green.