Skip to content

Give a closure written inside another closure its owner's type parameters - #1518

Merged
Frotty merged 1 commit into
masterfrom
lua-nested-closure-generics-to-master
Oct 21, 2026
Merged

Frotty merged 1 commit into
masterfrom
lua-nested-closure-generics-to-master

Conversation

@Frotty

@Frotty Frotty commented Oct 21, 2026

Copy link
Copy Markdown
Member

Re-lands #1516 on master. #1516 was stacked on #1515's branch and was merged into lua-closure-instanceof-identity after #1515 had already been squash-merged, so its change never reached master. This is its squash commit 359a92c cherry-picked onto master; the change set is identical, with no conflicts.

Checked locally on Windows: LuaBackendAuditTests 280, TypeClassTests 72, ClosureTests 68, GenericSpecialisationMatrixTests 384, all passing.


Original description of #1516:

Stacked on #1515. Merge this PR into that branch first, then #1515 into master. If #1515 lands first, this needs a roll-up PR to master.

When a closure is written inside another closure, in a generic class or function, EliminateGenerics threw "Rewrite generics with wrong sizes" on both Jass and Lua. It happened whenever either closure used the owner's T, even for a plain field read:

class G<T:>
    T val
    function getter() returns Get<T>
        MkGet<T> outer = () -> begin
            Get<T> inner = () -> val
            return inner
        end
        return outer.mk()

Cause

The inner closure is translated while the enclosing closure's class is still being created, before that class has its captured type variables. So the inner closure captured the enclosing this as MkGet_G_Test⟪⟫, with no type arguments, and the enclosing closure allocated the inner one without type arguments too.

Changes

  • ClosureTranslator: a closure written inside another one no longer captures type variables itself; the outermost closure does it for the whole nest.
    • Every class in the nest captures the same type variables, in the same order: all the ones any closure in the nest uses.
    • Each class refers to the other classes of the nest with its own variables.
    • A closure with no closure inside it captures exactly what it captured before.
  • EliminateGenerics: removes the isInsideAnotherClosure exclusion from classNeedsIdentity. Its javadoc described this crash as the reason for leaving such closures out. On Lua, an instanceof Box<T> in an inner closure now tells the instantiations apart.

Tests

  • New in LuaBackendAuditTests, each run on Jass, the interpreter and Lua. Each failed before this change: the first four with the crash, the last with an interpreter error.
    • genericFieldReadInAClosureInsideAnotherClosure
    • genericConstructionInAClosureInsideAnotherClosure
    • genericInstanceofInAClosureInsideAnotherClosure
    • boundDispatchInAClosureInsideAnotherClosure
    • genericStaticInAClosureTwoClosuresDeep (three levels of nesting, reading a per-instantiation static)
  • TypeClassTests.nestedClosuresInsideBoundedGenericAreRejectedForLua pinned the old rejection and asked to become a success case once nested closures work. It is now nestedClosuresInsideBoundedGenericDispatchOnTheBound, and passes on Jass, the interpreter and Lua.
  • Matrix: GenericSpecialisationMatrixTests passes 384 of 384 cases, run with WURST_GENERIC_MATRIX_KEEP_GOING.
  • Ran locally on Windows: LuaBackendAuditTests, FieldIterationTests, ClosureTests, GenericsTests, GenericsWithTypeclassesTests, GenericsModuleTests, GenericStoreSpecializationTests, GenericStoreGeneratedTests, InterpreterTests, CompiletimeTests, LuaTranslationTests, LuaKeyedMapTests, DeterministicChecks, NewFeatureTests, ClassesTests, LuaExemptionTests, LuaNameCompressionTests, LuaCodeListTests.
  • Not run: the full suite, -PallBackends, and Linux or macOS. The change has no host-specific code.
  • Large map: a large Lua map (-lua -inline -localOptimizations) produces byte-identical Lua with and without this change.
  • Backlog: the backlog entry for this crash is removed.

…ters (#1516)

* Give a closure written inside another closure its owner's type parameters

A closure written in another closure, in a generic class or function, made
EliminateGenerics throw "Rewrite generics with wrong sizes" on Jass and Lua
whenever either closure used the owner's T, even for a plain field read.
The inner closure is translated while the enclosing closure's class is
being created, before that class has its captured type variables: the
inner closure captured the enclosing this as a class type without type
arguments, and the enclosing closure allocated the inner one without them.

ClosureTranslator now leaves the type variables of a closure written in
another one to the outermost closure, which captures the same variables,
all that any closure of the nest uses and in the same order, for each class
of the nest, and makes each class name the others with its own variables.
A closure with no closure inside it captures what it captured before.

With that, EliminateGenerics no longer leaves such closures out of
classNeedsIdentity (isInsideAnotherClosure), so on Lua an instanceof
Box<T> in an inner closure tells the instantiations apart too.

* Turn the test of a rejected nested closure into a success case

TypeClassTests pinned that a closure written inside another one in a bounded
generic function was rejected on Lua ("could not be resolved for the Lua
target"), and asked to become a success case once that works. It works now:
the program dispatches on the bound on Jass, the interpreter and Lua.
@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-21T09:53:00.879903Z be0e334 PR opened
ℹ️ 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 merged commit 88386cf into master Oct 21, 2026
2 checks passed
@Frotty
Frotty deleted the lua-nested-closure-generics-to-master branch October 21, 2026 10:00
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