-
Notifications
You must be signed in to change notification settings - Fork 547
[RGen] Ensure that if a parameter name is a keyword that '@' is used as a prefix. #22930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…as a prefix. In C#, the @ symbol is used as an escape character to allow reserved keywords (like event) to be used as identifiers. However, when working with Roslyn's IParameterSymbol, the Name property returns the identifier without the @ prefix because the @ is only required in the source code to escape the keyword. Internally, the compiler treats the name as the unescaped version. The data model has to take this into account and re-escape the keyword so that we can use the same parameter name as the source code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR ensures that parameter names which are C# keywords are escaped with @
when generating source code.
- Added
GetSaveName
extension to prefix@
for keyword parameter names. - Updated transformer and generator code to use
GetSaveName
instead ofsymbol.Name
. - Added tests and updated project files to include the new extension.
Reviewed Changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.
Show a summary per file
File | Description |
---|---|
tests/rgen/.../FromDeclarationTests.cs | Added test case for method parameter named @event . |
tests/rgen/.../DelegateInfoTests.cs | Added test case for delegate parameter named @event . |
src/rgen/Microsoft.Macios.Transformer/Microsoft.Macios.Transformer.csproj | Included ParameterSymbolExtensions.cs in Transformer build. |
src/rgen/Microsoft.Macios.Transformer/DataModel/Parameter.Transformer.cs | Switched symbol.Name to symbol.GetSaveName() . |
src/rgen/Microsoft.Macios.Transformer/DataModel/DelegateParameter.Transformer.cs | Switched symbol.Name to symbol.GetSaveName() . |
src/rgen/Microsoft.Macios.Generator/Extensions/ParameterSymbolExtensions.cs | Added extension method GetSaveName . |
src/rgen/Microsoft.Macios.Generator/DataModel/Parameter.Generator.cs | Switched symbol.Name to symbol.GetSaveName() . |
src/rgen/Microsoft.Macios.Generator/DataModel/DelegateParameter.Generator.cs | Switched symbol.Name to symbol.GetSaveName() . |
src/rgen/Microsoft.Macios.Bindings.Analyzer/Microsoft.Macios.Bindings.Analyzer.csproj | Included ParameterSymbolExtensions.cs in Analyzer build. |
Comments suppressed due to low confidence (3)
src/rgen/Microsoft.Macios.Generator/Extensions/ParameterSymbolExtensions.cs:18
- [nitpick] The method is documented as returning a "safe name", but is named
GetSaveName
. Consider renaming it toGetSafeName
to match the documentation and improve clarity.
public static string GetSaveName (this IParameterSymbol symbol)
src/rgen/Microsoft.Macios.Generator/DataModel/Parameter.Generator.cs:41
- The
GetSaveName
extension method is defined inMicrosoft.Macios.Generator.Extensions
, but this file doesn't import that namespace. Addusing Microsoft.Macios.Generator.Extensions;
at the top to ensure it resolves.
parameter = new (symbol.Ordinal, new (symbol.Type, context.Compilation), symbol.GetSaveName ()) {
src/rgen/Microsoft.Macios.Generator/DataModel/DelegateParameter.Generator.cs:21
- The
GetSaveName
extension is inMicrosoft.Macios.Generator.Extensions
, but this file lacks thatusing
directive. Please addusing Microsoft.Macios.Generator.Extensions;
at the top.
parameter = new (symbol.Ordinal, new (symbol.Type), symbol.GetSaveName ()) {
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
✅ [CI Build #c6c4c53] Build passed (Build packages) ✅Pipeline on Agent |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
✅ [CI Build #b11c676] Build passed (Build macOS tests) ✅Pipeline on Agent |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
💻 [CI Build #b11c676] Tests on macOS X64 - Mac Sonoma (14) passed 💻✅ All tests on macOS X64 - Mac Sonoma (14) passed. Pipeline on Agent |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
💻 [CI Build #b11c676] Tests on macOS M1 - Mac Monterey (12) passed 💻✅ All tests on macOS M1 - Mac Monterey (12) passed. Pipeline on Agent |
💻 [CI Build #b11c676] Tests on macOS M1 - Mac Ventura (13) passed 💻✅ All tests on macOS M1 - Mac Ventura (13) passed. Pipeline on Agent |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
✅ [PR Build #c6c4c53] Build passed (Detect API changes) ✅Pipeline on Agent |
This comment has been minimized.
This comment has been minimized.
✅ API diff for current PR / commit.NET ( No breaking changes )✅ API diff vs stable.NET ( No breaking changes )ℹ️ Generator diffGenerator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes) Pipeline on Agent |
🚀 [CI Build #7d4f813] Test results 🚀Test results✅ All tests passed on VSTS: test results. 🎉 All 122 tests passed 🎉 Tests counts✅ cecil: All 1 tests passed. Html Report (VSDrops) Download Pipeline on Agent |
This comment has been minimized.
This comment has been minimized.
💻 [CI Build #b11c676] Tests on macOS arm64 - Mac Sequoia (15) passed 💻✅ All tests on macOS arm64 - Mac Sequoia (15) passed. Pipeline on Agent |
In C#, the @ symbol is used as an escape character to allow reserved keywords (like event) to be used as identifiers. However, when working with Roslyn's IParameterSymbol, the Name property returns the identifier without the @ prefix because the @ is only required in the source code to escape the keyword. Internally, the compiler treats the name as the unescaped version. The data model has to take this into account and re-escape the keyword so that we can use the same parameter name as the source code.