Repository navigation
Type a static inner class's members in that class's context, through nested owners - #1529
Conversation
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.
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.
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. |
355ed2f to
5bcca10
Compare
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5bcca105eb
ℹ️ 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".
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.
|
@codex review |
…ass-generics # Conflicts: # de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/GenericsTests.java
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! 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
Three shapes of a static inner class of a generic class failed on Jass with "Type variable T not eliminated". Lua passed (a) and (b), and failed (c).
Outer<T:>, a closure in a method doesReader r = reader(); return r.read(), wherestatic class Readerreads Outer'sstatic T last.Outer<T:>,let h = new Holder<Reader>(new Reader())thenreturn h.v.read().Outer<T:>containsstatic class Mid, which containsstatic class Leaf, whoseread()returns Outer'sstatic T last. Lua failed the unit-test check "Leaf_read uses the erased generic static Outer_last".There are two root causes, both in
ImTranslator, so there are two commits.1, for (a) and (b). A member's IM signature is made when code first asks for it, and it used the type-variable context of whoever asked. In (a) and (b), Outer's code asked for Reader's members before Reader was translated, so
Reader_read,new_Readerand the method's class type got Outer'sTinstead of theTReader captured. The IM showedReader_read(Reader<T340>) returns T340insideclass Reader<T600>. Nothing binds Outer'sTin an instantiation of Reader.2, for (c). #1509 captured only the direct owner's own type parameters.
Midhas none, soLeafcaptured nothing and kept Outer'sT.Change
inDeclarationContexttranslates a member's declaration in the context of the class that declares it: the inner class's captured variables plus the declaration's own, with the requester's context set aside. It is used for function skeletons ingetFuncFor, the method class type ingetMethodFor, and the types of class fields and statics ingetVarFor. A closure is an expression and is still typed where it is written. The owner lookup moves, unchanged, intocapturingOwnerOf.typeParametersCapturedFromcaptures the owner's own parameters and then, when the owner is itself a static inner class, those it captured. The existing recursion inEliminateGenericsandProgramStatefollowsLeaftoMidtoOuter; no backend changes.Tests
Three tests in
GenericsTests, run in the interpreter, on Jass and on Lua. Each fails without its fix: (a) and (b) without the first commit, (c) without the second.Windows, bundled Lua 5.3:
-PallBackends, these also passed: GenericsTests 84, GenericsWithTypeclassesTests 119, ClosureTests 68, ClassesTests 128, LuaBackendAuditTests 285, LuaTranslationTests 149, GenericSpecialisationMatrixTests 384, DeterministicChecks 29.-PallBackends: GenericsTests 89, ClosureTests 68, GenericsWithTypeclassesTests 119, LuaBackendAuditTests 285, InterpreterTests 29, CompiletimeTests 56.Not done: Linux and macOS.
Found by a defect audit of the 19-21 Oct merges.