Add support for nullable enums in expressive properties#61
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
There was a problem hiding this comment.
Pull request overview
Adds generator + integration coverage for expanding expressive properties involving nullable enum method calls (e.g., Trend?.ToString() and nullable-enum extension methods) so these can be expressed as expandable expression trees.
Changes:
- Update enum method expansion emission to lift per-arm enum constants to
Nullable<TEnum>when the receiver is nullable. - Add an integration test covering
ToString()on a nullable enum within an[Expressive]property. - Add generator verification tests + verified outputs for nullable-enum
ToString()and nullable-enum extension method expansion.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/ExpressiveSharp.IntegrationTests/Tests/EnumComparisonTests.cs | Adds runtime test and expressive property for ToString() on nullable enum. |
| tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/EnumTests.cs | Adds generator tests for nullable-enum ToString() and nullable-enum extension method expansion. |
| tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/EnumTests.ExpandToStringOnNullableEnum.verified.txt | Adds golden output for nullable-enum ToString() expansion. |
| tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/EnumTests.ExpandExtensionMethodOnNullableEnum.verified.txt | Adds golden output for nullable-enum extension method expansion. |
| src/ExpressiveSharp.Generator/Emitter/ExpressionTreeEmitter.cs | Lifts enum constants to nullable receiver type during enum method expansion. |
Comments suppressed due to low confidence (1)
src/ExpressiveSharp.Generator/Emitter/ExpressionTreeEmitter.cs:823
- For nullable enum receivers, the null wrapper currently returns
defaultVarwhen the receiver is null. This changes semantics for methods that have a defined null behavior, e.g.Nullable<TEnum>.ToString()(returns "" when HasValue is false) and extension methods like the newTrend?.Describe()example (should return "n/a" on null, but the expansion yields null). Instead of returningdefaultVarfor the null branch, compute the call result for anullreceiver/argument (using aConstant(null, typeof(Nullable<TEnum>))as the receiver or first argument, matching static vs instance) and return that for the null branch so the expansion preserves runtime behavior.
// For Nullable<TEnum>, wrap in: receiver == null ? default : chain
if (isNullable)
{
var nullConst = NextVar();
AppendLine($"var {nullConst} = {Expr}.Constant(null, typeof({receiverTypeFqn}));");
var nullCheck = NextVar();
AppendLine($"var {nullCheck} = {Expr}.Equal({receiverVar}, {nullConst});");
var wrappedVar = NextVar();
AppendLine($"var {wrappedVar} = {Expr}.Condition({nullCheck}, {defaultVar}, {currentVar}, typeof({returnTypeFqn}));");
currentVar = wrappedVar;
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+362
to
+391
| [TestMethod] | ||
| public Task ExpandExtensionMethodOnNullableEnum() | ||
| { | ||
| var compilation = CreateCompilation( | ||
| """ | ||
| namespace Foo { | ||
| public enum Trend { Increase, Decrease, NoChange } | ||
|
|
||
| public static class TrendExtensions | ||
| { | ||
| public static string Describe(this Trend? value) => | ||
| value.HasValue ? value.Value.ToString() : "n/a"; | ||
| } | ||
|
|
||
| public record Entity | ||
| { | ||
| public Trend? Trend { get; init; } | ||
|
|
||
| [Expressive] | ||
| public string TrendLabel => Trend.Describe(); | ||
| } | ||
| } | ||
| """); | ||
| var result = RunExpressiveGenerator(compilation); | ||
|
|
||
| Assert.AreEqual(0, result.Diagnostics.Length); | ||
| Assert.AreEqual(1, result.GeneratedTrees.Length); | ||
|
|
||
| return Verifier.Verify(result.GeneratedTrees[0].ToString()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixes #59