-
Notifications
You must be signed in to change notification settings - Fork 540
Fix Expression constant folding inside of MemberInitExpression
#5298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MichalMarsalek
wants to merge
9
commits into
Azure:main
Choose a base branch
from
MichalMarsalek:fix-#1664
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0dd36a3
fix #1664
MichalMarsalek 0c05cc9
Merge branch 'main' into fix-#1664
NaluTripician 8fc8b9c
Address PR review feedback for MemberInit constant folding fix
NaluTripician 6cce7dd
Merge branch 'main' into fix-#1664
NaluTripician 960135d
Changelog: Adds Unreleased entry for LINQ MemberInit constant-folding…
NaluTripician 216b594
Merge remote-tracking branch 'origin/main' into pr-5298-merge-main
NaluTripician ff9d2ff
Merge remote-tracking branch 'origin/main' into fix-#1664
NaluTripician 2aa88e2
Merge remote-tracking branch 'origin/main' into fix-#1664
NaluTripician 64d5c8b
Merge remote-tracking branch 'origin/main' into pr-5298-merge
NaluTripician File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Linq/ConstantEvaluatorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| //------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| //------------------------------------------------------------ | ||
|
|
||
| namespace Microsoft.Azure.Cosmos.Linq | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Linq.Expressions; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| [TestClass] | ||
| public class ConstantEvaluatorTests | ||
|
NaluTripician marked this conversation as resolved.
|
||
| { | ||
| [TestMethod] | ||
| public void ClosuresInsideMemberInitExpressionAreFolded() | ||
| { | ||
| int captured = 1; | ||
| Expression<Func<int, object>> expression = x => new TestClass { Property = x + captured }; | ||
|
|
||
| Expression folded = ConstantEvaluator.PartialEval(expression.Body); | ||
|
|
||
| MemberInitExpression memberInit = AssertMemberInit(folded, typeof(TestClass)); | ||
| MemberAssignment assignment = AssertSingleMemberAssignment(memberInit, nameof(TestClass.Property)); | ||
| BinaryExpression binary = AssertBinary(assignment.Expression, ExpressionType.Add); | ||
| AssertParameter(binary.Left, "x"); | ||
| AssertConstant(binary.Right, 1); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ClosuresInsideAnonymousObjectAreFolded() | ||
| { | ||
| int captured = 1; | ||
| Expression<Func<int, object>> expression = x => new { Property = x + captured }; | ||
|
|
||
| Expression folded = ConstantEvaluator.PartialEval(expression.Body); | ||
|
|
||
| NewExpression newExpression = AssertNew(folded); | ||
| Assert.AreEqual(1, newExpression.Arguments.Count); | ||
| BinaryExpression binary = AssertBinary(newExpression.Arguments[0], ExpressionType.Add); | ||
| AssertParameter(binary.Left, "x"); | ||
| AssertConstant(binary.Right, 1); | ||
| } | ||
|
|
||
| // Regression test for https://github.com/Azure/azure-cosmos-dotnet-v3/issues/1664. | ||
| // Mirrors the original bug report shape: a dictionary indexer keyed by a closure-captured | ||
| // variable, used inside a class member initializer. Before the fix this folded to | ||
| // `{"k": "Test"}["k"]` instead of `"Test"`, producing invalid SQL at the Cosmos backend. | ||
| // The parameter reference `q.StringProperty` anchors the MemberInit so the Nominator | ||
| // cannot collapse the entire expression to a single constant — only the closure-only | ||
| // dictionary indexer sub-tree should fold. | ||
| [TestMethod] | ||
| public void ClosuresUsedAsDictionaryIndexerInsideMemberInitAreFolded() | ||
| { | ||
| Dictionary<string, string> map = new Dictionary<string, string> { ["k"] = "Test" }; | ||
| string capturedKey = "k"; | ||
| Expression<Func<TestClass, object>> expression = | ||
| q => new TestClass { StringProperty = q.StringProperty + map[capturedKey] }; | ||
|
|
||
| Expression folded = ConstantEvaluator.PartialEval(expression.Body); | ||
|
|
||
| MemberInitExpression memberInit = AssertMemberInit(folded, typeof(TestClass)); | ||
| MemberAssignment assignment = AssertSingleMemberAssignment(memberInit, nameof(TestClass.StringProperty)); | ||
| BinaryExpression binary = AssertBinary(assignment.Expression, ExpressionType.Add); | ||
| // Left side stays as `q.StringProperty` (parameter-bound member access). | ||
| MemberExpression leftMember = binary.Left as MemberExpression; | ||
| Assert.IsNotNull(leftMember, $"Expected MemberExpression on left of Add but got {binary.Left?.NodeType.ToString() ?? "<null>"}."); | ||
| AssertParameter(leftMember.Expression, "q"); | ||
| Assert.AreEqual(nameof(TestClass.StringProperty), leftMember.Member.Name); | ||
| // Right side must be the folded literal — not a `Dictionary[indexer]` expression. | ||
| AssertConstant(binary.Right, "Test"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ClosuresInsideNestedMemberInitAreFolded() | ||
| { | ||
| int captured = 7; | ||
| Expression<Func<int, object>> expression = x => new OuterTestClass | ||
| { | ||
| Inner = new TestClass { Property = x + captured } | ||
| }; | ||
|
|
||
| Expression folded = ConstantEvaluator.PartialEval(expression.Body); | ||
|
|
||
| MemberInitExpression outerInit = AssertMemberInit(folded, typeof(OuterTestClass)); | ||
| MemberAssignment innerAssignment = AssertSingleMemberAssignment(outerInit, nameof(OuterTestClass.Inner)); | ||
| MemberInitExpression innerInit = AssertMemberInit(innerAssignment.Expression, typeof(TestClass)); | ||
| MemberAssignment propertyAssignment = AssertSingleMemberAssignment(innerInit, nameof(TestClass.Property)); | ||
| BinaryExpression binary = AssertBinary(propertyAssignment.Expression, ExpressionType.Add); | ||
| AssertParameter(binary.Left, "x"); | ||
| AssertConstant(binary.Right, 7); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ClosuresInsideMemberMemberBindingAreFolded() | ||
| { | ||
| int captured = 9; | ||
| Expression<Func<int, object>> expression = x => new OuterTestClass | ||
| { | ||
| Inner = { Property = x + captured } | ||
| }; | ||
|
|
||
| Expression folded = ConstantEvaluator.PartialEval(expression.Body); | ||
|
|
||
| MemberInitExpression outerInit = AssertMemberInit(folded, typeof(OuterTestClass)); | ||
| Assert.AreEqual(1, outerInit.Bindings.Count); | ||
| MemberMemberBinding nested = outerInit.Bindings[0] as MemberMemberBinding; | ||
| Assert.IsNotNull(nested, "Expected a MemberMemberBinding for nested initializer syntax."); | ||
| Assert.AreEqual(nameof(OuterTestClass.Inner), nested.Member.Name); | ||
| Assert.AreEqual(1, nested.Bindings.Count); | ||
| MemberAssignment propertyAssignment = nested.Bindings[0] as MemberAssignment; | ||
| Assert.IsNotNull(propertyAssignment, "Expected MemberAssignment inside MemberMemberBinding."); | ||
| Assert.AreEqual(nameof(TestClass.Property), propertyAssignment.Member.Name); | ||
| BinaryExpression binary = AssertBinary(propertyAssignment.Expression, ExpressionType.Add); | ||
| AssertParameter(binary.Left, "x"); | ||
| AssertConstant(binary.Right, 9); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ClosuresInsideMemberListBindingAreFolded() | ||
| { | ||
| int captured = 11; | ||
| Expression<Func<int, object>> expression = x => new OuterWithListTestClass | ||
| { | ||
| Items = { x + captured } | ||
| }; | ||
|
|
||
| Expression folded = ConstantEvaluator.PartialEval(expression.Body); | ||
|
|
||
| MemberInitExpression outerInit = AssertMemberInit(folded, typeof(OuterWithListTestClass)); | ||
| Assert.AreEqual(1, outerInit.Bindings.Count); | ||
| MemberListBinding listBinding = outerInit.Bindings[0] as MemberListBinding; | ||
| Assert.IsNotNull(listBinding, "Expected a MemberListBinding for collection initializer syntax."); | ||
| Assert.AreEqual(nameof(OuterWithListTestClass.Items), listBinding.Member.Name); | ||
| Assert.AreEqual(1, listBinding.Initializers.Count); | ||
| Assert.AreEqual(1, listBinding.Initializers[0].Arguments.Count); | ||
| BinaryExpression binary = AssertBinary(listBinding.Initializers[0].Arguments[0], ExpressionType.Add); | ||
| AssertParameter(binary.Left, "x"); | ||
| AssertConstant(binary.Right, 11); | ||
| } | ||
|
|
||
| private static MemberInitExpression AssertMemberInit(Expression expression, Type expectedType) | ||
| { | ||
| MemberInitExpression memberInit = expression as MemberInitExpression; | ||
| Assert.IsNotNull(memberInit, $"Expected MemberInitExpression but got {expression?.NodeType.ToString() ?? "<null>"}."); | ||
| Assert.AreEqual(expectedType, memberInit.Type); | ||
| return memberInit; | ||
| } | ||
|
|
||
| private static NewExpression AssertNew(Expression expression) | ||
| { | ||
| NewExpression newExpression = expression as NewExpression; | ||
| Assert.IsNotNull(newExpression, $"Expected NewExpression but got {expression?.NodeType.ToString() ?? "<null>"}."); | ||
| return newExpression; | ||
| } | ||
|
|
||
| private static MemberAssignment AssertSingleMemberAssignment(MemberInitExpression memberInit, string memberName) | ||
| { | ||
| Assert.AreEqual(1, memberInit.Bindings.Count, $"Expected a single binding for member '{memberName}'."); | ||
| MemberAssignment assignment = memberInit.Bindings[0] as MemberAssignment; | ||
| Assert.IsNotNull(assignment, $"Expected MemberAssignment for member '{memberName}' but got {memberInit.Bindings[0].BindingType}."); | ||
| Assert.AreEqual(memberName, assignment.Member.Name); | ||
| return assignment; | ||
| } | ||
|
|
||
| private static BinaryExpression AssertBinary(Expression expression, ExpressionType nodeType) | ||
| { | ||
| BinaryExpression binary = expression as BinaryExpression; | ||
| Assert.IsNotNull(binary, $"Expected BinaryExpression but got {expression?.NodeType.ToString() ?? "<null>"}."); | ||
| Assert.AreEqual(nodeType, binary.NodeType); | ||
| return binary; | ||
| } | ||
|
|
||
| private static void AssertParameter(Expression expression, string parameterName) | ||
| { | ||
| ParameterExpression parameter = expression as ParameterExpression; | ||
| Assert.IsNotNull(parameter, $"Expected ParameterExpression '{parameterName}' but got {expression?.NodeType.ToString() ?? "<null>"}."); | ||
| Assert.AreEqual(parameterName, parameter.Name); | ||
| } | ||
|
|
||
| private static void AssertConstant<T>(Expression expression, T expectedValue) | ||
| { | ||
| ConstantExpression constant = expression as ConstantExpression; | ||
| Assert.IsNotNull(constant, $"Expected ConstantExpression with value '{expectedValue}' but got {expression?.NodeType.ToString() ?? "<null>"}."); | ||
| Assert.AreEqual(expectedValue, constant.Value); | ||
| } | ||
|
|
||
| private class TestClass | ||
| { | ||
| public int Property { get; set; } | ||
|
|
||
| public string StringProperty { get; set; } | ||
| } | ||
|
|
||
| private class OuterTestClass | ||
| { | ||
| public TestClass Inner { get; set; } = new TestClass(); | ||
| } | ||
|
|
||
| private class OuterWithListTestClass | ||
| { | ||
| public List<int> Items { get; } = new List<int>(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.