Repository navigation
Generic methods in interfaces: one slot erased to any - #741
Merged
Merged
Conversation
`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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 toany.Before this change, the interface failed to compile: "Generic method 'map' in the interface is not allowed".
Interface declaration (
getInterfaceMethodNameAndType)any.InterfaceMethodInfo::genericSignaturekeeps the method's signature for call sites.T[],[A, B]andA | Bare an error, because their erased forms are not laid out like their instantiations.Implementer (
findErasedGenericMethod,instantiateErasedGenericFunction)anyand fills the slot.Call site (
mlirGenCallExpression,instantiateErasedGenericResultType)anyresult 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)
T(sizeof<T>()) runs withanywhen called through the interface.Tests
00interface_generic_methodown_interface_generic_method, under own, rc, gc and noneexport_interface_generic_method/import_interface_generic_method: plain,-shared, and-sharedwith JITResults
🤖 Generated with Claude Code