Repository navigation
Lua: run an interface default method for the implementing class's instantiation - #1527
Conversation
…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.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Problem
interface Shower<T: Kc>has a default methodshow()returningT.kkey(val()), andIntShower implements Shower<int>andStrShower implements Shower<string>are called throughShower<int>. Lua translation crashed with "Lua IM contains a dangling reference to removed function 'kkey'". The interpreter and Jass were right.EliminateGenerics.inheritsFromSpecialisedClassskipped 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>).TreeShakerfollows only type arguments in reachable code, so it removedState's constructor (Jass:#alloc(Other)left unlowered; Lua: danglingnew_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:Also:
FieldIterationTests.genericConstructionInAnInterfaceDefaultMethod(the TreeShaker sibling) failed on master on both targets.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 Showabledeclaringshow(),interface Shower extends Showablewith a body forshow(), andclass C implements Shower,s.show()on aShowablefails: Lua calls a nil slot, and the interpreter and Jass never reach the call.WurstValidator.findImplementationLinkskips candidates whoseattrNearestClassDef()is null, soInterfaceTranslatornever links the default.Found by a defect audit of the 19-21 Oct merges.