Skip to content

Type a static inner class's members in that class's context, through nested owners - #1529

Merged
Frotty merged 4 commits into
masterfrom
fix-static-inner-class-generics
Oct 21, 2026
Merged

Frotty merged 4 commits into
masterfrom
fix-static-inner-class-generics

Conversation

@Frotty

@Frotty Frotty commented Oct 21, 2026

Copy link
Copy Markdown
Member

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).

  • (a) In Outer<T:>, a closure in a method does Reader r = reader(); return r.read(), where static class Reader reads Outer's static T last.
  • (b) In Outer<T:>, let h = new Holder<Reader>(new Reader()) then return h.v.read().
  • (c) Outer<T:> contains static class Mid, which contains static class Leaf, whose read() returns Outer's static 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_Reader and the method's class type got Outer's T instead of the T Reader captured. The IM showed Reader_read(Reader<T340>) returns T340 inside class Reader<T600>. Nothing binds Outer's T in an instantiation of Reader.

2, for (c). #1509 captured only the direct owner's own type parameters. Mid has none, so Leaf captured nothing and kept Outer's T.

Change

  1. inDeclarationContext translates 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 in getFuncFor, the method class type in getMethodFor, and the types of class fields and statics in getVarFor. A closure is an expression and is still typed where it is written. The owner lookup moves, unchanged, into capturingOwnerOf.
  2. typeParametersCapturedFrom captures the owner's own parameters and then, when the owner is itself a static inner class, those it captured. The existing recursion in EliminateGenerics and ProgramState follows Leaf to Mid to Outer; 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:

  • Before the rebase onto master: the whole suite passed, 3,251 tests. With -PallBackends, these also passed: GenericsTests 84, GenericsWithTypeclassesTests 119, ClosureTests 68, ClassesTests 128, LuaBackendAuditTests 285, LuaTranslationTests 149, GenericSpecialisationMatrixTests 384, DeterministicChecks 29.
  • After the rebase, with -PallBackends: GenericsTests 89, ClosureTests 68, GenericsWithTypeclassesTests 119, LuaBackendAuditTests 285, InterpreterTests 29, CompiletimeTests 56.
  • Real maps: two large real maps compile to byte-identical optimised Lua, and one also to byte-identical Jass, with and without the change.

Not done: Linux and macOS.

Found by a defect audit of the 19-21 Oct merges.

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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-21T17:07:15.520353Z 11ed8f8 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Frotty
Frotty force-pushed the fix-static-inner-class-generics branch from 355ed2f to 5bcca10 Compare October 21, 2026 16:47
@Frotty

Frotty commented Oct 21, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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.
@Frotty

Frotty commented Oct 21, 2026

Copy link
Copy Markdown
Member Author

@codex review

…ass-generics

# Conflicts:
#	de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/GenericsTests.java
@Frotty

Frotty commented Oct 21, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 11ed8f8058

ℹ️ 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".

@Frotty
Frotty merged commit 6805fe0 into master Oct 21, 2026
2 checks passed
@Frotty
Frotty deleted the fix-static-inner-class-generics branch November 25, 2026 08:54
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