-
Notifications
You must be signed in to change notification settings - Fork 30
[WIP] Add support for EFP0013 diagnostic for unsupported expressions in projectable members #192
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
Open
koenbeuk
wants to merge
1
commit into
master
Choose a base branch
from
feat/csharp-feature-gaps
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+255
−7
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 5 additions & 1 deletion
6
src/EntityFrameworkCore.Projectables.Generator/AnalyzerReleases.Unshipped.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| | ||
| ### New Rules | ||
|
|
||
| | Rule ID | Category | Severity | Notes | | ||
| |---------|----------|----------|----------------------------------------------| | ||
| | EFP0013 | Design | Error | Unsupported expression in projectable member | |
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
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
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
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
137 changes: 137 additions & 0 deletions
137
tests/EntityFrameworkCore.Projectables.Generator.Tests/UnsupportedExpressionTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| using Microsoft.CodeAnalysis; | ||
| using Xunit; | ||
|
|
||
| namespace EntityFrameworkCore.Projectables.Generator.Tests; | ||
|
|
||
| /// <summary> | ||
| /// Tests that C# 12–14 syntax nodes that cannot be represented in expression trees cause the | ||
| /// generator to emit a clear EFP0013 diagnostic instead of silently producing broken generated code. | ||
| /// </summary> | ||
| public class UnsupportedExpressionTests : ProjectionExpressionGeneratorTestsBase | ||
| { | ||
| public UnsupportedExpressionTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { } | ||
|
|
||
| [Fact] | ||
| public void CollectionExpression_EmitsDiagnostic() | ||
| { | ||
| var compilation = CreateCompilation(@" | ||
| using System.Collections.Generic; | ||
| using EntityFrameworkCore.Projectables; | ||
|
|
||
| class Entity | ||
| { | ||
| [Projectable] | ||
| public List<int> Ids => [1, 2, 3]; | ||
| } | ||
| "); | ||
| var result = RunGenerator(compilation); | ||
|
|
||
| var diagnostic = Assert.Single(result.Diagnostics); | ||
| Assert.Equal("EFP0013", diagnostic.Id); | ||
| Assert.Equal(DiagnosticSeverity.Error, diagnostic.Severity); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IndexFromEndOperator_EmitsDiagnostic() | ||
| { | ||
| var compilation = CreateCompilation(@" | ||
| using EntityFrameworkCore.Projectables; | ||
|
|
||
| class Entity | ||
| { | ||
| public int[] Items { get; set; } | ||
|
|
||
| [Projectable] | ||
| public int Last => Items[^1]; | ||
| } | ||
| "); | ||
| var result = RunGenerator(compilation); | ||
|
|
||
| var diagnostic = Assert.Single(result.Diagnostics); | ||
| Assert.Equal("EFP0013", diagnostic.Id); | ||
| Assert.Equal(DiagnosticSeverity.Error, diagnostic.Severity); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RangeOperator_EmitsDiagnostic() | ||
| { | ||
| var compilation = CreateCompilation(@" | ||
| using EntityFrameworkCore.Projectables; | ||
|
|
||
| class Entity | ||
| { | ||
| public int[] Items { get; set; } | ||
|
|
||
| [Projectable] | ||
| public int[] Slice => Items[1..3]; | ||
| } | ||
| "); | ||
| var result = RunGenerator(compilation); | ||
|
|
||
| var diagnostic = Assert.Single(result.Diagnostics); | ||
| Assert.Equal("EFP0013", diagnostic.Id); | ||
| Assert.Equal(DiagnosticSeverity.Error, diagnostic.Severity); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void PrimaryConstructorParameter_EmitsDiagnostic() | ||
| { | ||
| var compilation = CreateCompilation(@" | ||
| using EntityFrameworkCore.Projectables; | ||
|
|
||
| class Entity(int id) | ||
| { | ||
| [Projectable] | ||
| public int DoubledId => id * 2; | ||
| } | ||
| "); | ||
| var result = RunGenerator(compilation); | ||
|
|
||
| var diagnostic = Assert.Single(result.Diagnostics); | ||
| Assert.Equal("EFP0013", diagnostic.Id); | ||
| Assert.Equal(DiagnosticSeverity.Error, diagnostic.Severity); | ||
| } | ||
|
|
||
| #if NET9_0_OR_GREATER | ||
| [Fact] | ||
| public void RefStructParameter_EmitsDiagnostic() | ||
| { | ||
| var compilation = CreateCompilation(@" | ||
| using System; | ||
| using EntityFrameworkCore.Projectables; | ||
|
|
||
| static class Extensions | ||
| { | ||
| [Projectable] | ||
| public static int Sum(params ReadOnlySpan<int> values) => 0; | ||
| } | ||
| "); | ||
| var result = RunGenerator(compilation); | ||
|
|
||
| var diagnostic = Assert.Single(result.Diagnostics); | ||
| Assert.Equal("EFP0013", diagnostic.Id); | ||
| Assert.Equal(DiagnosticSeverity.Error, diagnostic.Severity); | ||
| } | ||
| #endif | ||
|
|
||
| #if NET10_0_OR_GREATER | ||
| [Fact] | ||
| public void FieldKeyword_EmitsDiagnostic() | ||
| { | ||
| var compilation = CreateCompilation(@" | ||
| using EntityFrameworkCore.Projectables; | ||
|
|
||
| class Entity | ||
| { | ||
| [Projectable] | ||
| public string Name { get => field ?? ""default""; set; } | ||
| } | ||
| "); | ||
| var result = RunGenerator(compilation); | ||
|
|
||
| var diagnostic = Assert.Single(result.Diagnostics); | ||
| Assert.Equal("EFP0013", diagnostic.Id); | ||
| Assert.Equal(DiagnosticSeverity.Error, diagnostic.Severity); | ||
| } | ||
| #endif | ||
| } |
Oops, something went wrong.
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.
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.
Reporting EFP0013 for ref-like parameter types is good, but the generator still proceeds to generate
Expression<Func<...>>using that ref-like type in theFunc<...>type arguments. Ref-like types (e.g.ReadOnlySpan<T>) cannot be used as generic arguments, so this will cause additional compiler errors in the generated.g.csbeyond EFP0013, undermining the goal of a single clear diagnostic. Consider short-circuiting descriptor generation for this member (returnfalsefrom the body processor /GetDescriptor) or rewriting the parameter type to a safe placeholder solely to keep generated code compiling when EFP0013 is emitted.