Skip to content

Substitute type arguments into the element type of an array field - #1530

Merged
Frotty merged 1 commit into
masterfrom
fix-generic-array-field
Oct 21, 2026
Merged

Frotty merged 1 commit into
masterfrom
fix-generic-array-field

Conversation

@Frotty

@Frotty Frotty commented Oct 21, 2026

Copy link
Copy Markdown
Member

Problem

In a generic class, an array field whose element type is the type parameter could not be used through a concrete instantiation:

class G<T:>
    T array[3] arr
init
    let g = new G<int>()
    g.arr[1] = 3            // Cannot assign integer-literal to T

The validator accepts the declaration (its array-field checks are zero dimensions, a missing size and several dimensions), plain T fields and static T array already work, and the backends handle the field once it is typed. The type checker was inconsistent: a member access substitutes the receiver's type arguments into the field's type with setTypeArgs (VarLink.withTypeArgBinding), and WurstTypeArray did not override it, so its element type stayed the unbound T. Old generics <T> had the same gap.

Change

WurstTypeArray.setTypeArgs substitutes into the element type, as WurstTypeVararg already does, and returns this when nothing changes. The substituted copy shares the declared array's sizes (sizeSource), so the size expression is evaluated, and any error on it reported, once. No backend change: Jass specialises the arrays per instantiation, and Lua keeps erased per-instance storage.

Tests

Each new test failed without the change. All run in the interpreter, on Jass and on Lua.

  • GenericsWithTypeclassesTests:
    • an int element, also optimised;
    • methods of G reading and writing arr[i] with several instantiations;
    • a class element;
    • a tuple element, also optimised;
    • a subclass, including an upcast;
    • a generic receiver;
    • the wrong element type, which gives "Cannot assign string to int".
  • GenericsTests: old generics with int and class elements, and the wrong element type.

Windows, bundled Lua 5.3, -PallBackends: GenericsTests 103, GenericsWithTypeclassesTests 128 and ArrayTests 20 on master after #1527. Before the rebase: ClassesTests 128, LuaBackendAuditTests 285, GenericSpecialisationMatrixTests 384 and DeterministicChecks 29. The whole suite passed: 3,283 tests. Two large real maps compile to byte-identical optimised Lua, and one also to byte-identical Jass. Not done: Linux and macOS.

Found while fixing the defects from the 19-21 Oct audit (#1526).

An instance array field whose element type is the class's type parameter
(`T array[3] arr` in `class G<T:>` or `class G<T>`) was accepted, but a
read or write through a receiver of a concrete instantiation was checked
against the unbound `T`: `g.arr[1] = 3` on a `G<int>` failed with
"Cannot assign integer-literal to T", `g.arr[1] + 1` found no operator,
and a subclass `Sub extends G<int>` could not use the inherited array
with its concrete type.

The field's link substitutes the receiver's type arguments into the
field's type through `setTypeArgs`, but `WurstTypeArray` did not
override it, so the default returned the array type unchanged and the
binding never reached the element type. A plain field `T x` and a
static `T array` (read inside the class, where `T` stays unbound) were
not affected.

`WurstTypeArray.setTypeArgs` now substitutes into the element type, as
`WurstTypeVararg` already did. The substituted array shares the sizes of
the declared one, so its size expression is still evaluated and
reported once. The backends needed no change: Jass specialises one set
of typed arrays per instantiation, and Lua keeps the erased per-instance
table it uses for other type-parameter fields.

Tests cover new generics with int, string, class and tuple elements,
methods of the class, two instantiations, a subclass, a generic
receiver and an element of the wrong type, with and without the
optimiser on the interpreter, Jass and Lua, and old generics with int
and class elements, a subclass and the wrong element type.
@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-21T17:54:26.085931Z c91cdda 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 a908cb8 into master Oct 21, 2026
2 checks passed
@Frotty
Frotty deleted the fix-generic-array-field 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