Skip to content

Generic methods in interfaces: one slot erased to any - #741

Merged
ASDAlexander77 merged 1 commit into
mainfrom
generic-interface-methods
Oct 29, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
generic-interface-methods

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Generic methods in interfaces, implemented as option A of docs/generic-interface-methods-design.md: each generic method gets one vtable slot, with its type parameters erased to any.

interface Mapper { map<T>(x: T): T; }
class Id implements Mapper { map<T>(x: T): T { return x; } }
const m: Mapper = new Id();
const n: number = m.map<number>(5);   // explicit type argument
const s = m.map("ab"); s.length;      // inferred: s is a string

Before this change, the interface failed to compile: "Generic method 'map' in the interface is not allowed".

Interface declaration (getInterfaceMethodNameAndType)

  • The slot's signature binds each type parameter to any.
  • InterfaceMethodInfo::genericSignature keeps the method's signature for call sites.
  • A type parameter must be the whole type of a parameter or of the result. T[], [A, B] and A | B are an error, because their erased forms are not laid out like their instantiations.

Implementer (findErasedGenericMethod, instantiateErasedGenericFunction)

  • The class's generic method, or its nearest base's, is instantiated with any and fills the slot.
  • An instantiation made during discovery lives in the module discovery drops, so it is made again for the real module.

Call site (mlirGenCallExpression, instantiateErasedGenericResultType)

  • Type arguments come from the call, or are inferred from the arguments.
  • The call goes through the slot, and its any result goes through the checked cast to the instantiated result type.

Cross-module: an exported interface with a generic method is exported as its source (addInterfaceSourceToExport), the same way a generic interface is.

Classes (option D): a subclass generic method that overrides a base's generic method is now an error. Before, a call through the base reference silently ran the base method.

Limits (design doc §7)

  • A body that depends on the real T (sizeof<T>()) runs with any when called through the interface.
  • Under own, a class object unboxed from the result is a borrow of the box.
  • Calls through a base-class reference still do not dispatch to an override; overriding is rejected for now.

Tests

  • 00interface_generic_method
  • own_interface_generic_method, under own, rc, gc and none
  • export_interface_generic_method / import_interface_generic_method: plain, -shared, and -shared with JIT

Results

  • ctest: 6374/6374
  • DefaultLib, release jit:
    • gc 182/182
    • rc 181/182 (1 skip)
    • none 181/182 (1 skip)
    • own 171/182 (11 skips, all by design)

🤖 Generated with Claude Code

`interface Mapper { map<T>(x: T): T; }` was "Generic method 'map' in the interface is not allowed".
Now a generic interface method is one vtable slot with each type parameter erased to `any`:

- The interface binds each type parameter to `any` for the slot and keeps the method's signature.
  A type parameter must be the whole type of a parameter or the result (`T[]`, `[A, B]`, `A | B`
  erased are not laid out as their instantiations are): anything else is an error.
- A class fills the slot with its generic method (or its base's) instantiated with `any`
  (findErasedGenericMethod, instantiateErasedGenericFunction).
- A call infers the type arguments from the arguments when none are given, calls through the
  slot, and casts the `any` result to the instantiated result type.
- An exported interface with a generic method is exported as its source, so an importer keeps
  the generic signature.
- A generic class method overriding a base class's generic method is an error: a call through
  the base never reached it.

docs/generic-interface-methods-design.md §7 describes it and its limits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 7337c59 into main Oct 29, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the generic-interface-methods branch October 29, 2026 18:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant