Skip to content

Type a method call on an inherited generic method with the superclass's type arguments - #1510

Merged
Frotty merged 2 commits into
masterfrom
tuple-elimination-generic-crash
Oct 20, 2026
Merged

Frotty merged 2 commits into
masterfrom
tuple-elimination-generic-crash

Conversation

@Frotty

@Frotty Frotty commented Oct 20, 2026

Copy link
Copy Markdown
Member

EliminateTuples crashed ("//// Before ...") on a field of the tuple which a subclass's inherited generic method returns, on both Jass and Lua; a conditional over such a call failed Jass translation with "Type variable T not eliminated":

tuple pa(int x, int y)
class G<T:>
    static T sv
    function w(T v)
        sv = v
    function r() returns T
        return sv
class Sub extends G<pa>
init
    let g = new Sub()
    g.w(pa(1, 2))
    if g.r().x == 1   // crashed; `let p = g.r()` first compiled
        testSuccess()

Found by the bounded-exhaustive matrix of #1505 (tag lua-tuple-elimination-crash).

Cause

ImAttrType.getType(ImMethodCall) filled in only the type arguments of the receiver's own class. For class Sub extends G<pa> the call g.r() therefore had type T (G's type variable), and ExprTranslation built its temporaries from that type (temp_tuple for g.r().x, cond_result for b ? g.r() : ...), leaving a local of type T that EliminateTuples and the Jass translation could not handle.

Change

The IM type of a method call now follows the receiver's class up its superclass edges to the method's class and substitutes that class's type arguments. A receiver type without its class's type arguments keeps the previous behaviour.

Evidence

  • Three new GenericsWithTypeclassesTests cases, on both targets (one with the optimiser), each failing before the fix.
  • Output: the Jass and Lua emitted by GenericsWithTypeclassesTests, TupleTests, LuaBackendAuditTests, LuaTranslationTests, OptimizerTests and DeterministicChecks (1,830 files) are byte-identical to master's apart from the compiler-version header and the im2.j debug dump.
  • Passing locally after merging master: GenericsWithTypeclassesTests 118, TupleTests 30, LuaBackendAuditTests 243, LuaTranslationTests 144, OptimizerTests 179, DeterministicChecks 27; GenericSpecialisationMatrixTests PR tier 219 pass, 165 listed, 0 unlisted.

The matrix's closure variant of this tag (STATIC_CLOSURE_wCLOSURE_rDESTROY_TUPLEx2_opt) still fails: it is a different defect, a specialised closure writing the erased static (lua-static-erased), and stays listed.

Tested on Windows with the bundled Lua 5.3; Linux runs in PR CI, macOS only on master.

…ments

The IM type of a method call substituted only the type arguments of the
receiver's own class. A method which the receiver's class inherits from a
generic superclass (class Sub extends G<pa>) therefore had G's type variable
T as its type. The translation keeps such a call's result in a temporary of
that type (g.r().x, and the branches of a conditional expression), so a local
of type T was left in a function where T is not in scope: tuple elimination
failed on the selection ("//// Before") on Jass and Lua, and the conditional
failed Jass translation ("Type variable T not eliminated").

ImAttrType now follows the receiver's type through its superclasses to the
method's class and substitutes that class's type arguments.
@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:30:17.983312Z 8d4dce0 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 87e5ea8 into master Oct 20, 2026
2 checks passed
@Frotty
Frotty deleted the tuple-elimination-generic-crash branch October 20, 2026 18:35
Frotty added a commit that referenced this pull request Oct 21, 2026
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.
Frotty added a commit that referenced this pull request Oct 21, 2026
…ents (#1526)

The IM type of a member access substituted only the type arguments of the
receiver's own class. A field which the receiver's class inherits from a
generic superclass (class Sub extends G<int>, G declaring T v) therefore had
G's type variable T as its type. The translation keeps the branches of a
conditional expression in a temporary of the first branch's type, so
b ? s.v : 5 left a local of type T in a function where T is not in scope,
and Jass translation failed with "Type variable T not eliminated". Lua and
the interpreter passed, and a tuple argument (Sub extends G<pa>) passed.

ImAttrType.getType(ImMemberAccess) now follows the receiver's type through
its superclasses to the class declaring the field and substitutes that
class's type arguments, as getType(ImMethodCall) does since #1510. This
covers the implicit receiver in a subclass method, a field two generic
superclasses up, and a generic subclass passing its own argument to a
later type parameter of the superclass.
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