Support block-bodied members with [Projectable] attribute#152
Support block-bodied members with [Projectable] attribute#152
Conversation
- Created BlockStatementConverter to transform block bodies to expressions - Added support for simple return statements - Added support for if-else statements (converted to ternary) - Added support for local variable declarations (inlined) - Added diagnostics for unsupported statements (EFP0003) - Added comprehensive test cases - Updated existing test that expected block methods to fail Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
- Created 7 functional tests demonstrating EF Core SQL translation - Added comprehensive documentation explaining feature, limitations, and benefits - All 174 tests passing across all projects Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
|
@copilot add support for nested if else, and more complex conditions. support absence of else too. |
- Support if statements without else clause (uses default or fallback return)
- Support switch statements (converted to nested conditionals)
- Handle if { return x; } return y; pattern
- Added 5 generator tests and 4 functional tests
- Updated documentation with new features and SQL examples
- All 182 tests passing (84 generator + 76 functional + 22 unit)
Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Done! ✅ All requested features implemented in commit 7067bc2: 1. Nested if-else - Already supported, confirmed working
4. Switch statements - Now supported with:
Added 9 new tests (5 generator + 4 functional) demonstrating all features. All 182 tests passing. |
- Rename test to better reflect implicit return pattern - Add clarifying comment about control flow in BlockStatementConverter - All tests still passing Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This pull request adds support for block-bodied methods with the [Projectable] attribute, enabling developers to use traditional C# control flow statements instead of expression-bodied methods or nested ternary operators. The source generator now transforms supported block statements (if/else, switch, local variables) into expression trees that can be translated to SQL by Entity Framework Core.
Changes:
- Introduced
BlockStatementConverterclass to transform block statements into expression syntax - Updated
ProjectableInterpreterto detect and route block-bodied methods through the new converter - Added EFP0003 diagnostic warning for unsupported statements in block-bodied methods
- Comprehensive test coverage with 14 generator tests and 11 functional tests verifying correct SQL generation
- Added detailed documentation in
BlockBodiedMethods.md
Reviewed changes
Copilot reviewed 52 out of 52 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/EntityFrameworkCore.Projectables.Generator/BlockStatementConverter.cs | New converter class that transforms if/else, switch statements, and local variables into expression trees |
| src/EntityFrameworkCore.Projectables.Generator/ProjectableInterpreter.cs | Integration logic to detect block-bodied methods and route them to BlockStatementConverter |
| src/EntityFrameworkCore.Projectables.Generator/Diagnostics.cs | Added EFP0003 warning diagnostic for unsupported statements |
| src/EntityFrameworkCore.Projectables.Generator/AnalyzerReleases.Unshipped.md | Documentation of new EFP0003 diagnostic |
| tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs | 14 new generator tests covering various block-bodied patterns and one updated test |
| tests/EntityFrameworkCore.Projectables.Generator.Tests/*.verified.txt | Verified snapshots of generated code for each test case |
| tests/EntityFrameworkCore.Projectables.FunctionalTests/BlockBodiedMethodTests.cs | 11 functional tests verifying SQL generation for different patterns |
| tests/EntityFrameworkCore.Projectables.FunctionalTests/*.verified.txt | Verified SQL output for multiple .NET versions |
| docs/BlockBodiedMethods.md | Comprehensive documentation with examples, limitations, and SQL output samples |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/EntityFrameworkCore.Projectables.Generator/BlockStatementConverter.cs
Outdated
Show resolved
Hide resolved
src/EntityFrameworkCore.Projectables.Generator/BlockStatementConverter.cs
Show resolved
Hide resolved
src/EntityFrameworkCore.Projectables.Generator/BlockStatementConverter.cs
Outdated
Show resolved
Hide resolved
src/EntityFrameworkCore.Projectables.Generator/BlockStatementConverter.cs
Outdated
Show resolved
Hide resolved
…tion # Conflicts: # tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 55 out of 55 changed files in this pull request and generated 5 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/EntityFrameworkCore.Projectables.Generator/BlockStatementConverter.cs
Show resolved
Hide resolved
src/EntityFrameworkCore.Projectables.Generator/BlockStatementConverter.cs
Show resolved
Hide resolved
src/EntityFrameworkCore.Projectables.Generator/BlockStatementConverter.cs
Show resolved
Hide resolved
src/EntityFrameworkCore.Projectables.Generator/BlockStatementConverter.cs
Show resolved
Hide resolved
|
@copilot apply suggested changes and adapt tests |
- Add diagnostic for empty block bodies (EFP0003) - Fix transitive local variable inlining (var a = 1; var b = a + 2; now fully expands) - Add warning when local variables are used multiple times (semantics preservation) - Prevent locals in nested blocks from leaking into outer scopes - Fix documentation to show compilable C# code (no implicit returns) - Add tests for transitive locals and nested block restrictions - All 197 tests passing (96 net8.0 + 101 net10.0) Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
All code review suggestions applied in commit e2b1fad: ✅ Empty block diagnostic - Now reports EFP0003 when block is empty Added 2 new tests verifying transitive inlining and nested block restrictions. All 197 tests passing. |
|
@copilot you changed the summary and the title of this PR to reflect only pattern matching changes, it was wrong, the subject of this PR is support for block bodied methods. Please extract all the changes related to the improvements for the pattern matching support into a new Pull request pointing to the branch of this PR. |
I understand you want to separate pattern matching changes into a new PR. However, I don't have the ability to create new PRs or branches in GitHub. Here's what I can do:
You would then need to manually:
Would you like me to proceed with reverting the pattern matching changes from this branch and updating the PR description? |
|
@copilot let's do this, I created the new branch |
Pattern matching support has been moved to a separate branch/PR. This PR now focuses solely on block-bodied method support: - If-else statements - Switch statements - Local variables - Side effect detection Reverted commits: - adc95f5: Add documentation for pattern matching support - 31f4267: Fix pattern matching support in block-bodied methods - f2a805e: Initial exploration - understand pattern matching crash issue All 209 tests passing (102 net8.0 + 107 net10.0 generator tests, plus functional and unit tests) Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Done! Reverted pattern matching commits in 5f83f40. This PR now focuses solely on block-bodied method support (if-else, switch, local variables, side effects). All 209 tests passing. The following pattern matching commits were removed and should be cherry-picked to the new branch: |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 150 out of 150 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/EntityFrameworkCore.Projectables.Generator/BlockStatementConverter.cs
Outdated
Show resolved
Hide resolved
src/EntityFrameworkCore.Projectables.Generator/BlockStatementConverter.cs
Outdated
Show resolved
Hide resolved
src/EntityFrameworkCore.Projectables.Generator/BlockStatementConverter.cs
Show resolved
Hide resolved
|
@copilot handle suggestions and update readme file with the new feature, with a link to the the new markdown for details. Remove SideEffectDetection.md and add a summary of it in the block bodies methods md file. |
Code Review Fixes: - Use proper type resolution for [Projectable] attribute check (SymbolEqualityComparer) - Add parentheses when inlining local variables to preserve operator precedence - Restrict multiple if-without-else pattern to simple return bodies Documentation Updates: - Add block-bodied methods FAQ section to README with examples - Merge SideEffectDetection.md content into BlockBodiedMethods.md - Remove standalone SideEffectDetection.md file - Link to BlockBodiedMethods.md from README All 209 tests passing Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
All code review suggestions addressed and documentation updated in commit 8b144b5: Code Review Fixes: Documentation Updates: All 209 tests passing. |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 158 out of 158 changed files in this pull request and generated 6 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Support block-bodied members with [Projectable] attribute
Source generator now accepts block-bodied members in addition to expression-bodied, transforming supported control flow to expression trees.
For now it is experimental and requires to set AllowBlockBody = true in the Projectables attribute.
Implementation
BlockStatementConverter: Transforms block statements to expressions
ProjectableInterpreter: Detects method body type (expression vs block) and routes appropriately
Diagnostics:
Example
Both generate identical SQL:
Additional Examples
Switch Statement Support
Generates optimized SQL:
If Without Else Support
Supported patterns
Side Effect Detection
The generator now detects and reports specific side effects with precise error messages:
See Block-Bodied Methods Documentation for complete details.
Test coverage
Documentation
docs/BlockBodiedMethods.md: Complete guide for block-bodied method support (includes side effect detection)Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.