Skip to content

Implement CRef binding for compound assignment operators #78111

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
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
23 changes: 16 additions & 7 deletions src/Compilers/CSharp/Portable/Binder/Binder_Crefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,24 @@ private ImmutableArray<Symbol> BindOperatorMemberCref(OperatorMemberCrefSyntax s
CrefParameterListSyntax? parameterListSyntax = syntax.Parameters;
bool isChecked = syntax.CheckedKeyword.IsKind(SyntaxKind.CheckedKeyword);

// NOTE: Prefer binary to unary, unless there is exactly one parameter.
// CONSIDER: we're following dev11 by never using a binary operator name if there's
// exactly one parameter, but doing so would allow us to match single-parameter constructors.
SyntaxKind operatorTokenKind = syntax.OperatorToken.Kind();
string? memberName = parameterListSyntax != null && parameterListSyntax.Parameters.Count == 1
? null
: OperatorFacts.BinaryOperatorNameFromSyntaxKindIfAny(operatorTokenKind, isChecked);
string? memberName;

memberName = memberName ?? OperatorFacts.UnaryOperatorNameFromSyntaxKindIfAny(operatorTokenKind, isChecked: isChecked);
if (SyntaxFacts.IsOverloadableCompoundAssignmentOperator(operatorTokenKind))
{
memberName = OperatorFacts.CompoundAssignmentOperatorNameFromSyntaxKind(operatorTokenKind, isChecked);
}
else
{
// NOTE: Prefer binary to unary, unless there is exactly one parameter.
// CONSIDER: we're following dev11 by never using a binary operator name if there's
// exactly one parameter, but doing so would allow us to match single-parameter constructors.
memberName = parameterListSyntax != null && parameterListSyntax.Parameters.Count == 1
? null
: OperatorFacts.BinaryOperatorNameFromSyntaxKindIfAny(operatorTokenKind, isChecked);

memberName = memberName ?? OperatorFacts.UnaryOperatorNameFromSyntaxKindIfAny(operatorTokenKind, isChecked: isChecked);
}

if (memberName == null ||
(isChecked && !syntax.OperatorToken.IsMissing && !SyntaxFacts.IsCheckedOperator(memberName))) // the operator cannot be checked
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2649,7 +2649,7 @@ private BoundExpression BindIncrementOperator(ExpressionSyntax node, ExpressionS
}
}

ArrayBuilder<MethodSymbol>? LookupUserDefinedInstanceOperators(TypeSymbol lookupInType, string? checkedName, string ordinaryName, int parameterCount, ref CompoundUseSiteInfo<AssemblySymbol> useSiteInfo)
private ArrayBuilder<MethodSymbol>? LookupUserDefinedInstanceOperators(TypeSymbol lookupInType, string? checkedName, string ordinaryName, int parameterCount, ref CompoundUseSiteInfo<AssemblySymbol> useSiteInfo)
{
Debug.Assert(parameterCount is 0 or 1);

Expand Down
Loading