Skip to content

Give a read of an inherited generic field its superclass's type arguments - #1526

Merged
Frotty merged 2 commits into
masterfrom
fix-inherited-generic-field-type
Oct 21, 2026
Merged

Frotty merged 2 commits into
masterfrom
fix-inherited-generic-field-type

Conversation

@Frotty

@Frotty Frotty commented Oct 21, 2026

Copy link
Copy Markdown
Member

Problem

class G<T:> with a field T v, class Sub extends G<int>, then let z = b ? s.v : 5 failed Jass translation with "Type variable T not eliminated". ImAttrType.getType(ImMemberAccess) substituted only the receiver's own class's type arguments into the field's type, so a field declared in a generic superclass kept T. The conditional's temporary is typed from its first branch (ExprTranslation.translate(ExprIfElse)), so it became a local of type T, where T is not in scope. The interpreter and Lua passed. #1510 fixed the same shape for method calls (getType(ImMethodCall) with asSuperclass).

Change

For an access without its own type arguments, ImAttrType now finds the class that declares the field and walks the receiver's type up to it with asSuperclass, then substitutes that class's arguments, the same rule as the method-call case. If the receiver leaves out its type arguments, it falls back to them as before. An access that carries type arguments is unchanged.

Tests

Four tests in GenericsTests, each run in the interpreter, on Jass and on Lua, and each failing on Jass without the change:

  • the repro, inheritedGenericFieldReadInConditional;
  • the implicit receiver (b ? v : 5 in a method of Sub);
  • two generic superclasses (G<T:>, H<U:> extends G<U>, Sub extends H<int>);
  • a later type parameter (G<A:, B:>, Sub<U:> extends G<int, U>, which failed with "Type variable B not eliminated").

Windows, bundled Lua 5.3, -PallBackends: GenericsTests 85, GenericsWithTypeclassesTests 119, ClassesTests 128, LuaBackendAuditTests 285, GenericSpecialisationMatrixTests 384, DeterministicChecks 29, all passing. The four new tests also pass with -PallBackends=full. Two large real maps compile to byte-identical optimised Lua with and without the change. Not done: Linux and macOS.

Left alone

  • An array field typed by a type parameter (T array[3] arr in G<T:>) fails type checking with "Cannot assign integer-literal to T", even with no inheritance, because WurstTypeArray does not substitute its element type. That is a separate front-end gap.
  • EliminateGenerics, ImAttrType and LuaFieldDefaults read an ImMemberAccess's own type arguments differently when the receiver's class is not the declaring class. No probe showed a difference, so this is not changed here.

Found by a defect audit of the 19-21 Oct merges.

…ents

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.
@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-21T16:51:10.384241Z f8d0ff0 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.

…ic-field-type

# 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. You're on a roll.

Reviewed commit: f8d0ff08a8

ℹ️ 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 50dd79b into master Oct 21, 2026
2 checks passed
@Frotty
Frotty deleted the fix-inherited-generic-field-type 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