@@ -22,7 +22,7 @@ namespace Microsoft.EntityFrameworkCore.Query.Internal;
22
22
/// any release. You should only use it directly in your code with extreme caution and knowing that
23
23
/// doing so can result in application failures when updating to a new Entity Framework Core release.
24
24
/// </summary>
25
- public class LinqToCSharpSyntaxTranslator : ExpressionVisitor
25
+ public class LinqToCSharpSyntaxTranslator ( SyntaxGenerator syntaxGenerator ) : ExpressionVisitor
26
26
{
27
27
private sealed record StackFrame (
28
28
Dictionary < ParameterExpression , string > Variables ,
@@ -38,7 +38,7 @@ private readonly Stack<StackFrame> _stack
38
38
private sealed record LiftedState
39
39
{
40
40
internal readonly List < StatementSyntax > Statements = [ ] ;
41
- internal readonly Dictionary < ParameterExpression , string > Variables = new ( ) ;
41
+ internal readonly Dictionary < ParameterExpression , string > Variables = [ ] ;
42
42
internal readonly HashSet < string > VariableNames = [ ] ;
43
43
internal readonly List < LocalDeclarationStatementSyntax > UnassignedVariableDeclarations = [ ] ;
44
44
@@ -65,25 +65,16 @@ internal LiftedState CreateChild()
65
65
66
66
private readonly HashSet < ParameterExpression > _capturedVariables = [ ] ;
67
67
private ISet < string > _collectedNamespaces = null ! ;
68
- private readonly Dictionary < MethodBase , MethodDeclarationSyntax > _methodUnsafeAccessors = new ( ) ;
69
- private readonly Dictionary < ( FieldInfo Field , bool ForWrite ) , MethodDeclarationSyntax > _fieldUnsafeAccessors = new ( ) ;
68
+ private readonly Dictionary < MethodBase , MethodDeclarationSyntax > _methodUnsafeAccessors = [ ] ;
69
+ private readonly Dictionary < ( FieldInfo Field , bool ForWrite ) , MethodDeclarationSyntax > _fieldUnsafeAccessors = [ ] ;
70
70
71
71
private static MethodInfo ? _mathPowMethod ;
72
72
73
73
private readonly SideEffectDetectionSyntaxWalker _sideEffectDetector = new ( ) ;
74
74
private readonly ConstantDetectionSyntaxWalker _constantDetector = new ( ) ;
75
- private readonly SyntaxGenerator _g ;
75
+ private readonly SyntaxGenerator _g = syntaxGenerator ;
76
76
private readonly StringBuilder _stringBuilder = new ( ) ;
77
77
78
- /// <summary>
79
- /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
80
- /// the same compatibility standards as public APIs. It may be changed or removed without notice in
81
- /// any release. You should only use it directly in your code with extreme caution and knowing that
82
- /// doing so can result in application failures when updating to a new Entity Framework Core release.
83
- /// </summary>
84
- public LinqToCSharpSyntaxTranslator ( SyntaxGenerator syntaxGenerator )
85
- => _g = syntaxGenerator ;
86
-
87
78
/// <summary>
88
79
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
89
80
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
@@ -422,14 +413,9 @@ Expression VisitAssignment(BinaryExpression assignment, SyntaxKind kind)
422
413
423
414
// If the RHS was lifted out and the assignment lowering succeeded, Translate above returns the lowered assignment variable;
424
415
// this would mean that we return a useless identity assignment (i = i). Instead, just return it.
425
- if ( translatedRight == translatedLeft )
426
- {
427
- Result = translatedRight ;
428
- }
429
- else
430
- {
431
- Result = AssignmentExpression ( kind , translatedLeft , translatedRight ) ;
432
- }
416
+ Result = translatedRight == translatedLeft
417
+ ? translatedRight
418
+ : ( SyntaxNode ) AssignmentExpression ( kind , translatedLeft , translatedRight ) ;
433
419
434
420
return assignment ;
435
421
}
@@ -1474,7 +1460,7 @@ NameSyntax GenerateGenericType(Type type, List<TypeSyntax> genericArguments, int
1474
1460
SimpleNameSyntax nameSyntax = genericPartIndex <= 0
1475
1461
? IdentifierName ( type . Name )
1476
1462
: GenericName (
1477
- Identifier ( type . Name . Substring ( 0 , genericPartIndex ) ) ,
1463
+ Identifier ( type . Name [ .. genericPartIndex ] ) ,
1478
1464
TypeArgumentList ( SeparatedList ( genericArguments . Skip ( offset ) . Take ( length - offset ) ) ) ) ;
1479
1465
if ( type . DeclaringType == null )
1480
1466
{
@@ -1637,7 +1623,7 @@ when constantExpression.Type.Attributes.HasFlag(TypeAttributes.NestedPrivate)
1637
1623
{ Expression : null } => Generate ( member . Member . DeclaringType ! ) ,
1638
1624
1639
1625
// If the member is declared on an interface, add a cast up to it, to handle explicit interface implementation.
1640
- _ when member . Member . DeclaringType is { IsInterface : true }
1626
+ _ when member . Member . DeclaringType is { IsInterface : true } && member . Expression . Type != member . Member . DeclaringType
1641
1627
=> ParenthesizedExpression (
1642
1628
CastExpression ( Generate ( member . Member . DeclaringType ) , Translate < ExpressionSyntax > ( member . Expression ) ) ) ,
1643
1629
@@ -2212,7 +2198,7 @@ SyntaxList<StatementSyntax> ProcessArmBody(Expression body)
2212
2198
var result = translatedBody switch
2213
2199
{
2214
2200
BlockSyntax block => SingletonList < StatementSyntax > ( block . WithStatements ( block . Statements . Add ( BreakStatement ( ) ) ) ) ,
2215
- StatementSyntax s => List ( new [ ] { s , BreakStatement ( ) } ) ,
2201
+ StatementSyntax s => List ( [ s , BreakStatement ( ) ] ) ,
2216
2202
ExpressionSyntax e => List ( new StatementSyntax [ ] { ExpressionStatement ( e ) , BreakStatement ( ) } ) ,
2217
2203
2218
2204
_ => throw new ArgumentOutOfRangeException ( )
@@ -2379,14 +2365,14 @@ protected override Expression VisitTry(TryExpression tryNode)
2379
2365
var translatedBody = Translate ( tryNode . Body ) switch
2380
2366
{
2381
2367
BlockSyntax b => ( IEnumerable < SyntaxNode > ) b . Statements ,
2382
- var n => new [ ] { n }
2368
+ var n => [ n ]
2383
2369
} ;
2384
2370
2385
2371
var translatedFinally = Translate ( tryNode . Finally ) switch
2386
2372
{
2387
2373
BlockSyntax b => ( IEnumerable < SyntaxNode > ) b . Statements ,
2388
2374
null => null ,
2389
- var n => new [ ] { n }
2375
+ var n => [ n ]
2390
2376
} ;
2391
2377
2392
2378
switch ( _context )
0 commit comments