Skip to content

Lua: tell the instantiations of a generic class apart in instanceof - #1507

Merged
Frotty merged 6 commits into
masterfrom
lua-generic-instanceof-identity
Oct 20, 2026
Merged

Frotty merged 6 commits into
masterfrom
lua-generic-instanceof-identity

Conversation

@Frotty

@Frotty Frotty commented Oct 20, 2026

Copy link
Copy Markdown
Member

On Lua a new-generic class stays erased, so x instanceof Box<int> was true for a Box<string>, and for a Box<CB> against Box<CA>; Jass, which specialises every instantiation, answers correctly. No generic code is needed:

interface Marker
class Box<T:> implements Marker
    T item
init
    Marker s = new Box<string>()
    if not (s instanceof Box<int>)   // Lua: true
        testSuccess()

Found by the bounded-exhaustive matrix of #1505 (tag lua-instanceof-erased).

Cause

  • EliminateGenerics.transformGenericNewOnly collected the instantiations an instanceof names (collectRuntimeTypeSpecializations) only when a tuple type argument was present, and transformProgToLua did not enter the Lua specialisation for a program whose only need was a parameterised instanceof.
  • A new Box<T>() inside generic code was not treated as needing specialisation when Box's identity is observed, so the object it allocated stayed erased.

Change

  • WurstCompilerJassImpl.containsParameterizedInstanceof() also gates the Lua specialisation phase and is passed to transformGenericNewOnly(tuples, observesGenericIdentity), which now gates the instanceof collection on it.
  • EliminateGenerics.constructsClassWithObservedIdentity: a generic caller that constructs a class (or subclass) whose instantiation an instanceof names is specialised at the caller boundary, like constructsClassOwningGenericGlobals.

Only the instantiations an instanceof names get an identity of their own; every other allocation stays erased. Programs without a parameterised instanceof emit the same Lua: the 258 Lua scripts of LuaBackendAuditTests and LuaTranslationTests are byte-identical to master's.

Evidence

Seven new LuaBackendAuditTests cases (run on Jass too), each red on Lua before the fix: the program above, class arguments, a positive check, a subclass, and checks through generic code. They use executeProgOnlyAfterTransforms() because the IM interpreter ignores the type arguments of instanceof on untranslated generic IM (EvaluateExpr.eval(ImInstanceof)), a separate defect not fixed here.

Passing locally: LuaBackendAuditTests 250, LuaTranslationTests 144, TypeClassTests 72, GenericsWithTypeclassesTests 115, FieldIterationTests 43, GenericStoreGeneratedTests 48, GenericStoreSpecializationTests 21, DeterministicChecks 27, ClassesTests 128.

