Repository navigation
Write virtual only where C++ and C# accept it [patch] - #469
Merged
Merged
Conversation
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
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #467
What was wrong
CppGenerator.GenerateFunction) wrotevirtualwheneverIsVirtual || IsAbstract, without checking for a static member, a constructor, or a function outside a class. g++ rejectsstatic virtual int make(),virtual Shape()and a namespace-scopevirtual int helper().CSharpGenerator.OverrideObstacle) already dropped the modifier from static, struct, private and operator members (C# writesvirtual/abstracton private, struct, and static methods (CS0621, CS0106, CS0112) #412 / Leave virtual and abstract off C# methods that can't be overridden [patch] #416), but not from constructors or finalizers, sopublic virtual Shape()andvirtual ~Shape()failed with CS0106.What changed
WriteVirtualNotewritesvirtualonly 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.OverrideObstaclenow returnsConstructororDestructorobstacles, 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 itprotected.FunctionShapeTests.Cpp_SpellsEachKindOfDefinitionused to generate a bare free abstract function and expectvirtual 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
VirtualModifierPlacementTestsbuilds the issue's reproduction:class Shapewith 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:-std=c++20 -Wall -Wextra -pedantic -Werror)dotnet buildcompile testAll 5 tests fail with the generator changes reverted and pass with them. Full
Coder.Testsuite: 1669 tests, all passing.The issue's triage suggests landing this after #455. #455 calls
OverrideObstaclebut doesn't change its body, and this PR only changes the body, so the two shouldn't conflict.Generated by Claude Code