Skip to content

Give a member reached through an old-generic value its class's type arguments - #1531

Merged
Frotty merged 3 commits into
masterfrom
fix-chained-read-through-old-generic
Oct 21, 2026
Merged

Frotty merged 3 commits into
masterfrom
fix-chained-read-through-old-generic

Conversation

@Frotty

@Frotty Frotty commented Oct 21, 2026

Copy link
Copy Markdown
Member

Problem

A new-generic class's member reached through an old-generic value failed on Jass:

class Box<T:>
    T value
class Holder<T>
    T item
init
    let h = new Holder<Box<int>>
    ...
    if h.item.value == 5        // Could not find field array for Holder_item[h].Box_value

Reading through a local (Box<int> item = h.item) worked. A method call h.item.get() failed too, with an ArrayIndexOutOfBoundsException in specializeMethod.

EliminateGenerics.addMemberTypeArguments takes a member access's or method call's class type arguments from the receiver's IM type. An old-generic value is erased to any, so the access kept no type arguments and still named the field of the unspecialised Box<T>, which class elimination has no array for. The Wurst type of the receiver still had them, so the information was lost in the AST-to-IM translation.

Change

When the translated receiver's IM type is any, the translation supplies the class's type arguments from the receiver's Wurst type, or from the supertype that declares the member (ExprTranslation.erasedReceiverTypeArguments). All six translation sites that create a field access with a receiver go through one helper (memberAccess): reads, lvalues, and the overloaded [] get and set. A dynamic-dispatch method call adds the same arguments in front of its own. Receivers of any other IM type are unchanged, and no IM nodes are added.

Tests

In GenericsTests, run in the interpreter, on Jass and on Lua. Each failed on master:

  • the read, also with -inline -localOptimizations;
  • a write (h.item.value = 7);
  • a method call;
  • a read through an old-generic function result (id(b).value);
  • an inherited field and method (Sub<U:> extends Box<U> in Holder<Sub<string>>).

A non-generic Box and a new-generic Holder<T:> already passed.

Windows, bundled Lua 5.3, -PallBackends: GenericsTests 109 and CompiletimeTests 64 on master after #1527. Before the rebase: GenericsWithTypeclassesTests 119, ClassesTests 128, LuaBackendAuditTests 285, GenericSpecialisationMatrixTests 384 and DeterministicChecks 29. Two large real maps compile to byte-identical optimised Lua, and one also to byte-identical Jass. Not done: Linux and macOS.

Not covered: an old-generic value whose argument is a new-generic type variable, rather than a concrete class, gets no type arguments, as before.

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

…rguments

A field read, field write or method call on an old-generic value whose type
argument is a new-generic class, such as h.item.value with h a
Holder<Box<int>> or id(b).value with an old-generic id, failed on Jass:
"Could not find field array for Holder_item[h].Box_value", or an index error
in specializeMethod for h.item.get().

EliminateGenerics gives a member access or method call the type arguments of
the member's class from the receiver's IM type (addMemberTypeArguments). An
old-generic value is erased to any in the IM, so the receiver had no class
type, the access kept no type arguments and still named the member of the
unspecialised Box, which class elimination has no field array for. Reading
through a local of type Box<int> worked because the local carries the type.

The translation still has the receiver's Wurst type, so it now supplies the
class's type arguments when the translated receiver is erased: from the
receiver type itself or the supertype that declares the member. Field
accesses go through one helper, method calls prepend the arguments to their
own. Every other receiver is unchanged and gets its arguments from its IM
type as before. The IM shape is otherwise the same as for a typed receiver,
so neither backend emits anything new.
@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-21T19:28:25.326546Z 52a99e0 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: 2518046b34

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

…class's type arguments

The previous change supplied the class's type arguments to field accesses and
method calls whose receiver is an erased old-generic value, but three other
translations build a call on such a receiver themselves. With h a
Holder<Box<int>> and Box<T:> a new-generic class, h.item + "!" calling
Box.toString implicitly, destroy h.item, and for x from h.item each still
reached the members of the unspecialised Box on Jass: an index error in
specializeMethod for the first two, and a method call left for the Jass
translation for the loop.

The implicit toString call and the destroy call now take their type
arguments from erasedReceiverTypeArguments, as the explicit method call does.
The for-from loop stores its target in a variable typed by the erased IM
type, so the hasNext and next calls on that variable had no class to read
the arguments from; the variable now takes the class of the target's Wurst
type when the target is erased, as a local declared with that type would.

The other direct constructions of a member access or method call cannot see
an erased receiver: class field initialisers and closures access through
this or a closure variable of a class type, null-safe accesses and cascades
store the receiver in a variable of its Wurst type, the for-in loop calls
iterator() with its own type arguments, and the later passes copy an
existing access with its type arguments.
@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. Can't wait for the next one!

Reviewed commit: 01e0644e8b

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

…rough-old-generic

# Conflicts:
#	de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/GenericsTests.java
@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. Keep them coming!

Reviewed commit: 52a99e0ce9

ℹ️ 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 f540be4 into master Oct 21, 2026
2 checks passed
@Frotty
Frotty deleted the fix-chained-read-through-old-generic 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