-
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| } | ||
| } | ||
| } | ||
| 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; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,12 +13,59 @@ | |
| * limitations under the License. | ||
| */ | ||
|
|
||
| using System; | ||
| using System.Reflection; | ||
|
|
||
| namespace MongoDB.Driver.Linq.Linq3Implementation.Misc | ||
| { | ||
| internal static class MethodInfoExtensions | ||
| { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Naming a method 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 |
||
| public static bool Has1GenericArgument(this MethodInfo method, out Type genericArgument) | ||
| { | ||
| if (method.IsGenericMethod && | ||
| method.GetGenericArguments() is var genericArguments && | ||
| genericArguments.Length == 1) | ||
| { | ||
| genericArgument = genericArguments[0]; | ||
| return true; | ||
| } | ||
|
|
||
| genericArgument = null; | ||
| return false; | ||
| } | ||
|
|
||
| public static bool Has2Parameters(this MethodInfo method, out ParameterInfo parameter1, out ParameterInfo parameter2) | ||
| { | ||
| if (method.GetParameters() is var parameters && | ||
| parameters.Length == 2) | ||
| { | ||
| parameter1 = parameters[0]; | ||
| parameter2 = parameters[1]; | ||
| return true; | ||
| } | ||
|
|
||
| parameter1 = null; | ||
| parameter2 = null; | ||
| return false; | ||
| } | ||
|
|
||
| public static bool Has3Parameters(this MethodInfo method, out ParameterInfo parameter1, out ParameterInfo parameter2, out ParameterInfo parameter3) | ||
| { | ||
| if (method.GetParameters() is var parameters && | ||
| parameters.Length == 3) | ||
| { | ||
| parameter1 = parameters[0]; | ||
| parameter2 = parameters[1]; | ||
| parameter3 = parameters[2]; | ||
| return true; | ||
| } | ||
|
|
||
| parameter1 = null; | ||
| parameter2 = null; | ||
| parameter3 = null; | ||
| return false; | ||
| } | ||
|
|
||
| public static bool Is(this MethodInfo method, MethodInfo comparand) | ||
| { | ||
| if (comparand != null) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.