Skip to content

Keep the toIndex of a generic argument on Lua - #1502

Merged
Frotty merged 1 commit into
masterfrom
claude/hungry-curran-ea7b53
Oct 20, 2026
Merged

Frotty merged 1 commit into
masterfrom
claude/hungry-curran-ea7b53

Conversation

@Frotty

@Frotty Frotty commented Oct 20, 2026

Copy link
Copy Markdown
Member

Problem

On Lua, a value read from an old generic (<T>) and passed straight back into one lost its toIndex conversion while keeping its fromIndex:

Cell<bla> c = new Cell<bla>()
c.set(bla(5, 3))
c.set(c.get())

Jass cancels the pair and emits call dispatch_Cell_Cell_set(c, dispatch_Cell_Cell_get(c)). Lua emitted z, y = blaFromIndex(__wurst_ensureInt(Cell_Cell_get(c))) and then Cell_Cell_set(receiver, z, y): the tuple's two components went to a one-parameter function with no blaToIndex, so the wrong value was stored (with -inline -localOptimizations, a never-assigned local, i.e. nil).

Cause

Since #1280, on Lua the expected type of a generic argument is the selected overload's parameter type (AttrExprExpectedType.afterOverloading). That method normalised the type, which turns a bound type parameter into its base type, so wrapTranslation saw an expected bla instead of a bound T with blaToIndex. Only the fromIndex side applied.

Fix

afterOverloading returns the raw parameter type (and the raw expected type in its fallback), so both conversions are seen and cancel as on Jass. A parameter which is not a bound type parameter is unchanged, because normalize() only differs for bound type parameters.

Evidence

  • New GenericsTests.nestedOldGenericConversionsCancel and ...CancelOptimized (-inline -localOptimizations) run on the interpreter, Jass and Lua. The conversions are deliberately asymmetric (blaToIndex multiplies by 10) so a missing one shows. Both failed on Lua before the fix ("Succeed function not called") and pass after.
  • GenericsTests.implicitConversions2 no longer opts out of Lua (testLua(true).luaOnly(false)).
  • Focused classes green: GenericsTests (73), GenericsWithTypeclassesTests (115), LuaBackendAuditTests (242), LuaTranslationTests (144).
  • Emitted Lua for the repro is now Cell_Cell_set(c, Cell_Cell_get(c)), matching Jass.

Platforms

Ran on Windows only. The change is a type lookup in the translation with no host-dependent code, but Linux and macOS are left to CI.

On Lua the expected type of a generic argument came from the selected overload,
normalised, which dropped the bound type parameter and with it the toIndex
conversion. A value read from an old generic and passed straight back into one
then kept its fromIndex alone: c.set(c.get()) on a Cell<bla> passed the tuple to
a one-parameter function. Use the raw parameter type, so the two conversions
cancel as they do on Jass.
@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-20T14:23:56.911657Z 072a6ba 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 6e96771 into master Oct 20, 2026
2 checks passed
Frotty added a commit that referenced this pull request Oct 21, 2026
#1502 stopped normalising attrExpectedTypAfterOverloading so that the Lua
translation of a generic argument keeps its bound type parameter and with it
the toIndex conversion. The closure checks and the closure translation read
the same attribute, and a bound type parameter is neither a class nor an
interface type, so a closure passed to a generic parameter was rejected:
listeners.add(() -> ...) on an ArrayList<Listener> failed with "Closures can
only be used when a interface or class type is given, but at this position a
Listener is expected", for old and new generics, vararg or not.

The attribute is normalised again, as before #1502; the translation asks for
the raw parameter type by its own method.
@Frotty
Frotty deleted the claude/hungry-curran-ea7b53 branch November 25, 2026 08:53
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