Repository navigation
Let a class implement and override a method with type parameters of its own - #1534
Conversation
…ne which declares the same
No class could implement an interface method which declares type parameters of its own
(interface Top: function id<T:>(T value) returns T), and no class method could override a
superclass method of that shape: only a default in the interface itself worked. checkOverride
compared the parameter and return types of the two functions as they are written, so the T of
the implementation and the T of the interface method were two unrelated type parameters, and
the class was told it must implement the method (or that its override overrides nothing). Old
generic <T> methods had the same defect.
checkOverride now pairs the functions' own type parameters by position and compares the types
with the overridden function's renamed to the overriding one's. The two must declare the same
number, of the same kind of generic (old <T> or new <T:>), with the same bounds in the same
order: a call through the overridden function only guarantees its bounds, and the override is
specialised with what that call provides. A mismatch in an override says what it should be
("takes 1 type parameters, but there are 2 type parameters here.", "Type parameter T: Show
should be T: to override ..."); a class which does not implement the interface method still
gets the existing "must implement" message. A method without type parameters no longer counts
as implementing one which declares some.
Once accepted, the Jass generic elimination left an override of such a method unspecialised:
adaptSubmethods kept the sub-method of a plain class as it was and gave the sub-method of a
generic class only its class's type arguments ("Type variable T not eliminated", "Rewrite
generics with wrong sizes"). The method's own type arguments follow its class's in the
arguments a method is specialised with, and an override declares the same ones, so they now
carry over to every sub-method after that sub-method's class arguments.
… function its class gets for it
A class which implements an interface method with a method it inherits from a class outside
the interface gets a method of its own for it, and where the class or the implementation's class
is generic, a function of its own which calls the inherited one (implementationOfItsOwn). That
function declared no type variables and called the implementation with no type arguments. Now
that a method with type parameters of its own can implement an interface method, such a
function stood for a generic implementation with nothing to specialise it: on Jass the generic
elimination gave it the class's type arguments and the call's method arguments for the class's
variables alone ("Rewrite generics with wrong sizes"), and the interpreter could not dispatch a
bound of the method's parameter ("No type argument bound for T").
The function now declares a copy of each type variable the implementation declares itself,
uses it in the parameter and return types in place of the implementation's, and passes it to
the implementation, independently of the type arguments of the implementation's owner, which are
still substituted as the class sees them. A call through the interface therefore specialises
the implementation with its own type arguments on every backend. The function depends only on
the implementation, so it serves an interface default which implements the method the same way.
The default which another interface of the class gives the method beats the inherited one
(defaultOf), but sameParameterTypes compared the parameter types as written, so a default with
its own type parameters never matched and the inherited method ran instead. It now compares them
with the default's type parameters renamed to the abstract method's.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8aa65c7a7c
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…rapper of an override
Where an old-generic interface is bound to a type which needs index conversions (I<real>), the
override of its method is reached through a wrapper (OverrideUtils.addOverride) which converts the
indices the interface passes. The wrapper copied the interface method's parameters, declared no
type variables and called the implementation with no type arguments. For a method with a type
parameter of its own (function f<T:>(X x, T t) returns T), which a class can now implement, the
wrapper's parameters kept the interface method's type variable while the wrapper declared none,
and the Jass generic elimination left it in place ("Type variable T not eliminated").
The wrapper now declares a copy of each type variable the interface method declares itself, uses
the copies in its parameter and return types and passes them to the implementation, as the
function a class gets for an inherited implementation does.
…ters could override it
A class which inherits the implementation of an interface method takes the default another of
its interfaces gives that method instead (defaultOf). sameParameterTypes decided that by renaming
the default's own type parameters to the abstract method's and comparing the parameter types, but
never compared the type parameters themselves. A default with an old generic <U> stood for a new
generic <T:> method, and a default asking for a bound <U: Show> for a method without it: a call
through the abstract interface ran the default instead of the inherited method, and on the
interpreter could not dispatch the bound the call never provided ("No type class instance bound
for show").
The check of the functions' own type parameters (as many, of the same kind of generic, with the
same bounds) moves out of checkOverride into WurstValidator.checkOwnTypeParameters, with the
renaming in ownTypeParameterRenaming. checkOverride and sameParameterTypes both use them, so a
default stands for an abstract method exactly where it could override it.
…implementing class on Lua A call through a generic interface with a tuple as the method's own type argument specialises the method on Lua, and adaptSubmethods asks allocatesFromSpecialisedClass with the class's and the method's arguments together, which the tuple makes true although C<string> objects stay erased. The specialised implementation still lands on the erased class: specializeMethod gives it the class of the class-argument prefix, which for C<string> is the erased C, and attaches it there. The emitted script binds the dispatched slot on the erased tables (C.C_second_specialized_... = C_second_specialized, and the same for X), and the program runs on every backend. This test keeps it that way.
|
@codex review |
Brings in #1533 (a sub-interface default implements the super-interface's method). The main sources merged without conflicts: #1533's sub-interface default lookup (subInterfaceDefaults, settledBySuperclass, findClassImplementationLink) compares signatures through canOverride, which now renames the functions' own type parameters and checks their kind and bounds, and its methodOfItsOwn call for a sub-interface default goes through the bridge, which now forwards the implementation's own type parameters. defaultOf keeps the shared check in sameParameterTypes. The test files conflicted only where both branches appended tests: InterfaceTests and LuaBackendAuditTests keep master's tests followed by this branch's, and DeterministicChecks keeps both new determinism checks.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: dd236bbccd
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
With #1533 a sub-interface default implements the super-interface's method, and the class gets a method of its own for it, through the function of its own which runs the default where the class or the sub-interface is generic. The tests cover that for a method with a type parameter of its own, which the default renames: a generic and a plain sub-interface, a plain and a generic class, and calls through the super-interface with two type arguments, on every backend. Where the function runs the default of a generic sub-interface, or a generic class takes a plain sub-interface's default, it must forward the method's own type parameter: without that the Jass generic elimination fails ("Rewrite generics with wrong sizes"). Two sibling sub-interface defaults of such a method are the "must override" error, and a sub-interface method of another kind of generic is no default for it.
…e erased generic class
A plain interface method with a type parameter of its own (Top.pick<T:>), implemented by a generic
class (C<Y:> implements Top), is specialised on Lua when a call gives it a tuple (t.pick<pair>).
The interface gives only the method's argument, so adaptSubmethods could not take C's class
arguments from it and only registered a callback for the specialisations of C. An ordinary
new C<string>() leaves C erased and never specialises it, so nothing bound C's implementation to
the specialised slot, and the call failed ("attempt to call a nil value (field
'Top_pick_specialized__integer__integer_')").
Where the class's arguments are unknown but the method's own are not, the sub-method is now also
specialised for the method's arguments alone and bound to the erased class: its class's type
variables stay as they are, erased as in the class's other methods. The overrides below it get the
same, generic ones on their erased class and plain ones as ordinary specialisations. The callback
for specialised instantiations of the class stays.
…he abstract method
defaultOf lets the default of another interface beat the method a class inherits for an abstract
interface method. Its predicate, sameParameterTypes, compared the own type parameters and the
parameter types but not the return type, so a default pick<U:>(U value) returns int stood for
pick<T:>(T value) returns T: a call Abstract.pick<string> ran the default and returned an int
("Succeed function not called"). It also compared the abstract method's parameter types unbound,
as written in its interface.
defaultOf now binds the abstract method to the class and accepts a default only where
WurstValidator.canOverride does, the check of an override: its own type parameters, parameters
and return type. The validator's own-type-parameter helpers, which sameParameterTypes shared,
are private to it again.
|
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Problem
No class could implement an interface method that declares its own type parameters, with its own method or with an inherited one:
Class-to-class overrides of such methods failed the same way ("does not override anything"), for both
<T:>and old<T>. Only a default insideTopitself worked.Root causes and changes
WurstValidator.checkOverrideservesfindImplementationLink,canOverrideand the override checks inNameLinks. It compared parameter and return types as written, so the implementation'sTand the interface method'sTwere unrelated. It now pairs each function's own type parameters by position and renames the overridden function's to the overriding one's, asConfigFunctionMatcheralready does. Both must have the same count, the same generic system (<T>or<T:>) and the same bounds in the same order. There are two new override messages, in the existing style; no existing message changes.EliminateGenerics.adaptSubmethods, a sub-method of a plain class was left unspecialised ("Type variable T not eliminated"), and a sub-method of a generic class got only its class's type arguments ("Rewrite generics with wrong sizes"). The method's own type arguments now follow the class's for each sub-method, and the size check for Lua's targeted specialisation counts them too.InterfaceTranslator.implementationOfItsOwn, the function a class gets for an inherited implementation, declared no type variables and called the implementation with emptyImTypeArguments; review raised this on Let a sub-interface default implement the super-interface's method #1533. It now declares a copy of each of the implementation's own type variables (ImTranslator.copyTypeVar, which keeps the link back to the source parameter), and forwards them separately from the owner's arguments.InterfaceTranslator.sameParameterTypes, used bydefaultOf, compared parameter types without renaming, so a default with its own type parameters never won over an inherited method. It now renames first.Decisions (conservative; say if you want otherwise)
<T>cannot override a new<T:>, or the reverse.count()againstcount<T:>(). Master accepted that.Tests
InterfaceTests, 15 new, on the interpreter, Jass and Lua:intandstring;GTop<Y>andpick<T:>(T a, Y y);<T: Show>through the bridge;<T>;<T>against<T:>.LuaBackendAuditTests.implementationsWithTypeParametersOfTheirOwnBindTheDispatchedSlotchecks each class's binding of the dispatched slot (AGENTS.md §8).DeterministicChecks.ownTypeParameterDispatchIsIdenticalAcrossCompilationscompiles twice and in both unit orders, also with-inline -localOptimizations.14 of the 16 new tests fail on master. The two error tests pass there because master rejects every such program.
Windows, bundled Lua 5.3,
-PallBackends: InterfaceTests 54 (also with-PallBackends=full), GenericsTests 116, GenericsWithTypeclassesTests 128, TypeClassTests 72, ClassesTests 128, LuaBackendAuditTests 286, LuaTranslationTests 149, GenericSpecialisationMatrixTests 384, DeterministicChecks 31. The whole suite passed, 3,331 tests, including the stdlib's own tests. Two large real maps compile with no new errors or warnings, to byte-identical optimised Lua, and one also to byte-identical Jass. Not done: Linux and macOS.Follow-ups
OverrideUtils.addOverridestill declares no type variables. It is reached only when an old-generic interface needs index conversions and its method also declares type parameters of its own.Found while fixing the defects from the 19-21 Oct audit (#1533).