Skip to content

Lua: run an interface default method for the implementing class's instantiation - #1527

Merged
Frotty merged 3 commits into
masterfrom
fix-lua-interface-default-bound
Oct 21, 2026
Merged

Frotty merged 3 commits into
masterfrom
fix-lua-interface-default-bound

Conversation

@Frotty

@Frotty Frotty commented Oct 21, 2026

Copy link
Copy Markdown
Member

Problem

interface Shower<T: Kc> has a default method show() returning T.kkey(val()), and IntShower implements Shower<int> and StrShower implements Shower<string> are called through Shower<int>. Lua translation crashed with "Lua IM contains a dangling reference to removed function 'kkey'". The interpreter and Jass were right.

EliminateGenerics.inheritsFromSpecialisedClass skipped every interface as having "no code to inherit". So an implementing class kept the erased interface as its superclass and inherited the erased default method, whose bound dispatch Lua's targeted specialisation never resolves. A Wurst interface default method is code.

A sibling failed on both targets: wurstNewInstance<T>() in a default method, where the only mention of the constructed class is the implementing class's superclass edge (StateLoader implements Loader<State>). TreeShaker follows only type arguments in reachable code, so it removed State's constructor (Jass: #alloc(Other) left unlowered; Lua: dangling new_Other).

Change

  • EliminateGenerics: an interface counts when one of its default methods needs the type argument (classNeedsIdentity, for concrete type arguments only). Interfaces without such a default are handled as before. This path is Lua's targeted specialisation, so Jass output is unchanged.
  • TreeShaker: an allocated class's superclass type arguments are followed, recursively.

Tests

In GenericsTests (interpreter, Jass and Lua), each failing on master on Lua:

  • the repro;
  • a default overridden in one implementing class but not another, which also checks the AGENTS.md §8 slot binding in the Lua output;
  • a default inherited through an extending interface;
  • a default reached through a non-generic interface.

Also:

  • FieldIterationTests.genericConstructionInAnInterfaceDefaultMethod (the TreeShaker sibling) failed on master on both targets.
  • Guards that already passed: statics per instantiation, instanceof Box<T>, a generic implementing class.
  • DeterministicChecks.interfaceDefaultWithBoundIsIdenticalAcrossCompilations.

Four of the new tests run the interpreter only on the translated program. The interpreter on untranslated IM does not know a default's instantiation when it is inherited from a concrete one; #1524 changes that path.

Windows, bundled Lua 5.3, -PallBackends: GenericsTests 88, FieldIterationTests 45, DeterministicChecks 30, GenericsWithTypeclassesTests 119, TypeClassTests 72, InterfaceTests 40, LuaBackendAuditTests 285, LuaTranslationTests 149, GenericSpecialisationMatrixTests 384, ClassesTests 128, all passing. Two large real maps compile to byte-identical optimised Lua with and without the change, and one also to byte-identical Jass. Not done: Linux and macOS.

Found, not fixed

A default method in a sub-interface never implements a super-interface's method, on any backend, generic or not. Given interface Showable declaring show(), interface Shower extends Showable with a body for show(), and class C implements Shower, s.show() on a Showable fails: Lua calls a nil slot, and the interpreter and Jass never reach the call. WurstValidator.findImplementationLink skips candidates whose attrNearestClassDef() is null, so InterfaceTranslator never links the default.

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

…tantiation

A default method of a generic interface which dispatched on a type-class bound,
such as show() returning T.kkey(val()) in interface Shower<T: Kc>, crashed the
Lua translation when a concrete class inherited it (class IntShower implements
Shower<int>): "Lua IM contains a dangling reference to removed function 'kkey'".
Jass and the interpreter were right.

EliminateGenerics decides which classes extend the specialised class of an
instantiation instead of the erased one (inheritsFromSpecialisedClass), and it
left interfaces out as having no code to inherit. A default method is code the
implementing class inherits, so IntShower kept the erased default, whose bound
the targeted Lua specialisation never resolves. An interface now counts when
one of its default methods needs the type argument (classNeedsIdentity); one
without such a default is still decided as before. The same fixes a default
overridden in some implementing classes and inherited by others, one inherited
through an interface extending the generic interface, and one reached from a
method called through a non-generic interface.

wurstNewInstance<T>() in a default method failed on Jass and Lua alike: the
tree shake before the generics follows the type arguments in reachable code,
but the only one naming the class to construct was on the implementing class's
superclass edge (class StateLoader implements Loader<State>), so it dropped that
class's constructor. It now follows the type arguments a constructed class gives
its superclasses.

The new tests cover these shapes, a static per instantiation, an instanceof of
an instantiation of the type parameter and a generic implementing class, on Jass
and Lua, with the Lua slot binding of the overridden default and a compile-twice
determinism check. Where an inherited default does not know its instantiation in
untranslated IM, the interpreter runs the translated program only.
@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:36:49.827310Z 06237f0 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.

…efault-bound

# 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

1 similar comment
@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. 🎉

Reviewed commit: 9559290b55

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

…efault-bound

# 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. Already looking forward to the next diff.

Reviewed commit: 06237f0b85

ℹ️ 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 162c5c3 into master Oct 21, 2026
2 checks passed
@Frotty
Frotty deleted the fix-lua-interface-default-bound 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