Skip to content

Support extension operators in Linq Expression Trees #78942

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

Merged
merged 1 commit into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -8206,4 +8206,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_InstanceOperatorExtensionWrongReceiverType" xml:space="preserve">
<value>Cannot declare instance extension operator for a type that is not known to be a struct and is not known to be a class</value>
</data>
<data name="ERR_ExpressionTreeContainsExtensionBasedConditionalLogicalOperator" xml:space="preserve">
<value>An expression tree may not contain '&amp;&amp;' or '||' operators that use extension user defined operators.</value>
</data>
</root>
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2417,6 +2417,7 @@ internal enum ErrorCode
ERR_OperatorInExtensionOfStaticClass = 9555,
ERR_InstanceOperatorStructExtensionWrongReceiverRefKind = 9556,
ERR_InstanceOperatorExtensionWrongReceiverType = 9557,
ERR_ExpressionTreeContainsExtensionBasedConditionalLogicalOperator = 9558,

// Note: you will need to do the following after adding errors:
// 1) Update ErrorFacts.IsBuildOnlyDiagnostic (src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs)
Expand Down
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2528,6 +2528,7 @@ or ErrorCode.ERR_BadExtensionShiftOperatorSignature
or ErrorCode.ERR_OperatorInExtensionOfStaticClass
or ErrorCode.ERR_InstanceOperatorStructExtensionWrongReceiverRefKind
or ErrorCode.ERR_InstanceOperatorExtensionWrongReceiverType
or ErrorCode.ERR_ExpressionTreeContainsExtensionBasedConditionalLogicalOperator
=> false,
};
#pragma warning restore CS8524 // The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,23 @@ public override BoundNode VisitUserDefinedConditionalLogicalOperator(BoundUserDe
{
Error(ErrorCode.ERR_ExpressionTreeContainsAbstractStaticMemberAccess, node);
}

if (binary.GetIsNewExtensionMember())
{
// PROTOTYPE: Communicate to LDM
// An expression tree factory isn't happy in this case. It throws
// System.ArgumentException : The user-defined operator method 'op_BitwiseOr' for operator 'OrElse' must have associated boolean True and False operators.
Copy link
Member

@jjonescz jjonescz Jun 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it work if there were corresponding op_True and op_False operators also defined in the extension class? #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it work if there were corresponding op_True and op_False operators also defined in the extension class?

We wouldn't get here if there were not.

// or
// System.ArgumentException : The user-defined operator method 'op_BitwiseAnd' for operator 'AndAlso' must have associated boolean True and False operators.
//
// from Expression.ValidateUserDefinedConditionalLogicOperator(ExpressionType nodeType, Type left, Type right, MethodInfo method)
Error(ErrorCode.ERR_ExpressionTreeContainsExtensionBasedConditionalLogicalOperator, node);
}
else
{
Debug.Assert(!node.TrueOperator.GetIsNewExtensionMember());
Debug.Assert(!node.FalseOperator.GetIsNewExtensionMember());
}
}

return base.VisitUserDefinedConditionalLogicalOperator(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,15 @@ public override BoundNode VisitFunctionPointerLoad(BoundFunctionPointerLoad node
Debug.Assert(symbol?.GetIsNewExtensionMember() != true);
return base.VisitPropertySymbol(symbol);
}

public override BoundNode VisitUnaryOperator(BoundUnaryOperator node)
{
return ExtensionMethodReferenceRewriter.VisitUnaryOperator(this, node);
}

protected override BoundBinaryOperator.UncommonData? VisitBinaryOperatorData(BoundBinaryOperator node)
{
return ExtensionMethodReferenceRewriter.VisitBinaryOperatorData(this, node);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ method.OriginalDefinition is ErrorMethodSymbol ||
{ Name: nameof(VisitReadOnlySpanFromArray) } => method is { Name: "op_Implicit", IsExtensionMethod: false }, // Conversion operator from array to span cannot be an extension method
{ Name: nameof(VisitLoweredConditionalAccess) } => // Nullable.HasValue cannot be an extension method
method.ContainingAssembly.GetSpecialTypeMember(SpecialMember.System_Nullable_T_get_HasValue) == (object)method.OriginalDefinition,
{ Name: nameof(VisitUnaryOperator) } => !method.IsExtensionMethod, // Expression tree context. At the moment an operator cannot be an extension method
{ Name: nameof(VisitUserDefinedConditionalLogicalOperator) } => !method.IsExtensionMethod, // Expression tree context. At the moment an operator cannot be an extension method
{ Name: nameof(VisitCollectionElementInitializer) } => !method.IsExtensionMethod, // Expression tree context. At the moment an extension method cannot be used in expression tree here.
{ Name: nameof(VisitAwaitableInfo) } => method is { Name: "GetResult", IsExtensionMethod: false }, // Cannot be an extension method
Expand Down Expand Up @@ -231,10 +230,19 @@ public static BoundNode VisitFunctionPointerLoad(BoundTreeRewriter rewriter, Bou

protected override BoundBinaryOperator.UncommonData? VisitBinaryOperatorData(BoundBinaryOperator node)
{
Debug.Assert(node.Method is null ||
(!node.Method.IsExtensionMethod && !node.Method.GetIsNewExtensionMember())); // Expression tree context. At the moment an operator cannot be an extension method
return VisitBinaryOperatorData(this, node);
}

return base.VisitBinaryOperatorData(node);
public static BoundBinaryOperator.UncommonData? VisitBinaryOperatorData(BoundTreeRewriter rewriter, BoundBinaryOperator node)
{
// Local rewriter should have already rewritten interpolated strings into their final form of calls and gotos
Debug.Assert(node.InterpolatedStringHandlerData is null);

return BoundBinaryOperator.UncommonData.CreateIfNeeded(
node.ConstantValueOpt,
VisitMethodSymbolWithExtensionRewrite(rewriter, node.Method),
rewriter.VisitType(node.ConstrainedToType),
node.OriginalUserDefinedOperatorsOpt);
}

[return: NotNullIfNotNull(nameof(symbol))]
Expand All @@ -243,5 +251,20 @@ public static BoundNode VisitFunctionPointerLoad(BoundTreeRewriter rewriter, Bou
Debug.Assert(symbol?.GetIsNewExtensionMember() != true);
return base.VisitPropertySymbol(symbol);
}

public override BoundNode VisitUnaryOperator(BoundUnaryOperator node)
{
return VisitUnaryOperator(this, node);
}

public static BoundNode VisitUnaryOperator(BoundTreeRewriter rewriter, BoundUnaryOperator node)
{
MethodSymbol? methodOpt = VisitMethodSymbolWithExtensionRewrite(rewriter, node.MethodOpt);
ImmutableArray<MethodSymbol> originalUserDefinedOperatorsOpt = rewriter.VisitSymbols<MethodSymbol>(node.OriginalUserDefinedOperatorsOpt);
BoundExpression operand = (BoundExpression)rewriter.Visit(node.Operand);
TypeSymbol? constrainedToTypeOpt = rewriter.VisitType(node.ConstrainedToTypeOpt);
TypeSymbol? type = rewriter.VisitType(node.Type);
return node.Update(node.OperatorKind, operand, node.ConstantValueOpt, methodOpt, constrainedToTypeOpt, node.ResultKind, originalUserDefinedOperatorsOpt, type);
}
}
}
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading