Skip to content

Let a sub-interface default implement the super-interface's method - #1533

Merged
Frotty merged 5 commits into
masterfrom
fix-subinterface-default-implements-super
Oct 21, 2026
Merged

Frotty merged 5 commits into
masterfrom
fix-subinterface-default-implements-super

Conversation

@Frotty

@Frotty Frotty commented Oct 21, 2026

Copy link
Copy Markdown
Member

Problem

A default method in a sub-interface never implemented the super-interface's method, on every backend, generic or not:

interface Showable
    function show() returns int
interface Shower extends Showable
    function show() returns int
        return 7
class C implements Shower
init
    Showable s = new C()
    s.show()        // Lua calls a nil slot; the interpreter and Jass never return 7

WurstValidator.findImplementationLink decides the implementation both for the validator's abstract-method check and for ImTranslator.getClassesWithImplementation, and it skipped every candidate whose attrNearestClassDef() is null. So an interface default never counted, InterfaceTranslator linked no sub-method, and class C implements Showable, Shower got "Non-abstract class C must implement the following functions".

Change

  • Validator: when no class candidate implements the method, the nearest non-abstract default from a sub-interface of the declaring interface does (findSubInterfaceDefault, by supertype distance). The class's own method, a superclass method, an override below the class and module implementations still win, as before. No existing message changes.
  • Translator: InterfaceTranslator now gives the class a method of its own with exactly the default the validator chose. Before, it took the first default it found, so a class with S1 and S12 extends S1 ran S1's default.
  • Ambiguity is a compile error. When two sibling sub-interfaces (neither extends the other) both default the method at the same distance, and the class does not settle it, the class gets a new error:
    Class C must override function show, because it inherits a default implementation of it from each of the interfaces S1, S2.
    • Settles it: the class's own method or override, or a method from a superclass that itself implements the interfaces. Since a default beats an inherited class method, an inherited method from a class outside the interfaces does not settle it, and class E extends Base implements S1, S2 gets the error too.
    • No error: when one sub-interface extends the other, the nearer default wins.
    • Like "must implement": the error is not reported on abstract classes.

Tests

