Skip to content

Commit

Permalink
Fold member access into conditional
Browse files Browse the repository at this point in the history
  • Loading branch information
ranma42 committed Dec 28, 2024
1 parent 5effb26 commit 7d46e86
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,16 @@ public Expression Expand(InMemoryQueryExpression queryExpression, Expression lam

protected override Expression VisitMember(MemberExpression memberExpression)
{
// Fold member access into conditional, i.e. transform
// (test ? expr1 : expr2).Member -> (test ? expr1.Member : expr2.Member)
if (memberExpression.Expression is ConditionalExpression cond) {
return Visit(Expression.Condition(
cond.Test,
Expression.MakeMemberAccess(cond.IfTrue, memberExpression.Member),
Expression.MakeMemberAccess(cond.IfFalse, memberExpression.Member)
));
}

var innerExpression = Visit(memberExpression.Expression);

return TryExpand(innerExpression, MemberIdentity.Create(memberExpression.Member))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,16 @@ protected override Expression VisitListInit(ListInitExpression listInitExpressio
/// <inheritdoc />
protected override Expression VisitMember(MemberExpression memberExpression)
{
// Fold member access into conditional, i.e. transform
// (test ? expr1 : expr2).Member -> (test ? expr1.Member : expr2.Member)
if (memberExpression.Expression is ConditionalExpression cond) {
return Visit(Expression.Condition(
cond.Test,
Expression.MakeMemberAccess(cond.IfTrue, memberExpression.Member),
Expression.MakeMemberAccess(cond.IfFalse, memberExpression.Member)
));
}

var innerExpression = Visit(memberExpression.Expression);

return TryBindMember(innerExpression, MemberIdentity.Create(memberExpression.Member), out var expression)
Expand Down

0 comments on commit 7d46e86

Please sign in to comment.