-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5749: Support C# 14 changes that result in overloads now binding MemoryExtensions extension methods #1802
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
rstam
wants to merge
2
commits into
mongodb:main
Choose a base branch
from
rstam:csharp5749
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
2 commits
Select commit
Hold shift + click to select a range
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
150 changes: 150 additions & 0 deletions
150
src/MongoDB.Driver/Linq/Linq3Implementation/Misc/ClrCompatExpressionRewriter.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,150 @@ | ||
| /* Copyright 2010-present MongoDB Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| using System; | ||
| using System.Collections.ObjectModel; | ||
| using System.Linq; | ||
| using System.Linq.Expressions; | ||
| using System.Reflection; | ||
| using MongoDB.Driver.Linq.Linq3Implementation.Reflection; | ||
|
|
||
| namespace MongoDB.Driver.Linq.Linq3Implementation.Misc; | ||
|
|
||
| /// <summary> | ||
| /// This visitor rewrites expressions where features of .NET CLR or | ||
| /// C# compiler interfere with LINQ expression tree translation. | ||
| /// </summary> | ||
| internal class ClrCompatExpressionRewriter : ExpressionVisitor | ||
| { | ||
| private static readonly ClrCompatExpressionRewriter __instance = new(); | ||
|
|
||
| public static Expression Rewrite(Expression expression) | ||
| => __instance.Visit(expression); | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override Expression VisitMethodCall(MethodCallExpression node) | ||
| { | ||
| node = (MethodCallExpression)base.VisitMethodCall(node); | ||
|
|
||
| var method = node.Method; | ||
| var arguments = node.Arguments; | ||
|
|
||
| return method.Name switch | ||
| { | ||
| "Contains" => VisitContainsMethod(node, method, arguments), | ||
| "SequenceEqual" => VisitSequenceEqualMethod(node, method, arguments), | ||
| _ => node | ||
| }; | ||
|
|
||
| static Expression VisitContainsMethod(MethodCallExpression node, MethodInfo method, ReadOnlyCollection<Expression> arguments) | ||
| { | ||
| if (method.IsOneOf(MemoryExtensionsMethod.ContainsWithReadOnlySpanAndValue, MemoryExtensionsMethod.ContainsWithSpanAndValue)) | ||
| { | ||
| var itemType = method.GetGenericArguments().Single(); | ||
| var span = arguments[0]; | ||
| var value = arguments[1]; | ||
|
|
||
| if (TryUnwrapSpanImplicitCast(span, out var unwrappedSpan) && | ||
| unwrappedSpan.Type.ImplementsIEnumerableOf(itemType)) | ||
| { | ||
| return | ||
| Expression.Call( | ||
| EnumerableMethod.Contains.MakeGenericMethod(itemType), | ||
| [unwrappedSpan, value]); | ||
| } | ||
| } | ||
| else if (method.Is(MemoryExtensionsMethod.ContainsWithReadOnlySpanAndValueAndComparer)) | ||
| { | ||
| var itemType = method.GetGenericArguments().Single(); | ||
| var span = arguments[0]; | ||
| var value = arguments[1]; | ||
| var comparer = arguments[2]; | ||
|
|
||
| if (TryUnwrapSpanImplicitCast(span, out var unwrappedSpan) && | ||
| unwrappedSpan.Type.ImplementsIEnumerableOf(itemType)) | ||
| { | ||
| return | ||
| Expression.Call( | ||
| EnumerableMethod.ContainsWithComparer.MakeGenericMethod(itemType), | ||
| [unwrappedSpan, value, comparer]); | ||
| } | ||
| } | ||
|
|
||
| return node; | ||
| } | ||
|
|
||
| static Expression VisitSequenceEqualMethod(MethodCallExpression node, MethodInfo method, ReadOnlyCollection<Expression> arguments) | ||
| { | ||
| if (method.IsOneOf(MemoryExtensionsMethod.SequenceEqualWithReadOnlySpanAndReadOnlySpan, MemoryExtensionsMethod.SequenceEqualWithSpanAndReadOnlySpan)) | ||
| { | ||
| var itemType = method.GetGenericArguments().Single(); | ||
| var span = arguments[0]; | ||
| var other = arguments[1]; | ||
|
|
||
| if (TryUnwrapSpanImplicitCast(span, out var unwrappedSpan) && | ||
| TryUnwrapSpanImplicitCast(other, out var unwrappedOther) && | ||
| unwrappedSpan.Type.ImplementsIEnumerableOf(itemType) && | ||
| unwrappedOther.Type.ImplementsIEnumerableOf(itemType)) | ||
| { | ||
| return | ||
| Expression.Call( | ||
| EnumerableMethod.SequenceEqual.MakeGenericMethod(itemType), | ||
| [unwrappedSpan, unwrappedOther]); | ||
| } | ||
| } | ||
| else if (method.IsOneOf(MemoryExtensionsMethod.SequenceEqualWithReadOnlySpanAndReadOnlySpanAndComparer, MemoryExtensionsMethod.SequenceEqualWithSpanAndReadOnlySpanAndComparer)) | ||
| { | ||
| var itemType = method.GetGenericArguments().Single(); | ||
| var span = arguments[0]; | ||
| var other = arguments[1]; | ||
| var comparer = arguments[2]; | ||
|
|
||
| if (TryUnwrapSpanImplicitCast(span, out var unwrappedSpan) && | ||
| TryUnwrapSpanImplicitCast(other, out var unwrappedOther) && | ||
| unwrappedSpan.Type.ImplementsIEnumerableOf(itemType) && | ||
| unwrappedOther.Type.ImplementsIEnumerableOf(itemType)) | ||
| { | ||
| return | ||
| Expression.Call( | ||
| EnumerableMethod.SequenceEqualWithComparer.MakeGenericMethod(itemType), | ||
| [unwrappedSpan, unwrappedOther, comparer]); | ||
| } | ||
| } | ||
|
|
||
| return node; | ||
| } | ||
|
|
||
| // Erase implicit casts to ReadOnlySpan<T> and Span<T> | ||
| static bool TryUnwrapSpanImplicitCast(Expression expression, out Expression result) | ||
| { | ||
| if (expression is MethodCallExpression | ||
| { | ||
| Method: | ||
| { | ||
| Name: "op_Implicit", DeclaringType: { IsGenericType: true } implicitCastDeclaringType | ||
| } | ||
| } methodCallExpression | ||
| && implicitCastDeclaringType.GetGenericTypeDefinition() is var genericTypeDefinition | ||
| && (genericTypeDefinition == typeof(Span<>) || genericTypeDefinition == typeof(ReadOnlySpan<>))) | ||
| { | ||
| result = methodCallExpression.Arguments[0]; | ||
| return true; | ||
| } | ||
|
|
||
| result = null; | ||
| return false; | ||
| } | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
src/MongoDB.Driver/Linq/Linq3Implementation/Misc/LinqExpressionPreprocessor.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,33 @@ | ||
| /* Copyright 2010-present MongoDB Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| using System.Linq.Expressions; | ||
|
|
||
| namespace MongoDB.Driver.Linq.Linq3Implementation.Misc; | ||
|
|
||
| /// <summary> | ||
| /// This class is called before we process any LINQ expression trees | ||
| /// to perform any necessary pre-processing such as CLR compatibility | ||
| /// and partial evaluation. | ||
| /// </summary> | ||
| internal static class LinqExpressionPreprocessor | ||
| { | ||
| public static Expression Preprocess(Expression expression) | ||
| { | ||
| expression = ClrCompatExpressionRewriter.Rewrite(expression); | ||
| expression = PartialEvaluator.EvaluatePartially(expression); | ||
| return expression; | ||
| } | ||
| } |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of Has can we use TryGet given that this has out parameters that it tries to get? It would follow many other .net methods like TryGetValue on Dictionary.
E g. TryGetSingleGenericArgument
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to keep the existing names.
The Linq processor has many existing methods that are named
IsXyz,HasXyz,ImplementsXyzetc.. many of which also haveoutparameters.Naming a method
TryXyzputs the emphasis on the operation and whether it succeeds or fails.In the LINQ processor we aren't thinking in terms of success or failure, we are thinking in terms of asking questions like "is", "has", "implements" etc... the
outparameters are less important than the question being asked.