InterfaceTests covers:

  • the repro, through both interfaces;
  • a class method and a subclass override beating the default;
  • a class naming both interfaces, in either order;
  • a new generic Shower<T:> and an old generic Shower<T>;
  • a chain of three interfaces;
  • a default calling another interface method;
  • a sub-interface default over the super-interface's own default;
  • the sibling error, the sibling case settled by an override (it runs the class's own method), and the error behind an inherited class method.

Also:

  • LuaBackendAuditTests.aSubInterfaceDefaultIsBoundToTheSlotOfTheSuperInterfaceMethod checks the slot each class binds (AGENTS.md §8).
  • DeterministicChecks.subInterfaceDefaultDispatchIsIdenticalAcrossCompilations compiles twice and with the units in another order.

Without the change, every new test fails except one guard.

Windows, bundled Lua 5.3, -PallBackends: InterfaceTests 52, LuaBackendAuditTests 286, DeterministicChecks 31 and ClassesTests 128 on master after #1530. Before the rebase: ModuleTests 37, NewFeatureTests 42, GenericsTests 90, BugTests 132, LuaTranslationTests 149, and the whole suite with 3,286 tests. Two large real maps compile without new errors or warnings, and to byte-identical optimised Lua (one also to byte-identical Jass). Not done: Linux and macOS.

Not changed: InterfaceTranslator.defaultOf still skips generic interfaces, so for a class that inherits a class method and also gets a default from a generic interface, the inherited method still wins. No test covers that.

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

A class which took show() from the default of an interface extending the
interface that declares show() had no implementation of it through the
super-interface: a call through Showable read a nil slot on Lua, and the
interpreter and Jass never ran the default. A class which named both
interfaces was told that it must implement show().

WurstValidator.findImplementationLink, which decides the implementation for
the validator's abstract-method check and for the sub-methods the
translation links, only took methods of classes and skipped every candidate
without an enclosing class, so an interface default never counted.

When no class in the hierarchy implements the method, it now takes the
default of an interface which extends the declaring interface: the one
nearest to the class, and at the same distance the one the class names
first. A class's own method, an inherited class method and module
implementations keep their precedence, and a default of an unrelated
interface with the same signature is still not an implementation. This
covers new and old generic interfaces, chains of interfaces and a default
which overrides the super-interface's default.
A class which took a super-interface method from the defaults of two
sub-interfaces, neither extending the other, ran the default of the
interface it named first through the super-interface. Which interface a
class names first is no reason to prefer its default, so the class has no
single implementation of the method.

The validator now reports this on the class: "Class C must override
function show, because it inherits a default implementation of it from
each of the interfaces S1, S2." A class which overrides the method, or
inherits a class method that implements it, gets no error, and where one
sub-interface extends the other, its default is the nearer one and wins
without an error.

The translator also took the first default it found for such a class
instead of the default the validator chose, so a class implementing S1
and S12 extends S1 ran S1's default through S1 and Showable. A class which
takes a method from a sub-interface default now gets a method of its own
with exactly that default.
A class which inherits show from a superclass outside the interfaces and
takes two sibling sub-interface defaults for Showable's show got no error,
because the inherited method counted as its implementation. But a default
beats an inherited method, so the default of the interface the class names
first still ran through Showable.

The check now leaves out only a class whose own method implements the
method, or one inherited from a superclass which itself implements the
interface, where that method wins. It reads the defaults from the
interfaces' own methods, since the inherited class method hides them in
the class's name links, and reports the same error as for a class with no
implementation.
@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:52:50.021690Z ed7cdbf 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: 36d17821e6

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

When GrandBase declares show, Base extends GrandBase implements Showable,
and C extends Base implements S1, S2, Base has already adopted GrandBase's
show as its Showable implementation, and C inherits it. The validator only
asked whether GrandBase implements Showable, so it told C to override show.
The translator chose the default from C's own interfaces instead, so a call
through Showable ran S1's default for C and GrandBase's show for Base.

The validator now treats the inherited method as settled when any class
from C's superclass up to the declaring class implements the interface.
The translator looks for a default beating the inherited method from the
highest such class rather than from C, so C runs what Base runs.
@Frotty

Frotty commented Oct 21, 2026

Copy link
Copy Markdown
Member Author

@codex review

@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: b558bc2bce

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

Three gaps in how a class resolves a super-interface method from
sub-interface defaults:

A generic sub-interface's default (Shower<T:> or Shower<T> extends
Showable) never beat a method the class inherits from a superclass outside
the interface. The translator looked for such a default with defaultOf,
which skips generic interfaces, so a call through Showable ran the
inherited method. It now asks the validator for the sub-interface default
first, generic or not, and falls back to defaultOf for other interfaces.

A superclass which took the method from a default (Base implements S1)
did not settle it for its subclasses: C extends Base implements S2 found
no class method and chose from its own interfaces again. A superclass which
implements the interface now settles the method for a subclass that does
not declare it, whether the superclass runs a class method or a default,
in both the validator and the translation.

A default was chosen over a sibling default because its interface was
fewer steps from the class (C implements Wrapper, S2 with Wrapper extends
S1). Distance does not make a default more specific, so only a default
whose interface extends the other's wins now; otherwise the class must
override the method, with the existing error.
@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 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: ed7cdbf732

ℹ️ 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 aee56dd into master Oct 21, 2026
2 checks passed
Frotty added a commit that referenced this pull request Oct 21, 2026
Brings in #1533 (a sub-interface default implements the super-interface's method). The main
sources merged without conflicts: #1533's sub-interface default lookup (subInterfaceDefaults,
settledBySuperclass, findClassImplementationLink) compares signatures through canOverride, which
now renames the functions' own type parameters and checks their kind and bounds, and its
methodOfItsOwn call for a sub-interface default goes through the bridge, which now forwards the
implementation's own type parameters. defaultOf keeps the shared check in sameParameterTypes.

