Commit 31b9af3
fix(csharp-codegen): escape C# reserved keywords in generated identifiers (#4535)
## 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:
```csharp
[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:
```csharp
// 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
---------
Co-authored-by: Stable Genius <259448942+stablegenius49@users.noreply.github.com>
Co-authored-by: Ryan <r.ekhoff@clockworklabs.io>1 parent 6300236 commit 31b9af3
4 files changed
Lines changed: 188 additions & 69 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
422 | 422 | | |
423 | 423 | | |
424 | 424 | | |
| 425 | + | |
| 426 | + | |
425 | 427 | | |
426 | 428 | | |
427 | 429 | | |
| |||
431 | 433 | | |
432 | 434 | | |
433 | 435 | | |
434 | | - | |
| 436 | + | |
435 | 437 | | |
436 | 438 | | |
437 | 439 | | |
| |||
442 | 444 | | |
443 | 445 | | |
444 | 446 | | |
445 | | - | |
| 447 | + | |
446 | 448 | | |
447 | 449 | | |
448 | 450 | | |
| |||
462 | 464 | | |
463 | 465 | | |
464 | 466 | | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
465 | 473 | | |
466 | 474 | | |
467 | 475 | | |
| |||
557 | 565 | | |
558 | 566 | | |
559 | 567 | | |
560 | | - | |
| 568 | + | |
561 | 569 | | |
562 | 570 | | |
563 | 571 | | |
| |||
569 | 577 | | |
570 | 578 | | |
571 | 579 | | |
572 | | - | |
| 580 | + | |
573 | 581 | | |
574 | 582 | | |
575 | | - | |
| 583 | + | |
576 | 584 | | |
577 | 585 | | |
578 | 586 | | |
| |||
585 | 593 | | |
586 | 594 | | |
587 | 595 | | |
588 | | - | |
| 596 | + | |
589 | 597 | | |
590 | 598 | | |
591 | 599 | | |
| |||
597 | 605 | | |
598 | 606 | | |
599 | 607 | | |
600 | | - | |
| 608 | + | |
601 | 609 | | |
602 | | - | |
| 610 | + | |
603 | 611 | | |
604 | 612 | | |
605 | 613 | | |
| |||
615 | 623 | | |
616 | 624 | | |
617 | 625 | | |
618 | | - | |
| 626 | + | |
619 | 627 | | |
620 | 628 | | |
621 | 629 | | |
| |||
634 | 642 | | |
635 | 643 | | |
636 | 644 | | |
637 | | - | |
| 645 | + | |
638 | 646 | | |
639 | 647 | | |
640 | 648 | | |
641 | 649 | | |
642 | 650 | | |
643 | 651 | | |
644 | | - | |
| 652 | + | |
645 | 653 | | |
646 | 654 | | |
647 | 655 | | |
| |||
661 | 669 | | |
662 | 670 | | |
663 | 671 | | |
664 | | - | |
| 672 | + | |
665 | 673 | | |
666 | 674 | | |
667 | 675 | | |
| |||
680 | 688 | | |
681 | 689 | | |
682 | 690 | | |
683 | | - | |
| 691 | + | |
684 | 692 | | |
685 | 693 | | |
686 | 694 | | |
| |||
735 | 743 | | |
736 | 744 | | |
737 | 745 | | |
738 | | - | |
| 746 | + | |
739 | 747 | | |
740 | 748 | | |
741 | 749 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
42 | 43 | | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
43 | 56 | | |
44 | 57 | | |
45 | 58 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
256 | 256 | | |
257 | 257 | | |
258 | 258 | | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
259 | 340 | | |
260 | 341 | | |
261 | 342 | | |
| |||
0 commit comments