Not fixed yet (cross-checked against #1505's matrix)

With this change applied to #1505, the INSTANCEOF_ cases change as follows; none that passed before fails:

  • 14 listed cases now pass.
  • 21 still fail as listed: the reader runs erased through base-class, interface, destroy or closure dispatch.
  • 11 change symptom ("expected 3 but got 2"): the writer runs erased through such dispatch, so its object stays erased.
  • 5 closure-site cases now stop with the existing lua-closure-receiver compile error ("Could not adapt receiver") instead of compiling with wrong results.

These remaining failures are the erased-dispatch defect, which needs its own fix. Whichever of this PR and #1505 merges second updates genericSpecialisationMatrix-knownFailures.txt (remove the 14, update the 16 symptoms).

Tested on Windows with the bundled Lua 5.3; Linux runs in PR CI, macOS only on master. The change is IM logic with no host dependence.

On Lua a new-generic class stays erased, so x instanceof Box<int> was true
for a Box<string>, and for a Box<CB> against Box<CA>. Only tuple programs
gave an observed instantiation an identity of its own, because the
collection of instanceof targets ran only when a tuple type argument was
present.

Collect the instantiations an instanceof names whenever one does, and
enter the Lua specialisation for such programs. A generic function or
method which constructs a class with an observed instantiation is now
specialised at its callers, so the object it allocates carries the
identity the check compares. Instantiations no instanceof names stay
erased, and programs without a parameterised instanceof emit the same Lua.
@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:33:31.758841Z bc746b7 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: 4c06c6bd85

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

An object allocated from the class an instanceof observes, such as a
Box<int>, was still called through the virtual methods of its interface
and base class, but the copy's methods were bound to those roots only for
tuple and construction-only specialisations. The call found no slot on the
specialised class and failed at run time.

Bind the copied methods of such a class like a tuple specialisation's, also
for a class specialised before an instanceof named its instantiation. The
classes are walked in program order there, because binding appends to the
submethod lists whose order names the slots.
Fourteen instanceof cases now pass and leave the list. Five closure cases
now stop at the closure receiver defect and take its tag. In eleven the
check tells the instantiations apart but the object was allocated by an
erased writer ("expected 3 but got 2"); that is still lua-instanceof-erased,
which the matrix now recognises. The backlog entry describes what remains:
a generic class reached through a non-generic interface, base class or
destroy.
@Frotty

Frotty commented Oct 20, 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

if (runtimeTypeSpecializations.put(original, generics, true) == null) {

P1 Badge Preserve identity for allocations behind virtual dispatch

When new Box<T>() is reached through an interface, base-class, destructor, or similar virtual-dispatch path, that writer can remain erased while this code gives the instanceof Box<T> target a specialized identity. A real Box<int> then fails instanceof Box<int> on Lua even though it previously passed; the newly checked-in matrix baseline confirms this regression with eleven expected 3 but got 2 cases in genericSpecialisationMatrix-knownFailures.txt:55-82. Ensure those allocation paths are redirected before splitting the target identity, or retain the erased check until they can be redirected.

AGENTS.md reference: AGENTS.md:L318-L322

ℹ️ 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 construction in a generic body which stays erased, such as a method
reached through a non-generic interface, allocates the erased class for
every instantiation. A check of the specialised class then rejected such an
object even when it was of the checked instantiation, which an erased check
had accepted.

Split a class's identity only when no reachable erased construction makes an
object of it or of a subclass; otherwise its checks go back to the erased
class. Programs with tuple type arguments keep the split they had. The eleven
matrix cases which showed this return to their listed symptom, so the
matrix's check of that symptom is reverted.
@Frotty

Frotty commented Oct 20, 2026

Copy link
Copy Markdown
Member Author

Round-2 P1 (identity split while an allocation stays erased) addressed in e777b6b: after specialisation, keepErasedChecksWhereAnAllocationStaysErased finds every reachable allocation whose type arguments still contain a type variable; that class and its superclasses keep master's erased check for every instanceof naming them. genericInstanceofAcceptsAnObjectAllocatedByErasedCode failed on Lua at fdc7339 and passes now. The eleven matrix cases are back to master's listed symptom; the tier is 233 pass / 151 listed / 0 unlisted, and no case passing on master fails.

@Frotty

Frotty commented Oct 20, 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: e777b6b7bf

ℹ️ 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 closure in a generic class was specialised on construction when its body
checked instanceof Box<T> or constructed an observed Box<T>. Specialising it
leaves its captured receiver typed by the erased class, the closure receiver
defect, so programs which compiled before were rejected.

An instanceof identity is no longer a reason to specialise a closure, except
in programs with tuple type arguments, which keep what they counted before.
The closure stays erased, its constructions keep the erased check, and the
five matrix cases return to their listed symptom.
@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. 👍

Reviewed commit: bc746b7a7b

ℹ️ 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 681865f into master Oct 20, 2026
2 checks passed
Frotty added a commit that referenced this pull request Oct 21, 2026
…ure (#1515)

* Lua: tell the instantiations apart in an instanceof written in a closure

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.

* Compare instantiations in the interpreter's instanceof as the specialisation 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.

* Check every path to a generic supertype in the interpreter's instanceof

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 deleted the lua-generic-instanceof-identity 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