Repository navigation
Description
Summary
Support generic extension blocks such as extend Vec[T]:, which are explicitly used by the spec and by idiomatic With container APIs. Today the parser has generic machinery, but extend Vec[T]: is misread as trait generic arguments and rejected unless followed by for.
Spec
§9.5 shows generic extension blocks directly: extend Vec[T]: fn is_empty(self: &Vec[T]) -> bool: ... (docs/with-specification.md:3374-3379). Extension methods must participate in the normal method-call surface and generic instantiation model.
Current state
examples/ecs/src/storage.w notes that the compiler does not yet support generic extend blocks. Parser support for impl-level type parameters exists before the target name (src/Parser.w:2702-2708), and optional generic target arguments are represented by parse_optional_impl_target_args (src/Parser.w:2664-2689). However, parse_impl_block treats a [ immediately after the first name as trait generic arguments and then requires for (src/Parser.w:2718-2735), so extend Vec[T]: goes down the wrong branch.
The semantic root cause is larger than parsing: generic method ownership and lookup must preserve the generic target, not collapse all Vec[T] methods into an uninstantiated Vec entry. This likely shares the TY_GENERIC_INST identity/monomorphization work tracked by #391.
Implementation plan
- Update
Parser.parse_impl_blocksoextend Name[...]:is parsed as an extension target generic instantiation, whileimpl Trait[Args] for Typeremains trait-impl syntax. Theextendkeyword gives enough context to disambiguate. - Store target generic arguments in the impl/extend metadata rather than only a target type name. If no existing AST slot can carry this cleanly, add metadata parallel to
target_type_nodeinstead of encoding it into a mangled string. - In sema declaration collection, register generic extension methods against the generic base symbol plus type parameter list. Avoid treating
Vec[i32]andVec[str]as the same concrete sema type until Fix sema generic type instantiation (TY_GENERIC_INST erasure) — prerequisite for stdlib-as-generics #391's type-identity fix is in place. - In method lookup, when resolving
xs.is_empty(), instantiate extension method parameters with the receiver's generic arguments and check the body under those substitutions. - Ensure emitted method names remain deterministic for fixpoint. Do not use pointer addresses or unordered map iteration when deriving monomorphized names.
- Phase the work if needed: parser + loud "generic extend semantic support pending" diagnostic first is acceptable only if it fails loudly; silently accepting and mischecking methods is not.
Test plan
Add test/spec/spec_ss09_5_generic_extend.w:
- define
type Box[T] { value: T },extend Box[T]: fn get(self: &Box[T]) -> T, and call it fori32. - define
extend Vec[T]: fn is_empty(self: &Vec[T]) -> boolmirroring the spec once Vec generic lookup supports it.
Add negative coverage:
extend Vec[T]:with an unboundTin a non-generic method body should produce a clear type-parameter diagnostic.- method lookup should not allow a
Vec[i32]extension method to be called on a different generic base.
Acceptance criteria
extend Vec[T]:parses as a generic extension block, not a trait impl requiringfor.- Generic extension methods typecheck with receiver type arguments substituted.
- Method lookup remains deterministic and fixpoint-stable.
- Unsupported partial states fail loudly.
with build && with build :fixpoint && with build :testpass.
Dependencies
#391 for correct generic instantiation identity and monomorphization.
Activity
ehartford commentedon Jun 13, 2026
Implemented in f121123.
extend Name[T]:now parses as a generic extension target, generic method calls preserve their qualified resolved symbol through MIR lowering, and codegen monomorphizes generic extension methods on built-in generic wrappers such asVec[T]. Added positive coverage for §9.5extend Box[T]andextend Vec[T], plus negative coverage for unbound target type parameters and wrong-base method lookup. Verification passed: build, fixpoint, full test suite, and test-green.