Skip to content

Commit f3aba8c

Browse files
authored
Cleanup usage of BoundBinaryOperator.Method API (#80942)
`BoundBinaryOperator.Method` is replaced with two APIs: - BinaryOperatorMethod - LeftTruthOperatorMethod Closes #78628.
1 parent bdc3713 commit f3aba8c

17 files changed

+70
-43
lines changed

src/Compilers/CSharp/Portable/Binder/Binder.ValueChecks.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,14 @@ public static MethodInvocationInfo FromUnaryOperator(BoundUnaryOperator unaryOpe
220220

221221
public static MethodInvocationInfo FromBinaryOperator(BoundBinaryOperator binaryOperator)
222222
{
223-
Debug.Assert(binaryOperator.Method is not null);
223+
var binaryOperatorMethod = binaryOperator.BinaryOperatorMethod;
224+
Debug.Assert(binaryOperatorMethod is not null);
224225
return new MethodInvocationInfo
225226
{
226-
MethodInfo = MethodInfo.Create(binaryOperator.Method),
227+
MethodInfo = MethodInfo.Create(binaryOperatorMethod),
227228
Receiver = null,
228229
ReceiverIsSubjectToCloning = ThreeState.Unknown,
229-
Parameters = binaryOperator.Method.Parameters,
230+
Parameters = binaryOperatorMethod.Parameters,
230231
ArgsOpt = [binaryOperator.Left, binaryOperator.Right],
231232
ArgumentRefKindsOpt = default,
232233
ArgsToParamsOpt = default,
@@ -3912,7 +3913,7 @@ internal SafeContext GetRefEscape(BoundExpression expr, SafeContext localScopeDe
39123913

39133914
case BoundKind.BinaryOperator:
39143915
Debug.Assert(expr is BoundBinaryOperator binaryOperator &&
3915-
(binaryOperator.Method is not { } binaryMethod ||
3916+
(binaryOperator.BinaryOperatorMethod is not { } binaryMethod ||
39163917
binaryMethod.HasUnsupportedMetadata ||
39173918
binaryMethod.RefKind == RefKind.None));
39183919
break;
@@ -4257,7 +4258,7 @@ internal bool CheckRefEscape(SyntaxNode node, BoundExpression expr, SafeContext
42574258

42584259
case BoundKind.BinaryOperator:
42594260
Debug.Assert(expr is BoundBinaryOperator binaryOperator &&
4260-
(binaryOperator.Method is not { } binaryMethod ||
4261+
(binaryOperator.BinaryOperatorMethod is not { } binaryMethod ||
42614262
binaryMethod.HasUnsupportedMetadata ||
42624263
binaryMethod.RefKind == RefKind.None));
42634264
break;
@@ -4672,7 +4673,7 @@ internal SafeContext GetValEscape(BoundExpression expr, SafeContext localScopeDe
46724673
case BoundKind.BinaryOperator:
46734674
var binary = (BoundBinaryOperator)expr;
46744675

4675-
if (binary.Method is { } binaryMethod)
4676+
if (binary.BinaryOperatorMethod is { } binaryMethod)
46764677
{
46774678
return GetInvocationEscapeScope(
46784679
MethodInvocationInfo.FromBinaryOperator(binary),
@@ -5441,7 +5442,7 @@ internal bool CheckValEscape(SyntaxNode node, BoundExpression expr, SafeContext
54415442
return true;
54425443
}
54435444

5444-
if (binary.Method is { } binaryMethod)
5445+
if (binary.BinaryOperatorMethod is { } binaryMethod)
54455446
{
54465447
return CheckInvocationEscape(
54475448
binary.Syntax,

src/Compilers/CSharp/Portable/Binder/Binder_TupleOperators.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ private TupleBinaryOperatorInfo BindTupleBinaryOperatorInfo(BinaryExpressionSynt
108108
out UnaryOperatorSignature boolOperator);
109109
CheckConstraintLanguageVersionAndRuntimeSupportForOperator(node, boolOperator.Method, isUnsignedRightShift: false, boolOperator.ConstrainedToTypeOpt, diagnostics);
110110

111-
return new TupleBinaryOperatorInfo.Single(binary.Left.Type, binary.Right.Type, binary.OperatorKind, binary.Method, binary.ConstrainedToType,
111+
Debug.Assert(!binary.OperatorKind.IsDynamic());
112+
return new TupleBinaryOperatorInfo.Single(binary.Left.Type, binary.Right.Type, binary.OperatorKind, binary.BinaryOperatorMethod, binary.ConstrainedToType,
112113
conversionIntoBoolOperatorPlaceholder, conversionIntoBoolOperator, boolOperator);
113114

114115
default:

src/Compilers/CSharp/Portable/BoundTree/BoundBinaryOperator.UncommonData.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ namespace Microsoft.CodeAnalysis.CSharp
1010
{
1111
internal partial class BoundBinaryOperator
1212
{
13+
private partial void Validate()
14+
{
15+
if (Data is { Method: { } method })
16+
{
17+
if (OperatorKind.IsDynamic())
18+
{
19+
Debug.Assert(OperatorKind.IsLogical());
20+
Debug.Assert(method.Name is WellKnownMemberNames.TrueOperatorName or WellKnownMemberNames.FalseOperatorName);
21+
Debug.Assert(method.ParameterCount == 1);
22+
}
23+
else
24+
{
25+
Debug.Assert(method is ErrorMethodSymbol or { ParameterCount: 2 });
26+
}
27+
}
28+
}
29+
1330
internal class UncommonData
1431
{
1532
public static UncommonData UnconvertedInterpolatedStringAddition(ConstantValue? constantValue) =>
@@ -54,6 +71,8 @@ public static UncommonData InterpolatedStringHandlerAddition(InterpolatedStringH
5471
private UncommonData(ConstantValue? constantValue, MethodSymbol? method, TypeSymbol? constrainedToType, ImmutableArray<MethodSymbol> originalUserDefinedOperatorsOpt, bool isUnconvertedInterpolatedStringAddition, InterpolatedStringHandlerData? interpolatedStringHandlerData)
5572
{
5673
Debug.Assert(interpolatedStringHandlerData is null || !isUnconvertedInterpolatedStringAddition);
74+
Debug.Assert(method is null or ErrorMethodSymbol { ParameterCount: 0 } or { MethodKind: MethodKind.UserDefinedOperator } or { ParameterCount: 2 });
75+
5776
ConstantValue = constantValue;
5877
Method = method;
5978
ConstrainedToType = constrainedToType;

src/Compilers/CSharp/Portable/BoundTree/BoundExpression.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,10 @@ internal partial class BoundBinaryOperator
403403
{
404404
public override ConstantValue? ConstantValueOpt => Data?.ConstantValue;
405405

406-
public override Symbol? ExpressionSymbol => this.Method;
406+
public override Symbol? ExpressionSymbol => this.BinaryOperatorMethod;
407407

408-
internal MethodSymbol? Method => Data?.Method;
408+
public MethodSymbol? BinaryOperatorMethod => OperatorKind.IsDynamic() ? null : Data?.Method;
409+
public MethodSymbol? LeftTruthOperatorMethod => OperatorKind.IsDynamic() && OperatorKind.IsLogical() ? Data?.Method : null;
409410

410411
internal TypeSymbol? ConstrainedToType => Data?.ConstrainedToType;
411412

src/Compilers/CSharp/Portable/BoundTree/BoundNodes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@
481481
<Field Name="Right" Type="BoundExpression"/>
482482
</AbstractNode>
483483

484-
<Node Name="BoundBinaryOperator" Base="BoundBinaryOperatorBase" SkipInNullabilityRewriter="true">
484+
<Node Name="BoundBinaryOperator" Base="BoundBinaryOperatorBase" SkipInNullabilityRewriter="true" HasValidate="true">
485485
<Field Name="OperatorKind" Type="BinaryOperatorKind"/>
486486
<Field Name="Data" Type="BoundBinaryOperator.UncommonData?" />
487487
<Field Name="ResultKind" PropertyOverrides="true" Type="LookupResultKind"/>

src/Compilers/CSharp/Portable/BoundTree/NullabilityRewriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private BoundNode VisitBinaryOperatorBase(BoundBinaryOperatorBase binaryOperator
9999
{
100100
BoundBinaryOperator binary => binary.Update(
101101
binary.OperatorKind,
102-
binary.Data?.WithUpdatedMethod(GetUpdatedSymbol(binary, binary.Method)),
102+
binary.BinaryOperatorMethod is { } binaryOperatorMethod ? binary.Data?.WithUpdatedMethod(GetUpdatedSymbol(binary, binaryOperatorMethod)) : binary.Data,
103103
binary.ResultKind,
104104
leftChild,
105105
right,

src/Compilers/CSharp/Portable/CodeGen/Optimizer.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,8 @@ public override BoundNode VisitBinaryOperator(BoundBinaryOperator node)
15331533
}
15341534

15351535
var type = this.VisitType(binary.Type);
1536-
left = binary.Update(binary.OperatorKind, binary.ConstantValueOpt, binary.Method, binary.ConstrainedToType, binary.ResultKind, left, right, type);
1536+
Debug.Assert(!binary.OperatorKind.IsDynamic());
1537+
left = binary.Update(binary.OperatorKind, binary.ConstantValueOpt, binary.BinaryOperatorMethod, binary.ConstrainedToType, binary.ResultKind, left, right, type);
15371538

15381539
if (stack.Count == 0)
15391540
{
@@ -1567,7 +1568,8 @@ private BoundNode VisitBinaryOperatorSimple(BoundBinaryOperator node)
15671568

15681569
EnsureStackState(cookie); // implicit label here
15691570

1570-
return node.Update(node.OperatorKind, node.ConstantValueOpt, node.Method, node.ConstrainedToType, node.ResultKind, left, right, node.Type);
1571+
Debug.Assert(!node.OperatorKind.IsDynamic());
1572+
return node.Update(node.OperatorKind, node.ConstantValueOpt, node.BinaryOperatorMethod, node.ConstrainedToType, node.ResultKind, left, right, node.Type);
15711573
}
15721574

15731575
return base.VisitBinaryOperator(node);
@@ -2108,7 +2110,8 @@ public override BoundNode VisitBinaryOperator(BoundBinaryOperator node)
21082110
binary = stack.Pop();
21092111
var right = (BoundExpression)this.Visit(binary.Right);
21102112
var type = this.VisitType(binary.Type);
2111-
left = binary.Update(binary.OperatorKind, binary.ConstantValueOpt, binary.Method, binary.ConstrainedToType, binary.ResultKind, left, right, type);
2113+
Debug.Assert(!binary.OperatorKind.IsDynamic());
2114+
left = binary.Update(binary.OperatorKind, binary.ConstantValueOpt, binary.BinaryOperatorMethod, binary.ConstrainedToType, binary.ResultKind, left, right, type);
21122115

21132116
if (stack.Count == 0)
21142117
{

src/Compilers/CSharp/Portable/Compilation/CSharpSemanticModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3854,12 +3854,12 @@ private static void GetSymbolsAndResultKind(BoundBinaryOperator binaryOperator,
38543854
{
38553855
if (!isDynamic)
38563856
{
3857-
GetSymbolsAndResultKind(binaryOperator, binaryOperator.Method, binaryOperator.OriginalUserDefinedOperatorsOpt, out symbols, out resultKind);
3857+
GetSymbolsAndResultKind(binaryOperator, binaryOperator.BinaryOperatorMethod, binaryOperator.OriginalUserDefinedOperatorsOpt, out symbols, out resultKind);
38583858
}
38593859
}
38603860
else
38613861
{
3862-
Debug.Assert((object)binaryOperator.Method == null && binaryOperator.OriginalUserDefinedOperatorsOpt.IsDefaultOrEmpty);
3862+
Debug.Assert((object)binaryOperator.BinaryOperatorMethod == null && binaryOperator.OriginalUserDefinedOperatorsOpt.IsDefaultOrEmpty);
38633863

38643864
if (!isDynamic &&
38653865
(op == BinaryOperatorKind.Equal || op == BinaryOperatorKind.NotEqual) &&

src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5339,7 +5339,7 @@ private void ReinferBinaryOperatorAndSetResult(
53395339
TypeWithState rightType,
53405340
BoundBinaryOperator binary)
53415341
{
5342-
var inferredResult = ReinferAndVisitBinaryOperator(binary, binary.OperatorKind, binary.Method, binary.Type, binary.Left, leftOperand, leftConversion, leftType, binary.Right, rightOperand, rightConversion, rightType);
5342+
var inferredResult = ReinferAndVisitBinaryOperator(binary, binary.OperatorKind, binary.BinaryOperatorMethod, binary.Type, binary.Left, leftOperand, leftConversion, leftType, binary.Right, rightOperand, rightConversion, rightType);
53435343
SetResult(binary, inferredResult, inferredResult.ToTypeWithAnnotations(compilation));
53445344
}
53455345

@@ -12293,7 +12293,7 @@ void afterLeftChildOfBoundBinaryOperatorHasBeenVisited(BoundBinaryOperator node)
1229312293

1229412294
Visit(node.Right);
1229512295
TypeWithState rightType = ResultType;
12296-
SetResultType(node, InferResultNullability(node.OperatorKind, node.Method, node.Type, leftType, rightType));
12296+
SetResultType(node, InferResultNullability(node.OperatorKind, node.BinaryOperatorMethod, node.Type, leftType, rightType));
1229712297
AfterRightChildOfBinaryLogicalOperatorHasBeenVisited(node.Right, isAnd, isBool, ref leftTrue, ref leftFalse);
1229812298
}
1229912299

src/Compilers/CSharp/Portable/Generated/BoundNodes.xml.Generated.cs

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)