The test files conflicted only where both branches appended tests: InterfaceTests and
LuaBackendAuditTests keep master's tests followed by this branch's, and DeterministicChecks keeps
both new determinism checks.
Frotty added a commit that referenced this pull request Oct 21, 2026
With #1533 a sub-interface default implements the super-interface's method, and the class gets a
method of its own for it, through the function of its own which runs the default where the class
or the sub-interface is generic. The tests cover that for a method with a type parameter of its
own, which the default renames: a generic and a plain sub-interface, a plain and a generic class,
and calls through the super-interface with two type arguments, on every backend. Where the
function runs the default of a generic sub-interface, or a generic class takes a plain
sub-interface's default, it must forward the method's own type parameter: without that the Jass
generic elimination fails ("Rewrite generics with wrong sizes"). Two sibling sub-interface
defaults of such a method are the "must override" error, and a sub-interface method of another
kind of generic is no default for it.
Frotty added a commit that referenced this pull request Oct 22, 2026
…ts own (#1534)

* Let a method with type parameters of its own implement and override one which declares the same

No class could implement an interface method which declares type parameters of its own
(interface Top: function id<T:>(T value) returns T), and no class method could override a
superclass method of that shape: only a default in the interface itself worked. checkOverride
compared the parameter and return types of the two functions as they are written, so the T of
the implementation and the T of the interface method were two unrelated type parameters, and
the class was told it must implement the method (or that its override overrides nothing). Old
generic <T> methods had the same defect.

checkOverride now pairs the functions' own type parameters by position and compares the types
with the overridden function's renamed to the overriding one's. The two must declare the same
number, of the same kind of generic (old <T> or new <T:>), with the same bounds in the same
order: a call through the overridden function only guarantees its bounds, and the override is
specialised with what that call provides. A mismatch in an override says what it should be
("takes 1 type parameters, but there are 2 type parameters here.", "Type parameter T: Show
should be T: to override ..."); a class which does not implement the interface method still
gets the existing "must implement" message. A method without type parameters no longer counts
as implementing one which declares some.

Once accepted, the Jass generic elimination left an override of such a method unspecialised:
adaptSubmethods kept the sub-method of a plain class as it was and gave the sub-method of a
generic class only its class's type arguments ("Type variable T not eliminated", "Rewrite
generics with wrong sizes"). The method's own type arguments follow its class's in the
arguments a method is specialised with, and an override declares the same ones, so they now
carry over to every sub-method after that sub-method's class arguments.

* Forward an inherited implementation's own type parameters through the function its class gets for it

A class which implements an interface method with a method it inherits from a class outside
the interface gets a method of its own for it, and where the class or the implementation's class
is generic, a function of its own which calls the inherited one (implementationOfItsOwn). That
function declared no type variables and called the implementation with no type arguments. Now
that a method with type parameters of its own can implement an interface method, such a
function stood for a generic implementation with nothing to specialise it: on Jass the generic
elimination gave it the class's type arguments and the call's method arguments for the class's
variables alone ("Rewrite generics with wrong sizes"), and the interpreter could not dispatch a
bound of the method's parameter ("No type argument bound for T").

The function now declares a copy of each type variable the implementation declares itself,
uses it in the parameter and return types in place of the implementation's, and passes it to
the implementation, independently of the type arguments of the implementation's owner, which are
still substituted as the class sees them. A call through the interface therefore specialises
the implementation with its own type arguments on every backend. The function depends only on
the implementation, so it serves an interface default which implements the method the same way.

The default which another interface of the class gives the method beats the inherited one
(defaultOf), but sameParameterTypes compared the parameter types as written, so a default with
its own type parameters never matched and the inherited method ran instead. It now compares them
with the default's type parameters renamed to the abstract method's.

* Forward a method's own type parameters through the index-conversion wrapper of an override

Where an old-generic interface is bound to a type which needs index conversions (I<real>), the
override of its method is reached through a wrapper (OverrideUtils.addOverride) which converts the
indices the interface passes. The wrapper copied the interface method's parameters, declared no
type variables and called the implementation with no type arguments. For a method with a type
parameter of its own (function f<T:>(X x, T t) returns T), which a class can now implement, the
wrapper's parameters kept the interface method's type variable while the wrapper declared none,
and the Jass generic elimination left it in place ("Type variable T not eliminated").

The wrapper now declares a copy of each type variable the interface method declares itself, uses
the copies in its parameter and return types and passes them to the implementation, as the
function a class gets for an inherited implementation does.

* Let a default stand for an abstract method only where its type parameters could override it

A class which inherits the implementation of an interface method takes the default another of
its interfaces gives that method instead (defaultOf). sameParameterTypes decided that by renaming
the default's own type parameters to the abstract method's and comparing the parameter types, but
never compared the type parameters themselves. A default with an old generic <U> stood for a new
generic <T:> method, and a default asking for a bound <U: Show> for a method without it: a call
through the abstract interface ran the default instead of the inherited method, and on the
interpreter could not dispatch the bound the call never provided ("No type class instance bound
for show").

The check of the functions' own type parameters (as many, of the same kind of generic, with the
same bounds) moves out of checkOverride into WurstValidator.checkOwnTypeParameters, with the
renaming in ownTypeParameterRenaming. checkOverride and sameParameterTypes both use them, so a
default stands for an abstract method exactly where it could override it.

* Test that a tuple type argument of a method's own reaches the erased implementing class on Lua

A call through a generic interface with a tuple as the method's own type argument specialises the
method on Lua, and adaptSubmethods asks allocatesFromSpecialisedClass with the class's and the
method's arguments together, which the tuple makes true although C<string> objects stay erased.
The specialised implementation still lands on the erased class: specializeMethod gives it the
class of the class-argument prefix, which for C<string> is the erased C, and attaches it there. The
emitted script binds the dispatched slot on the erased tables (C.C_second_specialized_... =
C_second_specialized, and the same for X), and the program runs on every backend. This test keeps
it that way.

* Test sub-interface defaults of a method with a type parameter of its own

With #1533 a sub-interface default implements the super-interface's method, and the class gets a
method of its own for it, through the function of its own which runs the default where the class
or the sub-interface is generic. The tests cover that for a method with a type parameter of its
own, which the default renames: a generic and a plain sub-interface, a plain and a generic class,
and calls through the super-interface with two type arguments, on every backend. Where the
function runs the default of a generic sub-interface, or a generic class takes a plain
sub-interface's default, it must forward the method's own type parameter: without that the Jass
generic elimination fails ("Rewrite generics with wrong sizes"). Two sibling sub-interface
defaults of such a method are the "must override" error, and a sub-interface method of another
kind of generic is no default for it.

* Lua: bind a method specialised for its own type arguments alone to the erased generic class

A plain interface method with a type parameter of its own (Top.pick<T:>), implemented by a generic
class (C<Y:> implements Top), is specialised on Lua when a call gives it a tuple (t.pick<pair>).
The interface gives only the method's argument, so adaptSubmethods could not take C's class
arguments from it and only registered a callback for the specialisations of C. An ordinary
new C<string>() leaves C erased and never specialises it, so nothing bound C's implementation to
the specialised slot, and the call failed ("attempt to call a nil value (field
'Top_pick_specialized__integer__integer_')").

Where the class's arguments are unknown but the method's own are not, the sub-method is now also
specialised for the method's arguments alone and bound to the erased class: its class's type
variables stay as they are, erased as in the class's other methods. The overrides below it get the
same, generic ones on their erased class and plain ones as ordinary specialisations. The callback
for specialised instantiations of the class stays.

* Let a default beat an inherited method only where it could override the abstract method

defaultOf lets the default of another interface beat the method a class inherits for an abstract
interface method. Its predicate, sameParameterTypes, compared the own type parameters and the
parameter types but not the return type, so a default pick<U:>(U value) returns int stood for
pick<T:>(T value) returns T: a call Abstract.pick<string> ran the default and returned an int
("Succeed function not called"). It also compared the abstract method's parameter types unbound,
as written in its interface.

defaultOf now binds the abstract method to the class and accepts a default only where
WurstValidator.canOverride does, the check of an override: its own type parameters, parameters
and return type. The validator's own-type-parameter helpers, which sameParameterTypes shared,
are private to it again.
@Frotty
Frotty deleted the fix-subinterface-default-implements-super 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