You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
virtual is written where it doesn't compile: C++ static, constructor and free functions; C# constructors and destructors #467
C++:Coder/Languages/CppGenerator.cs:296-306 writes static and then virtual whenever IsVirtual || IsAbstract. It never checks whether the function is static, is a constructor, or has an enclosing class at all.
C#:OverrideObstacle (Coder/Languages/CSharpGenerator.cs:1803-1823) drops virtual with a note for static, struct and private members and for operators (#412 / #416). It does not cover FunctionKind.Constructor or FunctionKind.Destructor, so canOverride stays true and virtual is written.
a static function int make() with IsVirtual = true, and the same with IsAbstract = true
a constructor Shape with IsVirtual = true
a destructor Shape with IsVirtual = true, the idiomatic C++ base-class destructor
and the same file has a free function int helper() with IsVirtual = true.
Target
Output
Compiler
C++
static virtual int make() / static virtual int make() = 0;
g++: member cannot be declared both 'virtual' and 'static'
C++
virtual Shape()
g++: constructors cannot be declared 'virtual'
C++
virtual int helper() at namespace scope
g++: 'virtual' outside class declaration
C#
public virtual Shape() and virtual ~Shape()
dotnet build: CS0106 The modifier 'virtual' is not valid for this item
Why it matters
A virtual destructor is the standard C++ base-class pattern, so an AST written C++-first generates C# that does not compile. C# already handles the static case with a note. C++ has no equivalent guard at all, so a static-virtual or abstract-static AST, which C# accepts in interfaces, produces C++ that does not compile.
Open PR #455 changes C# access modifiers per position but does not touch virtual.
Suggested fix
C#: have OverrideObstacle return an obstacle, with its WriteInexpressible note, for Kind is FunctionKind.Constructor or FunctionKind.Destructor.
C++: write virtual only for a non-static member function with an enclosing class that is not a constructor; destructors keep it. Otherwise drop it and write a note, the way C# does.
For a static abstract function in C++, drop = 0 too, since there is nothing for it to mean.
Add compile tests (g++ and dotnet build) for each row above.
Priority: Medium. The generated C++ and C# fail to compile, but the failure is loud and needs IsVirtual/IsAbstract set on a static member, constructor, destructor or free function. The C# virtual destructor case is the most likely to show up in practice, because ASTs written C++-first use it.
Area / suggested assignee:CSharpGenerator.OverrideObstacle (:1803-1823) and the modifier writer in CppGenerator.cs:296-306. Owner: @matt-edmondson
Next step: add Constructor and Destructor to OverrideObstacle. Add a matching guard in C++ that keeps virtual on destructors and drops = 0 for static abstract. Add a g++ and a dotnet build compile test for each row of the table. Land this after Spell C# access modifiers C# allows where each declaration stands [patch] #455 to avoid conflicts in the same modifier code.
Activity
matt-edmondson commentedon Nov 7, 2026
Triage
IsVirtual/IsAbstractset on a static member, constructor, destructor or free function. The C# virtual destructor case is the most likely to show up in practice, because ASTs written C++-first use it.CSharpGenerator.OverrideObstacle(:1803-1823) and the modifier writer inCppGenerator.cs:296-306. Owner: @matt-edmondsonvirtual/abstracton private, struct, and static methods (CS0621, CS0106, CS0112) #412/Leave virtual and abstract off C# methods that can't be overridden [patch] #416, which covered static, struct, private and operator members in C#. Open PR Spell C# access modifiers C# allows where each declaration stands [patch] #455 (C# access modifiers per position) is adjacent but, as the issue says, does not touchvirtual, so this is not already in progress.OverrideObstacle. Add a matching guard in C++ that keepsvirtualon destructors and drops= 0for static abstract. Add a g++ and adotnet buildcompile test for each row of the table. Land this after Spell C# access modifiers C# allows where each declaration stands [patch] #455 to avoid conflicts in the same modifier code.Generated by Claude Code