Lift try..catch out of an expression and replace with a temporary variable#34912
Lift try..catch out of an expression and replace with a temporary variable#34912ChrisJollyAU wants to merge 3 commits into
Conversation
|
@ChrisJollyAU thanks for submitting this - this is generally a non-trivial part of EF. Just to set expectations, it will probably be quite a while before we're able to review this - we're currently heads-down in 9.0 stabilization, docs and various other activities... But we definitely plan to do substantial work on (and hopefully finalize) the NativeAOT support in 10, and this would be part of that. |
249ae47 to
6b86657
Compare
There was a problem hiding this comment.
Pull request overview
This PR addresses precompiled query code generation failures when EnableDetailedErrors injects try/catch into expression trees in contexts where a TryExpression cannot be emitted as valid C# (e.g., inside method arguments, assignments, and expression lists). It also adjusts detailed-error exception helpers and a couple of property-related bugs to ensure the generated code compiles and throws the intended exceptions.
Changes:
- Add lifting support in
LinqToCSharpSyntaxTranslatorto translateTryExpressionin expression contexts by hoisting it into statements via a temporary variable. - Fix detailed-errors exception handling in relational shaper generation (null
propertyhandling, correct property name usage, and JSON exception helper signature). - Enable detailed errors in the SQL Server precompiled query functional test fixture to exercise the new translation path.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| test/EFCore.SqlServer.FunctionalTests/Query/PrecompiledQuerySqlServerTest.cs | Enables detailed errors in the fixture to reproduce/cover the TryExpression translation scenario in generated precompiled code. |
| src/EFCore.Relational/Query/RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.cs | Adjusts detailed-errors try/catch generation (property-null handling and JSON error helper call-site changes). |
| src/EFCore.Relational/Query/RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.ClientMethods.cs | Makes JSON extraction exception helper callable from generated code by updating accessibility/signature and adding internal-API annotation/docs. |
| src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs | Implements lifting of TryExpression out of expression contexts to valid statement-based C# using a temp local. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
Ok. Implemented the suggestion and merged the main so that i could test on latest code. All passes |
When
EnableDetailedErrorsis turned on it wraps the expression to read the field value in a try...catch statementIn certain situations this is not valid C#
NewExpressionAlso fixed some of the catch handling
ThrowExtractJsonPropertyExceptionneeds to be public for it to be called (likeThrowReadValueExceptionalready is)property.Namerather than justpropertypropertyis null so handle that for the call toThrowReadValueExceptionas wellFixes #34393