Skip to content

Lua: tell the instantiations apart in an instanceof written in a closure - #1515

Merged
Frotty merged 5 commits into
masterfrom
lua-closure-instanceof-identity
Oct 21, 2026
Merged

Frotty merged 5 commits into
masterfrom
lua-closure-instanceof-identity

Conversation

@Frotty

@Frotty Frotty commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

An instanceof Box<T> written in a closure inside a method of a new-generic class G<T:> accepted every Box instantiation on Lua. Jass was right. This was the lua-instanceof-erased backlog tag, the last one the generic specialisation matrix listed.

class G<T:>
    function pred() returns Pred
        return (Marker m) -> m instanceof Box<T>
init
    let p = new G<int>().pred()
    if not p.test(new Box<string>())   // Lua: false before this change
        testSuccess()

Changes

  • EliminateGenerics.classNeedsIdentity: Lua: tell the instantiations of a generic class apart in instanceof #1507 stopped an instanceof from giving a closure an identity of its own, because specialising the closure left its captured receiver typed by the erased class. Lua: allocate an instantiation whose members need its type argument from its specialised class #1512 fixed that receiver, so the exclusion (identityIgnored) is removed. An instanceof of the type parameter now counts for a closure the same as for any other member that needs its type argument.
  • Which classes an instanceof observes is collected once, before specialisation starts (classesWithObservedIdentity). This includes checks still written with a type variable. A closure that constructs the Box<T> which another closure checks used to stay erased: Box<int> only became observed in the second round, but classNeedsIdentity keeps the answer it gave in the first round. Every object it made was then of the erased class, so keepErasedChecksWhereAnAllocationStaysErased put every check of Box back to the erased class.
  • IM interpreter: instanceof compared only the class. In compile-time code and @Test functions this took a Box<string> for a Box<int>. It now also compares the type arguments of the instantiation. If an instantiation cannot be resolved there, it throws an explicit InterpreterException. It compares instantiations the way the specialisation does (GenericTypes, which ignores type-class bindings), and it checks every path to a generic supertype: a class can reach G<int> and G<string> through two interfaces. The instanceof audit tests used to run the interpreter only after the transformations, and that is how this went unnoticed. They now run it on the generic program too.

Tests

  • New in LuaBackendAuditTests, with Jass and the interpreter as the oracle:
    • genericInstanceofInAClosureRejectsAnotherInstantiation
    • genericInstanceofOfAnObjectAnotherClosureConstructs
    • genericInstanceofThroughANonGenericInterface
    • genericInstanceofOfANestedBoundTypeArgument and genericInstanceofThroughTwoPathsToOneGenericInterface, from the Codex review. The second failed in the interpreter before its fix. The first did not fail before: no divergence in bindings was found, so it guards the GenericTypes comparison.
  • The first test failed on master on Lua. Without the interpreter change, the existing instanceof tests fail once they run the generic program.
  • The nine INSTANCEOF_CLOSURE_* cases are removed from genericSpecialisationMatrix-knownFailures.txt. With WURST_GENERIC_MATRIX_KEEP_GOING, the pull-request tier passes 384 of 384 cases.
  • Ran locally on Windows: GenericSpecialisationMatrixTests, LuaBackendAuditTests, FieldIterationTests, ClosureTests, GenericsTests, GenericsWithTypeclassesTests, GenericsModuleTests, GenericStoreSpecializationTests, GenericStoreGeneratedTests, InterpreterTests, CompiletimeTests, LuaTranslationTests, LuaKeyedMapTests, DeterministicChecks, NewFeatureTests, ClassesTests, LuaExemptionTests.
  • Not run locally: the full suite, -PallBackends, and the full matrix product. Linux and macOS are not verified locally either. The change has no host-specific code.
  • A large Lua map without parameterised instanceof (-lua -inline -localOptimizations) produces byte-identical Lua with and without this change. That is expected: the changed paths run only when a program contains a parameterised instanceof.

Backlog

  • The lua-instanceof-erased entry is removed.
  • New entry for a crash found along the way, which master has too, on Jass and Lua: a closure inside another closure that uses its owner's T fails in EliminateGenerics with "Rewrite generics with wrong sizes".

An instanceof Box<T> in a closure written in a method of a generic class
accepted every Box on Lua. #1507 kept closures erased for an instanceof,
because specialising one left its captured receiver typed by the erased
class; #1512 fixed that receiver, so the exclusion is lifted: an instanceof
of the type parameter now counts towards a closure's identity like any
other member which needs its type argument.

A closure which constructs the Box<T> another closure checks still stayed
erased. Box<int> is observed only once the checking closure is
specialised, in the second round, but classNeedsIdentity keeps its first
answer, given in the first. Whether an instanceof observes a class is now
collected once, before the specialisation, from every parameterised
instanceof, including one still written with a type variable.

The IM interpreter compared only the class in an instanceof, so it took a
Box<string> for a Box<int> in compile-time code and @test functions. It
now compares the type arguments of the instantiation too. The audit tests
for instanceof identity run the interpreter on the generic program again.

Removes the nine INSTANCEOF_CLOSURE cases from the matrix's known failures
and the lua-instanceof-erased backlog entry. Records a crash found on the
way: a closure inside another closure which uses its owner's T fails in
EliminateGenerics on Jass 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-21T08:59:33.141260Z f0f6c07 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: 3fd442a30a

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

…isation does

Type-class bindings are dispatch metadata, which GenericTypes ignores at every
nesting level when it tells specialisations apart; the interpreter now compares
an object's instantiation with the checked one the same way, instead of through
equalsType, which compares nested bindings. Adds a check of a type argument with
a bound inside it, built directly and through generic functions.
@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: 3e5f5791bb

ℹ️ 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 class which reaches one generic interface through two others, as G<int> and
as G<string>, is an instance of both, as on Jass and Lua. The interpreter
compared the checked instantiation with the first path to the interface only,
so one of the two checks was false. It now collects the instantiations of
every path.
@Frotty

Frotty commented Oct 21, 2026

Copy link
Copy Markdown
Member Author

@codex review

@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. 🚀

Reviewed commit: f0f6c0784e

ℹ️ 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 5d29653 into master Oct 21, 2026
2 checks passed
Frotty added a commit that referenced this pull request Oct 21, 2026
…lasses in the interpreter (#1524)

* Bind an inherited generic method's type parameters through the superclasses in the interpreter

A method a class inherits from a generic superclass (class Sub extends G<int>)
ran in the IM interpreter with G's T unbound. The interpreter bound the type
variables of the method's class positionally from the receiver's own type
arguments, so a Sub, which has none, left T open. A new Box<T>() in such a
method was then recorded as Box<T>: since #1515 its instanceof threw "Cannot
decide whether Box<T> is a Box<integer>", and before that it took the object
for a Box<string> too. A static of G read in such a method was keyed apart
from the one G<int> writes, so it read as uninitialised. Jass and Lua were
right, because their specialisation follows the superclass edge, as
ImAttrType does for IM types since #1510.

The interpreter now takes the receiver's type as an instance of the method's
class through the superclass chain (ProgramState.adaptToSuperclass) before it
binds that class's type variables. Adds tests for an instanceof of an object
made in an inherited method, through one and two generic superclasses and in
a default method of a generic interface, and for a static per instantiation
read in an inherited method, on Jass, Lua and the interpreter.

* Test an inherited method of an old generic class too

The interpreter's binding of an inherited method's type parameters through the superclasses serves both generic systems. An old generic's parameter is erased, so cover a value returned by a method inherited through one and two old-generic superclasses, in the interpreter and on both targets.
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