Skip to content

Commit 857912f

Browse files
authored
Address PROTOTYPE comment in ExtensionMethodBodyRewriter (#77782)
1 parent 67a009f commit 857912f

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ protected BoundTreeRewriterWithStackGuardWithoutRecursionOnTheLeftOfBinaryOperat
249249

250250
if (child.Kind != BoundKind.BinaryOperator)
251251
{
252-
return base.VisitBinaryOperator(node);
252+
return node.Update(node.OperatorKind, VisitBinaryOperatorData(node), node.ResultKind, (BoundExpression)this.Visit(node.Left), (BoundExpression)this.Visit(node.Right), this.VisitType(node.Type));
253253
}
254254

255255
var stack = ArrayBuilder<BoundBinaryOperator>.GetInstance();
@@ -279,7 +279,7 @@ protected BoundTreeRewriterWithStackGuardWithoutRecursionOnTheLeftOfBinaryOperat
279279
var right = (BoundExpression?)this.Visit(binary.Right);
280280
Debug.Assert(right is { });
281281
var type = this.VisitType(binary.Type);
282-
left = binary.Update(binary.OperatorKind, binary.Data, binary.ResultKind, left, right, type);
282+
left = binary.Update(binary.OperatorKind, VisitBinaryOperatorData(binary), binary.ResultKind, left, right, type);
283283
}
284284
while (stack.Count > 0);
285285

@@ -289,6 +289,11 @@ protected BoundTreeRewriterWithStackGuardWithoutRecursionOnTheLeftOfBinaryOperat
289289
return left;
290290
}
291291

292+
protected virtual BoundBinaryOperator.UncommonData? VisitBinaryOperatorData(BoundBinaryOperator node)
293+
{
294+
return node.Data;
295+
}
296+
292297
public sealed override BoundNode? VisitIfStatement(BoundIfStatement node)
293298
{
294299
if (node.AlternativeOpt is not BoundIfStatement ifStatement)

src/Compilers/CSharp/Portable/Lowering/BoundTreeToDifferentEnclosingContextRewriter.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Microsoft.CodeAnalysis.CSharp
1616
/// a bound node rewriter that rewrites types properly (which in some cases the automatically-generated
1717
/// base class does not). This is used in the lambda rewriter, the iterator rewriter, and the async rewriter.
1818
/// </summary>
19-
internal abstract class BoundTreeToDifferentEnclosingContextRewriter : BoundTreeRewriterWithStackGuard
19+
internal abstract class BoundTreeToDifferentEnclosingContextRewriter : BoundTreeRewriterWithStackGuardWithoutRecursionOnTheLeftOfBinaryOperator
2020
{
2121
// A mapping from every local variable to its replacement local variable. Local variables are replaced when
2222
// their types change due to being inside of a generic method. Otherwise we reuse the original local (even
@@ -111,20 +111,12 @@ protected BoundBlock VisitBlock(BoundBlock node, bool removeInstrumentation)
111111
return TypeMap.SubstituteType(type).Type;
112112
}
113113

114-
public override BoundNode VisitBinaryOperator(BoundBinaryOperator node)
114+
protected override BoundBinaryOperator.UncommonData? VisitBinaryOperatorData(BoundBinaryOperator node)
115115
{
116116
// Local rewriter should have already rewritten interpolated strings into their final form of calls and gotos
117117
Debug.Assert(node.InterpolatedStringHandlerData is null);
118118

119-
return node.Update(
120-
node.OperatorKind,
121-
node.ConstantValueOpt,
122-
VisitMethodSymbol(node.Method),
123-
VisitType(node.ConstrainedToType),
124-
node.ResultKind,
125-
(BoundExpression)Visit(node.Left),
126-
(BoundExpression)Visit(node.Right),
127-
VisitType(node.Type));
119+
return BoundBinaryOperator.UncommonData.CreateIfNeeded(node.ConstantValueOpt, VisitMethodSymbol(node.Method), VisitType(node.ConstrainedToType), node.OriginalUserDefinedOperatorsOpt);
128120
}
129121

130122
public override BoundNode? VisitConversion(BoundConversion node)

src/Compilers/CSharp/Portable/Lowering/ExtensionMethodBodyRewriter.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,5 @@ public override BoundNode VisitFunctionPointerLoad(BoundFunctionPointerLoad node
187187
{
188188
return ExtensionMethodReferenceRewriter.VisitFunctionPointerLoad(this, node);
189189
}
190-
191-
// PROTOTYPE: Handle deep recursion on long chain of binary operators, etc.
192190
}
193191
}

src/Compilers/CSharp/Portable/Lowering/ExtensionMethodReferenceRewriter.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,5 +228,13 @@ public static BoundNode VisitFunctionPointerLoad(BoundTreeRewriter rewriter, Bou
228228
TypeSymbol? type = rewriter.VisitType(node.Type);
229229
return node.Update(targetMethod, constrainedToTypeOpt, type);
230230
}
231+
232+
protected override BoundBinaryOperator.UncommonData? VisitBinaryOperatorData(BoundBinaryOperator node)
233+
{
234+
Debug.Assert(node.Method is null ||
235+
(!node.Method.IsExtensionMethod && !node.Method.GetIsNewExtensionMember())); // Expression tree context. At the moment an operator cannot be an extension method
236+
237+
return base.VisitBinaryOperatorData(node);
238+
}
231239
}
232240
}

0 commit comments

Comments
 (0)