Skip to content

Lua: give a static inner class its generic owner's instantiation - #1509

Merged
Frotty merged 3 commits into
masterfrom
lua-generic-static-inner-class
Oct 20, 2026
Merged

Frotty merged 3 commits into
masterfrom
lua-generic-static-inner-class

Conversation

@Frotty

@Frotty Frotty commented Oct 20, 2026

Copy link
Copy Markdown
Member

A generic class's static was lost on Lua when only a static inner class read it. In class Outer<T:> with static T t, the specialised constructor wrote the specialised copy of t, while Inner.next() read the erased Outer_t, which stayed nil. The module form (static thistype t in a module used by class A<T:>) lost t = this the same way with -inline -localOptimizations, and without them the Lua translation crashed in TypeSubst.resolve ("Could not find replacement for T"). GenericsWithTypeclassesTests.genericClassWithStaticInnerClass and genericModuleThistypeSmall passed 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 (Inner becomes Inner<T>), but WurstTypeClassOrInterface.imTranslateType passed only the class's own type arguments and dropped the owner's, which the frontend keeps in the type's captured binding.

  • Split store: Outer.iterator() returned a bare Inner, so .next() carried no instantiation and kept reading the erased static.
  • Crash: ImAttrType.getType(ImMethodCall) substituted the return type A<T> against an Iterator receiver without type arguments.
  • A second gap: EliminateGenerics.adaptGenericsToOwner followed only superclass links, so even a specialised Inner could not reach Outer's statics.

Change

  • imTranslateType appends the owner's captured parameters (new ImTranslator.capturedOwnerTypeParameters), from the captured binding or, inside the owner or the inner class, the parameter itself, as constructor calls already pass.
  • EliminateGenerics maps 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.
  • No runtime guard, helper call or name matching.

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

  • Both named tests now run on Lua as well; two Lua regressions in LuaBackendAuditTests (two instantiations; the module form with -inline -localOptimizations); a test with the inner object in a local, which also crashed Jass before; a unit-order determinism case in DeterministicChecks.
  • Passing locally: GenericsWithTypeclassesTests 116, LuaBackendAuditTests 245, GenericStoreGeneratedTests 48, GenericStoreSpecializationTests 21, TypeClassTests 72, FieldIterationTests 43, DeterministicChecks 28, LuaTranslationTests 144, GenericsTests 73, GenericsModuleTests 6, ModuleTests 37, ClassesTests 128, BugTests 132, ClosureTests 65, NewFeatureTests 42.
  • Cross-checked against Test Lua's targeted generic specialisation against Jass, bounded-exhaustively #1505's matrix: no listed case changes (the lua-static-erased cases 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 specializeFunction leaves T as the parameter type; the Jass-side fillMissingTypeArgsFromScope matches 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.

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

chatgpt-codex-connector Bot commented Oct 20, 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-20T17:19:07.044450Z 190421a 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.

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

Frotty commented Oct 20, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

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

@Frotty

Frotty commented Oct 20, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 190421a089

ℹ️ 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 8cbea90 into master Oct 20, 2026
2 checks passed
Frotty added a commit that referenced this pull request Oct 20, 2026
 made stale, exempt the Jass halves of the Reflection difference tests
Frotty added a commit that referenced this pull request Oct 21, 2026
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.
Frotty added a commit that referenced this pull request Oct 21, 2026
…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.
@Frotty
Frotty deleted the lua-generic-static-inner-class 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