Repository navigation
Lua: give a static inner class its generic owner's instantiation - #1509
Conversation
A static inner class of a new-generic class captures the owner's type parameters (Inner becomes Inner<T> in the IM), but the IM translation of a type naming it gave only the class's own type arguments and dropped the owner's, which the frontend keeps in the type's captured binding. A call on such an object therefore carried no instantiation. On Lua the inner class's reader of an owner static kept the erased store, which nothing writes once the constructor is specialised (Outer.t read nil), and typing the call crashed in TypeSubst when the return type named the captured variable and nothing was inlined. Jass got by on a by-name fill in EliminateGenerics, and a local holding the inner object crashed there too. WurstTypeClassOrInterface.imTranslateType now adds the captured owner parameters: the captured binding where there is one, the parameter itself in the owner or the inner class, as a constructor call already does. EliminateGenerics maps an inner class's instantiation onto the owner whose parameters it captured (matched by source parameter), so a specialised reader reaches the owner's specialised static, and rewrites the statics in a capturing class's copies as it does for the owner's. genericClassWithStaticInnerClass and genericModuleThistypeSmall now run on Lua as well; new regressions cover two instantiations, the inlined module form, locals holding the inner object, and unit order.
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: 37c0defd5d
ℹ️ 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".
A static of Base read in a static inner class of Outer<T> extends Base<T> kept the erased store on Lua: adaptGenericsToOwner compared Base's type parameters with those the inner class captured from Outer, found none, and gave up. The IM interpreter keyed the read by the inner class's own type and found the static unset. Both now take the inner class's instantiation to the one of the owner whose parameters it captured (ImTranslator.capturedOwnerType, Outer<T> with the inner class's T), then adapt that through the superclass edges, so the direct owner and its superclasses go one way. The test harness gives its interpreter run before the Jass transformation what the translation recorded, as a compile-time run gets it.
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! 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". |
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. 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". |
A static inner class captures the type parameters of the class it is written in (#1509), but only that class's own. In Outer<T:> containing static class Mid containing static class Leaf, Mid has no parameters of its own, so Leaf captured nothing and its reader of Outer's `static T last` kept Outer's T: Jass failed with "Type variable T not eliminated", and Lua read the erased store of Outer.last, which has specialised copies. A static inner class now captures the owner's own parameters followed by those the owner captured itself when it is a static inner class too, the order of the owner's type variables. Leaf becomes Leaf<T'>, ImTranslator.capturedOwnerType gives Mid<T'>, and EliminateGenerics and the interpreter follow Leaf to Mid to Outer as they already follow one level.
…nested owners (#1529) * Type a static inner class's members in that class's context A static inner class of a new-generic class names the owner's type parameters it captured by its own variables (Outer<T>.Reader is Reader<T'>). The IM translation creates a member's signature on demand, in the type variable context of whatever code asks for it first. When the owner's code asked first, as a closure in a method of Outer calling reader().read(), or `new Holder<Reader>(new Reader())` followed by h.v.read(), Reader.read, its constructor function and the method's class type were made with Outer's T instead of Reader's T'. Nothing binds Outer's T in an instantiation of Reader, so Jass failed with "Type variable T not eliminated" on the reader's return type. Function skeletons, method class types and the types of class fields and statics are now translated in the context of the class declaring them (ImTranslator.inDeclarationContext): the captured variables of a static inner class, then the declaration's own, with the requester's overrides set aside. A closure's skeleton is an expression and is still typed where it is written. * Capture a generic owner's parameters through nested static classes A static inner class captures the type parameters of the class it is written in (#1509), but only that class's own. In Outer<T:> containing static class Mid containing static class Leaf, Mid has no parameters of its own, so Leaf captured nothing and its reader of Outer's `static T last` kept Outer's T: Jass failed with "Type variable T not eliminated", and Lua read the erased store of Outer.last, which has specialised copies. A static inner class now captures the owner's own parameters followed by those the owner captured itself when it is a static inner class too, the order of the owner's type variables. Leaf becomes Leaf<T'>, ImTranslator.capturedOwnerType gives Mid<T'>, and EliminateGenerics and the interpreter follow Leaf to Mid to Outer as they already follow one level. * Check the IM signatures of a static inner class's members The three static inner class tests only ran their programs, so a wrong type variable in a signature could hide behind inlining or specialisation. A test now translates each program to the IM, which both backends start from, and checks that no member or constructor signature of the inner class names a type variable of a class it is nested in, and that read returns one of the inner class's own. Without the change to ImTranslator it fails on Reader_read's class type naming Outer's T.
A generic class's static was lost on Lua when only a static inner class read it. In
class Outer<T:>withstatic T t, the specialised constructor wrote the specialised copy oft, whileInner.next()read the erasedOuter_t, which stayed nil. The module form (static thistype tin a module used byclass A<T:>) lostt = thisthe same way with-inline -localOptimizations, and without them the Lua translation crashed inTypeSubst.resolve("Could not find replacement for T").GenericsWithTypeclassesTests.genericClassWithStaticInnerClassandgenericModuleThistypeSmallpassed on the interpreter and Jass only (BACKLOG.md).Cause
One cause, earlier than
EliminateGenerics: the IM translator gives a static inner class its owner's type parameters (InnerbecomesInner<T>), butWurstTypeClassOrInterface.imTranslateTypepassed only the class's own type arguments and dropped the owner's, which the frontend keeps in the type's captured binding.Outer.iterator()returned a bareInner, so.next()carried no instantiation and kept reading the erased static.ImAttrType.getType(ImMethodCall)substituted the return typeA<T>against anIteratorreceiver without type arguments.EliminateGenerics.adaptGenericsToOwnerfollowed only superclass links, so even a specialisedInnercould not reachOuter's statics.Change
imTranslateTypeappends the owner's captured parameters (newImTranslator.capturedOwnerTypeParameters), from the captured binding or, inside the owner or the inner class, the parameter itself, as constructor calls already pass.EliminateGenericsmaps an inner class's instantiation onto its owner by source type parameter (capturedOwnerGenerics) and rewrites the owner's statics in the inner class's specialised copies.Only classes which captured an owner's type parameters are affected: the 2,170 Jass and Lua scripts emitted by ClassesTests, GenericsTests, InterpreterTests, OptimizerTests, LuaTranslationTests, LuaBackendAuditTests, LuaKeyedMapTests, LuaTypecastingTests, LuaHashtableTests and LuaCodeListTests (many with the standard library) are byte-identical to master's apart from the compiler-version header.
Evidence
-inline -localOptimizations); a test with the inner object in a local, which also crashed Jass before; a unit-order determinism case in DeterministicChecks.lua-static-erasedcases are erased-dispatch paths, a different cause); the 384-case tier is unchanged.BACKLOG.md: the entry is removed; two follow-ups are added (a tuple instantiation's constructor write to a generic static is still lost on Lua, because
specializeFunctionleavesTas the parameter type; the Jass-sidefillMissingTypeArgsFromScopematches type variables by name and is no longer reached by these classes).Tested on Windows with the bundled Lua 5.3; Linux runs in PR CI, macOS only on master. No host-dependent code.