Skip to content

Write virtual only where C++ and C# accept it [patch] - #469

Merged
matt-edmondson merged 2 commits into
mainfrom
fix/467-virtual-modifier
Nov 10, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
fix/467-virtual-modifier

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #467

What was wrong

What changed

  • C++: the new WriteVirtualNote writes virtual only on a non-static member function that is not a constructor. Destructors keep it, since a virtual destructor is the usual base-class pattern. Anywhere else the modifier is dropped and a note says so (// virtual: make is static, which C++ cannot declare virtual). An abstract function that cannot be virtual is declared with ; instead of = 0.
  • C#: OverrideObstacle now returns Constructor or Destructor obstacles, and each writes a note. An abstract constructor or finalizer has no body once the modifier is gone, so the note replaces the whole declaration, the same way a static abstract member is handled. A private virtual constructor also stays private now; before, the private-virtual rule made it protected.
  • FunctionShapeTests.Cpp_SpellsEachKindOfDefinition used to generate a bare free abstract function and expect virtual void step() = 0;, which is the namespace-scope case the issue reports as invalid C++. The function now sits inside a class, so the test still checks = 0.

Tests

VirtualModifierPlacementTests builds the issue's reproduction: class Shape with a virtual constructor, a virtual destructor, a static virtual function, a static abstract function and a virtual method, plus a virtual free function. The class has:

  • generation assertions for C++ and C#
  • a g++ compile test (-std=c++20 -Wall -Wextra -pedantic -Werror)
  • a dotnet build compile test

All 5 tests fail with the generator changes reverted and pass with them. Full Coder.Test suite: 1669 tests, all passing.

The issue's triage suggests landing this after #455. #455 calls OverrideObstacle but doesn't change its body, and this PR only changes the body, so the two shouldn't conflict.


Generated by Claude Code

matt-edmondson and others added 2 commits November 8, 2026 03:30
C++ wrote `virtual` on static members, constructors and functions outside a
class, and C# on constructors and finalizers. Each now drops it with a note
saying what was dropped. A C++ destructor keeps it, and a static or free
abstract function in C++ is declared without `= 0`.

Fixes #467

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW2mFigVQkgzUYqEcdJ
Addresses SonarQube S3358 on the new WriteVirtualNote.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW2mFigVQkgzUYqEcdJ
@sonarqubecloud

sonarqubecloud Bot commented Nov 8, 2026

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit f65f175 into main Nov 10, 2026
16 checks passed
@matt-edmondson
matt-edmondson deleted the fix/467-virtual-modifier branch November 10, 2026 23: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.

virtual is written where it doesn't compile: C++ static, constructor and free functions; C# constructors and destructors

1 participant