Skip to content

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

Closed

Description

@matt-edmondson

What's wrong

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.

Reproduction (HEAD 60ace00)

class Shape contains:

  • 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.

Activity

  1. matt-edmondson commented on Nov 7, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions