Skip to content

Commit

Permalink
Evaluate captures during expr tree comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
stakx committed Apr 18, 2020
1 parent b0ca3ee commit f1a5f60
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Moq/ExpressionComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Diagnostics;
using System.Linq.Expressions;

using Moq.Expressions.Visitors;

namespace Moq
{
internal sealed class ExpressionComparer : IEqualityComparer<Expression>
Expand Down Expand Up @@ -200,7 +202,21 @@ private bool EqualsMemberBinding(MemberBinding x, MemberBinding y)

private bool EqualsMember(MemberExpression x, MemberExpression y)
{
return x.Member == y.Member && this.Equals(x.Expression, y.Expression);
// If any of the two nodes represents an access to a captured variable,
// we want to compare its value, not its identity. (`EvaluateCaptures` is
// a no-op in all other cases, so it is safe to apply "just in case".)
var rx = x.Apply(EvaluateCaptures.Rewriter);
var ry = y.Apply(EvaluateCaptures.Rewriter);

if (rx == x && ry == y)
{
return x.Member == y.Member && this.Equals(x.Expression, y.Expression);
}
else
{
// Rewriting occurred, we might no longer have two `MemberExpression`s:
return this.Equals(rx, ry);
}
}

private bool EqualsMemberInit(MemberInitExpression x, MemberInitExpression y)
Expand Down

0 comments on commit f1a5f60

Please sign in to comment.