Skip to content

Support generic extend blocks (§9.5) #405

Closed

Description

@ehartford

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

  1. Update Parser.parse_impl_block so extend Name[...]: is parsed as an extension target generic instantiation, while impl Trait[Args] for Type remains trait-impl syntax. The extend keyword gives enough context to disambiguate.
  2. 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_node instead of encoding it into a mangled string.
  3. In sema declaration collection, register generic extension methods against the generic base symbol plus type parameter list. Avoid treating Vec[i32] and Vec[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.
  4. 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.
  5. Ensure emitted method names remain deterministic for fixpoint. Do not use pointer addresses or unordered map iteration when deriving monomorphized names.
  6. 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 for i32.
  • define extend Vec[T]: fn is_empty(self: &Vec[T]) -> bool mirroring the spec once Vec generic lookup supports it.

Add negative coverage:

  • extend Vec[T]: with an unbound T in 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 requiring for.
  • 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 :test pass.

Dependencies

#391 for correct generic instantiation identity and monomorphization.

Activity

  1. ehartford commented on Jun 13, 2026

    @ehartford
    ContributorAuthor

    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 as Vec[T]. Added positive coverage for §9.5 extend Box[T] and extend 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.

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