Skip to content
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

Call Enter/LeaveRegion APIs for binary expressions #76412

Merged
merged 8 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -2541,6 +2541,7 @@ private void VisitBinaryOperatorChildren(BoundBinaryOperator node)
do
{
stack.Push(binary);
EnterRegionIfNeeded(binary);
Copy link
Contributor Author

@RikkiGibson RikkiGibson Dec 13, 2024

Choose a reason for hiding this comment

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

We need to take care that Enter is called for all the binary ops, from outermost to innermost, and then Leave is called from innermost to outermost. Most convenient way to do this is to call Enter right after Push, then Leave right before "Pop or exit".

binary = binary.Left as BoundBinaryOperator;
}
while (binary != null && !binary.OperatorKind.IsLogical() && binary.InterpolatedStringHandlerData is null);
Expand All @@ -2550,6 +2551,7 @@ private void VisitBinaryOperatorChildren(BoundBinaryOperator node)
}

#nullable enable
/// <param name="stack">Nested left-associative binary operators, pushed on from outermost to innermost.</param>
protected virtual void VisitBinaryOperatorChildren(ArrayBuilder<BoundBinaryOperator> stack)
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
{
var binary = stack.Pop();
Expand Down Expand Up @@ -2592,6 +2594,7 @@ protected virtual void VisitBinaryOperatorChildren(ArrayBuilder<BoundBinaryOpera
SetConditionalState(isNullConstant == isEquals(binary)
? (State, stateWhenNotNull)
: (stateWhenNotNull, State));
LeaveRegionIfNeeded(binary);

if (stack.Count == 0)
{
Expand All @@ -2609,6 +2612,7 @@ protected virtual void VisitBinaryOperatorChildren(ArrayBuilder<BoundBinaryOpera
Unsplit();
VisitRvalue(binary.Right);
}
LeaveRegionIfNeeded(binary);

if (stack.Count == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14147,5 +14147,84 @@ ref struct RS
Assert.Null(GetSymbolNamesJoined(flowAnalysis.ReadInside));
Assert.Equal("this", GetSymbolNamesJoined(flowAnalysis.WrittenInside));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/38087")]
public void Repro_38087()
{
var comp = CreateCompilation("""
class Program
{
private static void Repro()
{
int i = 1, j = 2;
int k = i + j + 1;
}
}
""");
comp.VerifyEmitDiagnostics();

var tree = comp.CommonSyntaxTrees[0];
var model = comp.GetSemanticModel(tree);

var decls = tree.GetRoot().DescendantNodes().OfType<LocalDeclarationStatementSyntax>().ToArray();
Assert.Equal(2, decls.Length);
var decl = decls[1];
Assert.Equal("int k = i + j + 1;", decl.ToString());
var flowAnalysis = model.AnalyzeDataFlow(decl);
Assert.Equal("i, j", GetSymbolNamesJoined(flowAnalysis.ReadInside));

var binOps = tree.GetRoot().DescendantNodes().OfType<BinaryExpressionSyntax>().ToArray();
Assert.Equal(2, binOps.Length);
var add = binOps[0];
Assert.Equal("i + j + 1", add.ToString());
flowAnalysis = model.AnalyzeDataFlow(add);
Assert.Equal("i, j", GetSymbolNamesJoined(flowAnalysis.ReadInside));

add = binOps[1];
Assert.Equal("i + j", add.ToString());
flowAnalysis = model.AnalyzeDataFlow(add);
Assert.Equal("i, j", GetSymbolNamesJoined(flowAnalysis.ReadInside));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/38087")]
public void FourBinaryOperands()
{
var comp = CreateCompilation("""
class Program
{
private static void Repro()
{
int i = 1, j = 2, k = 3, l = 4;
_ = i + j + k + l;
}
}
""");
comp.VerifyEmitDiagnostics();

var tree = comp.CommonSyntaxTrees[0];
var model = comp.GetSemanticModel(tree);

var decl = tree.GetRoot().DescendantNodes().OfType<ExpressionStatementSyntax>().Single();
Assert.Equal("_ = i + j + k + l;", decl.ToString());
var flowAnalysis = model.AnalyzeDataFlow(decl);
Assert.Equal("i, j, k, l", GetSymbolNamesJoined(flowAnalysis.ReadInside));

var binOps = tree.GetRoot().DescendantNodes().OfType<BinaryExpressionSyntax>().ToArray();
Assert.Equal(3, binOps.Length);
var add = binOps[0];
Assert.Equal("i + j + k + l", add.ToString());
flowAnalysis = model.AnalyzeDataFlow(add);
Assert.Equal("i, j, k, l", GetSymbolNamesJoined(flowAnalysis.ReadInside));

add = binOps[1];
Assert.Equal("i + j + k", add.ToString());
flowAnalysis = model.AnalyzeDataFlow(add);
Assert.Equal("i, j, k", GetSymbolNamesJoined(flowAnalysis.ReadInside));

add = binOps[2];
Assert.Equal("i + j", add.ToString());
flowAnalysis = model.AnalyzeDataFlow(add);
Assert.Equal("i, j", GetSymbolNamesJoined(flowAnalysis.ReadInside));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9994,6 +9994,38 @@ End Module
capturedOutside)
End Sub

<WorkItem("https://github.com/dotnet/roslyn/issues/38087")>
<Fact()>
Public Sub NestedBinaryOperator()
Dim compilation = CreateCompilationWithMscorlib40AndVBRuntime(
<compilation>
<file name="a.vb">
Module Program
Sub M(i As Integer, j As Integer, k As Integer, l As Integer)
Dim x = i + j + k + l
End Sub
End Module
</file>
</compilation>)

Dim tree = compilation.SyntaxTrees.First()
Dim model = compilation.GetSemanticModel(tree)
Dim nodes = tree.GetRoot().DescendantNodes().OfType(Of BinaryExpressionSyntax)().ToArray()
Assert.Equal(3, nodes.Length)

Assert.Equal("i + j + k + l", nodes(0).ToString())
Dim dataFlowResults = model.AnalyzeDataFlow(nodes(0))
Assert.Equal("i, j, k, l", GetSymbolNamesJoined(dataFlowResults.ReadInside))

Assert.Equal("i + j + k", nodes(1).ToString())
dataFlowResults = model.AnalyzeDataFlow(nodes(1))
Assert.Equal("i, j, k", GetSymbolNamesJoined(dataFlowResults.ReadInside))

Assert.Equal("i + j", nodes(2).ToString())
dataFlowResults = model.AnalyzeDataFlow(nodes(2))
Assert.Equal("i, j", GetSymbolNamesJoined(dataFlowResults.ReadInside))
End Sub

#End Region

End Class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6048,4 +6048,35 @@ struct S1
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/38087")]
public async Task TestPartialSelectionOfArithmeticExpression()
{
await TestInRegularAndScript1Async(
"""
class C
{
private void Repro()
{
int i = 1, j = 2;
int k = [|i + j|] + 1;
}
}
""",
"""
class C
{
private void Repro()
{
int i = 1, j = 2;
int k = {|Rename:NewMethod|}(i, j) + 1;
}

private static int NewMethod(int i, int j)
{
return i + j;
}
}
""");
}
}