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

AbstractFlowPass - VisitArrayCreation should use the regular way for visiting initializer. #57612

Merged
merged 1 commit into from
Nov 8, 2021
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
22 changes: 1 addition & 21 deletions src/Compilers/CSharp/Portable/FlowAnalysis/AbstractFlowPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1494,7 +1494,6 @@ public override BoundNode VisitBadStatement(BoundBadStatement node)
return null;
}

// Can be called as part of a bad expression.
public override BoundNode VisitArrayInitialization(BoundArrayInitialization node)
{
foreach (var child in node.Initializers)
Expand Down Expand Up @@ -2601,29 +2600,10 @@ public override BoundNode VisitArrayCreation(BoundArrayCreation node)
VisitRvalue(expr);
}

if (node.InitializerOpt != null)
{
VisitArrayInitializationInternal(node, node.InitializerOpt);
}

VisitRvalue(node.InitializerOpt);
return null;
}

private void VisitArrayInitializationInternal(BoundArrayCreation arrayCreation, BoundArrayInitialization node)
{
foreach (var child in node.Initializers)
{
if (child.Kind == BoundKind.ArrayInitialization)
{
VisitArrayInitializationInternal(arrayCreation, (BoundArrayInitialization)child);
}
else
{
VisitRvalue(child);
}
}
}

public override BoundNode VisitForStatement(BoundForStatement node)
{
if (node.Initializer != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8768,5 +8768,114 @@ public CustomHandler(int literalLength, int formattedCount" + (validityParameter
}

#endregion

[Fact]
public void TestDataFlowsArrayInit_01()
{
var analysis = CompileAndAnalyzeDataFlowExpression(@"
class C {
public void F(int x)
{
int a = 1, y = 2;
int[] b = /*<bind>*/{ a + x + 3 } /*</bind>*/;
int c = a + 4 + y;
}
}");
Assert.Equal("x, a", GetSymbolNamesJoined(analysis.DataFlowsIn));
}

[Fact]
public void TestDataFlowsArrayInit_02()
{
var analysis = CompileAndAnalyzeDataFlowExpression(@"
class C {
public void F(int x)
{
int a = 1, y = 2;
int[,] b = /*<bind>*/{ { a + x + 3 } }/*</bind>*/;
int c = a + 4 + y;
}
}");
Assert.Equal("x, a", GetSymbolNamesJoined(analysis.DataFlowsIn));
}

[Fact]
public void TestDataFlowsArrayInit_03()
{
var analysis = CompileAndAnalyzeDataFlowExpression(@"
class C {
public void F(int x)
{
int a = 1, y = 2;
int[,] b = {/*<bind>*/{ a + x + 3 } /*</bind>*/};
int c = a + 4 + y;
}
}");
Assert.Equal("x, a", GetSymbolNamesJoined(analysis.DataFlowsIn));
}

[Fact]
[WorkItem(57572, "https://github.com/dotnet/roslyn/issues/57572")]
public void TestDataFlowsArrayInit_04()
{
var analysis = CompileAndAnalyzeDataFlowExpression(@"
class C {
public void F(int x)
{
int a = 1, y = 2;
int[] b = new int[] /*<bind>*/{ a + x + 3 } /*</bind>*/;
int c = a + 4 + y;
}
}");
Assert.Equal("x, a", GetSymbolNamesJoined(analysis.DataFlowsIn));
}

[Fact]
[WorkItem(57572, "https://github.com/dotnet/roslyn/issues/57572")]
public void TestDataFlowsArrayInit_05()
{
var analysis = CompileAndAnalyzeDataFlowExpression(@"
class C {
public void F(int x)
{
int a = 1, y = 2;
int[,] b = new int[,] /*<bind>*/{ {a + x + 3} } /*</bind>*/;
int c = a + 4 + y;
}
}");
Assert.Equal("x, a", GetSymbolNamesJoined(analysis.DataFlowsIn));
}

[Fact]
[WorkItem(57572, "https://github.com/dotnet/roslyn/issues/57572")]
public void TestDataFlowsArrayInit_06()
{
var analysis = CompileAndAnalyzeDataFlowExpression(@"
class C {
public void F(int x)
{
int a = 1, y = 2;
int[,] b = new int[,] {/*<bind>*/{ a + x + 3 } /*</bind>*/};
int c = a + 4 + y;
}
}");
Assert.Equal("x, a", GetSymbolNamesJoined(analysis.DataFlowsIn));
}

[Fact]
public void TestDataFlowsObjectInit()
{
var analysis = CompileAndAnalyzeDataFlowExpression(@"
class C {
public int Data;
public void F(int x)
{
int a = 1, y = 2;
var b = new object() /*<bind>*/{ Data = a + x + 3 } /*</bind>*/;
int c = a + 4 + y;
}
}");
Assert.Equal("x, a", GetSymbolNamesJoined(analysis.DataFlowsIn));
}
}
}