Skip to content

Lift try..catch out of an expression and replace with a temporary variable#34912

Open
ChrisJollyAU wants to merge 3 commits into
dotnet:mainfrom
ChrisJollyAU:fix34393
Open

Lift try..catch out of an expression and replace with a temporary variable#34912
ChrisJollyAU wants to merge 3 commits into
dotnet:mainfrom
ChrisJollyAU:fix34393

Conversation

@ChrisJollyAU

@ChrisJollyAU ChrisJollyAU commented Oct 15, 2024

Copy link
Copy Markdown
Contributor

When EnableDetailedErrors is turned on it wraps the expression to read the field value in a try...catch statement

In certain situations this is not valid C#

  • When the expression is an argument to a method call
  • Assignment of the right hand expression to a variable or instance member
  • When the expression is part of a list
    • List of expressions for array initialize
    • List of expressions for the constructor with a NewExpression

Also fixed some of the catch handling

  • ThrowExtractJsonPropertyException needs to be public for it to be called (like ThrowReadValueException already is)
  • One place needed to refer to property.Name rather than just property
  • There is a case where property is null so handle that for the call to ThrowReadValueException as well

Fixes #34393

@roji

roji commented Oct 16, 2024

Copy link
Copy Markdown
Member

@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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 LinqToCSharpSyntaxTranslator to translate TryExpression in expression contexts by hoisting it into statements via a temporary variable.
  • Fix detailed-errors exception handling in relational shaper generation (null property handling, 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.

Comment thread src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs
@AndriySvyryd AndriySvyryd marked this pull request as draft June 19, 2026 19:04
ChrisJollyAU and others added 2 commits June 23, 2026 17:43
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@ChrisJollyAU ChrisJollyAU marked this pull request as ready for review June 23, 2026 10:37
@ChrisJollyAU

Copy link
Copy Markdown
Contributor Author

Ok. Implemented the suggestion and merged the main so that i could test on latest code. All passes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

System.NotImplementedException for Precompiled Query with detailed errors enabled

4 participants