Skip to content

C++: a property with accessor bodies over its same-named backing field (count / Count) generates int count() const beside int count, which fails to compile #140

Closed

Description

@matt-edmondson

What's wrong

C++ has no properties, so StandardLanguageGenerator.Separate lowers a property with accessor bodies into a getter/setter pair. The getter is named property.GetterName(MemberNaming), and C++ sets MemberNaming to NamingStyle.Snake, so a property Count gets the getter count().

The most common way to write a property with bodies is over a private camelCase field of the same name: field count, property Count. In C++ the getter then takes exactly the name of the field it wraps, and a data member and a member function can't share a name.

  • Getter/setter separation:
    private IEnumerable<AstNode> Separate(PropertyDeclaration property)
    {
    if (property.IsAutomatic)
    {
    // The one thing the field form cannot carry: a property may be readable and not
    // writable, and a field is neither or both. Said in the field's own documentation
    // rather than in a note beside it, so it travels with the declaration to wherever this
    // target puts it.
    IEnumerable<string> documentation = property.CanWrite
    ? property.Documentation
    : [.. property.Documentation, $"{property.Name} is read-only, which a field is not."];
    yield return new FieldDeclaration(property.Name ?? "value", property.Type)
    {
    Visibility = property.Visibility,
    IsStatic = property.IsStatic,
    Documentation = [.. documentation],
    Annotations = [.. property.Annotations.Select(annotation => annotation.Clone())],
    };
    yield break;
    }
    if (property.CanRead)
    {
    FunctionDeclaration getter = new(property.GetterName(MemberNaming))
    {
    ReturnType = property.Type,
    Visibility = property.Visibility,
    IsStatic = property.IsStatic,
    IsReadOnly = true,
    Documentation = [.. property.Documentation],
    Annotations = [.. property.Annotations.Select(annotation => annotation.Clone())],
    Body = [.. property.GetterBody.Select(statement => statement.Clone())],
    };
    yield return getter;
    }
    if (property.CanWrite)
    {
    FunctionDeclaration setter = new(property.SetterName(MemberNaming))
    {
    ReturnType = new TypeReference("void"),
    Visibility = property.Visibility,
    IsStatic = property.IsStatic,
    Body = [.. property.SetterBody.Select(statement => statement.Clone())],
    };
    setter.Parameters.Add(new Parameter("value") { Type = property.Type ?? new TypeReference("object") });
    yield return setter;
    }
    }
  • Getter name:
    public string GetterName(NamingStyle style) => NameStyles.Spell(Name ?? "value", style);
  • C++ member naming:
    /// <inheritdoc/>
    /// <remarks>
    /// The standard library spells an invented accessor <c>set_value</c>, and that is the convention a reader of any C++ header already has.
    /// </remarks>
    protected override NamingStyle MemberNaming => NamingStyle.Snake;
  • C++ separating class members:
    classDecl = Separated(classDecl);

Repro (against 8753490)

var cls = new ClassDeclaration("Counter");
cls.Members.Add(new FieldDeclaration("count", new TypeReference("int")) { Visibility = Visibility.Private, InitialValue = new LiteralExpression<int>(0) });
var prop = new PropertyDeclaration("Count", new TypeReference("int")) { Visibility = Visibility.Public, HasSetter = true };
prop.GetterBody.Add(new ReturnStatement(new VariableReference("count")));
prop.SetterBody.Add(new AssignmentStatement(new VariableReference("count"), new VariableReference("value")));
cls.Members.Add(prop);
new CppGenerator().Generate(cls);

emits

class Counter
{
private:
    int count = 0;

public:
    int count() const
    {
        return count;
    }
    void set_count(int value) ...

and g++ -std=c++20 -fsyntax-only reports:

error: 'int Counter::count() const' conflicts with a previous declaration
note: previous declaration 'int Counter::count'

The same AST generates valid code for every other target. C# and Python keep Count and count apart, Rust allows a field and a method with the same name, Go writes Count(), and C prefixes the name as Counter_count. Only C++ fails.

Why it matters

A class written in the ordinary C# style can't be emitted as C++. C++ is also the target that downstream consumers, such as ktsu.Semantics' C++ projection, generate headers with.

Suggested fix / acceptance criteria

  • When lowering a property, check the generated getter and setter names against the class's other member names. On a clash, choose a spelling that doesn't collide (for example get_count()) and apply it deterministically.
  • Add a CppGeneratedSourceCompilesTests case: field count plus a property Count with bodies compiles, and a driver can call the getter and setter.
  • Pin the chosen spelling in CppGeneratorTests.

Activity

  1. added
    bug
    on Sep 30, 2026
  2. matt-edmondson commented on Oct 5, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    • Category: Bug
    • Priority: Medium. The ordinary C# shape (field count, property Count with bodies) emits C++ that does not compile. C++ is the target downstream header generation uses. It affects only C++.
    • Area: StandardLanguageGenerator.Separate, PropertyDeclaration.GetterName, CppGenerator member naming
    • Duplicates / in progress: No duplicate or open PR. Fix the #130–#137 generator/serializer cluster by cause: shared preamble, comment writer, typed YAML reads, and literal vocabulary #145 lists it as unrelated.
    • Next step: When a property is lowered, check the getter and setter names against sibling members and fall back deterministically (for example to get_count()). Add the compile test in CppGeneratedSourceCompilesTests and pin the spelling in CppGeneratorTests.

    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

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions