-
Notifications
You must be signed in to change notification settings - Fork 2
Use unsafe accessor initialize readonly member #149
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
Use unsafe accessor initialize readonly member #149
Conversation
Introduces LIGHT_PROTO_W001 diagnostic to warn when a value type member has a default value that may cause unexpected deserialization results if SkipConstructor is not set. Updates generator logic to report this warning, adds documentation, and adjusts related tests to suppress or handle the warning.
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 pull request enhances the LightProto code generator to better handle readonly and init-only members, especially in .NET 8+. It introduces new diagnostics for default values that may cause deserialization issues, improves error handling, and adds support for using UnsafeAccessor to initialize readonly members on .NET 8+.
Key changes:
- Added new warning
LIGHT_PROTO_W001to alert users when value type members have default values that may break deserialization - Implemented UnsafeAccessor-based initialization for readonly/init-only members on .NET 8+, with fallback behavior on earlier versions
- Improved error messages and added validation for parameterless constructors
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 10 comments.
Show a summary per file
| File | Description |
|---|---|
| src/LightProto.Generator/LightProtoGenerator.cs | Core generator changes: added SourceProductionContext threading, UnsafeAccessor implementation, new warning diagnostic, improved error handling |
| docs/Diagnostic.md | Documentation for new LIGHT_PROTO_W001 warning with examples and fixes |
| tests/LightProto.Tests/Parsers/SkipConstructorWithReadonlyFieldTests.cs | New test for SkipConstructor with readonly fields |
| tests/LightProto.Tests/Parsers/SkipConstructorFalseWithReadonlyFieldTests.cs | New test for normal constructor with readonly fields |
| tests/LightProto.Tests/Parsers/SkipConstructorWithInitializerTests.cs | Removed NET7_0_OR_GREATER conditional compilation (not needed for this test) |
| tests/LightProto.Tests/Parsers/SkipConstructorTests.cs | Removed NET7_0_OR_GREATER conditional compilation (not needed for this test) |
| tests/LightProto.Tests/Parsers/InitializerTests.cs | Added suppression for LIGHT_PROTO_W001 warning |
| tests/LightProto.Tests/CsPackage.cs | Added null-conditional operators for safer StringBuilder comparison |
| tests/TestAot/TestAot.csproj | Added protobuf-net package reference for compatibility testing |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
tests/LightProto.Tests/Parsers/SkipConstructorWithReadonlyFieldTests.cs
Outdated
Show resolved
Hide resolved
tests/LightProto.Tests/Parsers/SkipConstructorFalseWithReadonlyFieldTests.cs
Outdated
Show resolved
Hide resolved
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #149 +/- ##
=======================================
Coverage 98.39% 98.39%
=======================================
Files 99 99
Lines 2186 2186
Branches 231 231
=======================================
Hits 2151 2151
Misses 23 23
Partials 12 12 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This comment has been minimized.
This comment has been minimized.
Renamed Diagnostic.md to Diagnostics.md and fixed typos in the documentation. Updated LightProtoGenerator to improve boolean expression clarity, error messages, and help link references. Changed test preprocessor directives from NET7_0_OR_GREATER to NET8_0_OR_GREATER for relevant test files.
|
This pull request introduces diagnostics and improved handling for default values in proto members, especially focusing on deserialization issues that can arise when members have default values. It adds a new warning (
LIGHT_PROTO_W001), updates the code generation logic to report this warning, and enhances error handling for readonly and init-only members. There are also some improvements and bug fixes for .NET 8+ compatibility and readonly member assignment.Key changes include:
Diagnostics and Warnings:
LIGHT_PROTO_W001, which informs users that members with default values may break deserialization, and suggests solutions.LIGHT_PROTO_W001as a diagnostic warning when a value type proto member has a default value andSkipConstructoris not set. [1] [2]LIGHT_PROTO_W001inInitializerTeststo avoid test noise.Code Generation and Error Handling Improvements:
SourceProductionContextfor improved diagnostic reporting and error handling throughout proto contract and member processing. [1] [2] [3] [4] [5] [6] [7].NET 8+ Compatibility:
UnsafeAccessorfor assigning readonly members on .NET 8+, and to fallback to previous mechanisms on earlier versions. [1] [2] [3] [4] [5]Bug Fixes and Minor Improvements:
StringBuilderField.These changes collectively improve the robustness of the code generator, provide clearer diagnostics to users, and ensure better compatibility and safety across different .NET versions.
Fixes: #144
Fixes: #143