Skip to content

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

Merged
Frotty merged 3 commits into
lua-closure-instanceof-identityfrom
lua-nested-closure-generics
Oct 21, 2026
Merged

Frotty merged 3 commits into
lua-closure-instanceof-identityfrom
lua-nested-closure-generics

Conversation

@Frotty

@Frotty Frotty commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

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

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.
@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:25:13.350515Z db4aba9 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.

… into lua-nested-closure-generics

# Conflicts:
#	de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/LuaBackendAuditTests.java
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.
@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. You're on a roll.

Reviewed commit: db4aba92af

ℹ️ 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 359a92c into lua-closure-instanceof-identity Oct 21, 2026
2 checks passed
@Frotty
Frotty deleted the lua-nested-closure-generics branch October 21, 2026 09:45
Frotty added a commit that referenced this pull request Oct 21, 2026
…ters (#1516) (#1518)

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