Skip to content

fix(csharp-codegen): escape C# reserved keywords in generated identifiers#4535

Open
stablegenius49 wants to merge 2 commits intoclockworklabs:masterfrom
stablegenius49:fix/csharp-escape-keywords-4529
Open

fix(csharp-codegen): escape C# reserved keywords in generated identifiers#4535
stablegenius49 wants to merge 2 commits intoclockworklabs:masterfrom
stablegenius49:fix/csharp-escape-keywords-4529

Conversation

@stablegenius49
Copy link

Summary

This PR fixes an issue where C# reserved keywords (like params, class, event, etc.) used as field or parameter names in SpacetimeDB types would cause compilation errors in the generated code.

Problem

When a user defines a table or reducer with a field named using a C# reserved keyword:

[SpacetimeDB.Table]
public partial struct MyTable
{
    public int @params;  // User escapes it in their code
    public string @class;
}

The codegen would generate invalid C# like:

// Generated code (broken)
public int params;  // Error: keyword used as identifier
public void Read(BinaryReader reader) {
    params = ...;  // Error
}

Solution

  1. Added an Identifier property to MemberDeclaration in the codegen that automatically detects C# reserved keywords using SyntaxFacts.GetKeywordKind() and prefixes them with @ when needed.

  2. Updated all code generation sites to use Identifier instead of Name when generating:

    • Field declarations
    • Property accessors
    • Constructor parameters
    • BSATN serialization code
    • Index accessors
    • Reducer/procedure parameters
  3. Added a regression test that verifies tables, reducers, and procedures with keyword field names compile successfully.

Test Plan

  • Added CSharpKeywordIdentifiersAreEscapedInGeneratedCode test that creates a table with @class and @params fields, plus a reducer and procedure with keyword parameters
  • Existing tests continue to pass (verified locally with FormerlyForbiddenFieldNames fixture which already tests edge cases like Read, Write, GetAlgebraicType)

Fixes #4529

…iers

- Add Identifier property to MemberDeclaration that prefixes C# keywords with @

- Use SyntaxFacts.GetKeywordKind to detect reserved keywords

- Update all code generation sites to use Identifier instead of Name

- Add test for keyword field names (class, params) in tables/reducers/procedures

Fixes clockworklabs#4529
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@bfops bfops requested a review from rekhoff March 3, 2026 18:06
@bfops
Copy link
Collaborator

bfops commented Mar 3, 2026

Thank you for opening a PR for this! We'll work on getting it reviewed.

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.

[C#] Parameter with name params and class should be prefixed with @

3 participants