Skip to content

[cpp] Add missing override specifiers#12840

Open
tobil4sk wants to merge 4 commits intoHaxeFoundation:developmentfrom
tobil4sk:fix/cpp-overrides
Open

[cpp] Add missing override specifiers#12840
tobil4sk wants to merge 4 commits intoHaxeFoundation:developmentfrom
tobil4sk:fix/cpp-overrides

Conversation

@tobil4sk
Copy link
Member

@tobil4sk tobil4sk commented Mar 17, 2026

Together with HaxeFoundation/hxcpp#1319, this fixes warnings about missing overrides that show up when compiling a hxcpp program.

These were already present in a few places, and it is fine to add them because the minimum c++ version for haxe 5 is already c++11. It is good practice to have these to help catch issues like #11666, where a method is overloaded instead of overridden.

I had to modify the test for #9516, because adding these revealed a possible bug. Despite override in haxe code, Baz.fun was never actually overriding Bar.fun, because Foo is marked as @:nativeGen. This happens even with haxe 4.3.7, but is only revealed when these override specifiers are added.

Adding overrides explicitly has revealed a potential bug in this test
case. Even though Baz.fun looks like it is overriding Foo.fun, this is
not actually the case because Foo is generated as non-virtual and
therefore cannot be overridden.

This may be a bug with nativeGen.
@tobil4sk
Copy link
Member Author

@Aidan63 does this seem good to you? You mentioned you ran into some weird issues when you last tried this.

@Aidan63
Copy link
Contributor

Aidan63 commented Mar 18, 2026

I think this looks good. I'm guessing I was getting tripped up by marking all toString functions as overridden, even if they had arguments, because I don't remember doing that.

@tobil4sk
Copy link
Member Author

Yeah the toString situation is a bit finicky. The genCppia code should probably check for arguments too because currently it just uses the name: #12375, but that's a separate issue.

I guess I should also check that a virtual toString in a @:nativeGen class doesn't automatically become override because that doesn't actually inherit from hx::Object.

Btw, in that test case that I had to modify:

@:nativeGen
@:structAccess
private class Foo {
  // ...
  public function fun() return /*...*/;
}

The problem is that Foo.fun is not overridden in any haxe code, so it assumes it is non virtual. Do you know if there is any way to tell haxe that Foo might be extended in external code? I tried making it public, but that doesn't help either, I also found @:nonVirtual but that is the opposite.

@Aidan63
Copy link
Contributor

Aidan63 commented Mar 18, 2026

For native gen classes you might just have to assume that every function is virtual unless otherwise specified (final, nonVirtual), since thats the haxe semantics. I don't think there's any work around for that.

@tobil4sk
Copy link
Member Author

I think haxe decides somewhere else to make it non-virtual, because otherwise virtual would get added automatically. From what I can tell the behaviour is the same for @:nativeGen and normal classes, where it assumes it's not virtual unless it is overridden in the haxe program.

I'm testing this with haxe 4 and nightly:

@:nativeGen // or commented out
class Parent {
  public function fun() return 42;
}

@:nativeGen
class Child extends Parent {
  override function fun() return 10;
}

If I comment out Child or Child.fun, then haxe automatically makes Parent.fun non virtual.

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.

2 participants