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

Region analysis of nameof #55724

Merged
merged 1 commit into from
Aug 21, 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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public override BoundNode VisitLocalFunctionStatement(BoundLocalFunctionStatemen
return base.VisitLocalFunctionStatement(node);
}

public override BoundNode VisitNameOfOperator(BoundNameOfOperator node)
{
return node;
}

private void MakeSlots(ImmutableArray<ParameterSymbol> parameters)
{
// assign slots to the parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4774,6 +4774,60 @@ void N(System.Action x, System.Action y) { }
Assert.Equal("this", GetSymbolNamesJoined(analysis.DefinitelyAssignedOnExit));
}

[Fact, WorkItem(53591, "https://github.com/dotnet/roslyn/issues/53591")]
public void TestNameOfInLambda()
{
var analysis = CompileAndAnalyzeDataFlowExpression(@"
class C
{
void M()
{
Func<string> x = /*<bind>*/() => nameof(ClosureCreated)/*</bind>*/;
}
}");

Assert.True(analysis.Succeeded);
Assert.Null(GetSymbolNamesJoined(analysis.Captured));
Assert.Null(GetSymbolNamesJoined(analysis.CapturedInside));
Assert.Null(GetSymbolNamesJoined(analysis.CapturedOutside));
}

[Fact, WorkItem(53591, "https://github.com/dotnet/roslyn/issues/53591")]
public void TestNameOfWithAssignmentInLambda()
{
var analysis = CompileAndAnalyzeDataFlowExpression(@"
class C
{
void M()
{
Func<string> x = /*<bind>*/() => nameof(this = null)/*</bind>*/;
}
}");

Assert.True(analysis.Succeeded);
Assert.Null(GetSymbolNamesJoined(analysis.Captured));
Assert.Null(GetSymbolNamesJoined(analysis.CapturedInside));
Assert.Null(GetSymbolNamesJoined(analysis.CapturedOutside));
}

[Fact, WorkItem(53591, "https://github.com/dotnet/roslyn/issues/53591")]
public void TestUnreachableThisInLambda()
{
var analysis = CompileAndAnalyzeDataFlowExpression(@"
class C
{
void M()
{
Func<string> x = /*<bind>*/() => false ? this.ToString() : string.Empty/*</bind>*/;
}
}");

Assert.True(analysis.Succeeded);
Assert.Equal("this", GetSymbolNamesJoined(analysis.Captured));
Assert.Equal("this", GetSymbolNamesJoined(analysis.CapturedInside));
Assert.Null(GetSymbolNamesJoined(analysis.CapturedOutside));
}

[Fact]
public void TestReturnFromLambda()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35071,7 +35071,7 @@ public static void Main(string[] args)
var decl = GetOutVarDeclaration(tree, name);
var refs = GetReferences(tree, name).ToArray();
Assert.Equal(2, refs.Length);
VerifyModelForOutVar(model, decl, refs[0]);
VerifyModelForOutVar(model, decl, isDelegateCreation: false, isExecutableCode: false, isShadowed: false, references: refs[0]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please clarify what the significance of this change is?

Copy link
Member Author

Choose a reason for hiding this comment

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

The isExecutableCode: false is not the default for overload with fewer parameters. Here's what that drives:

            if (isExecutableCode)
            {
                Assert.True(dataFlow.Succeeded);
                Assert.True(dataFlow.VariablesDeclared.Contains(symbol, ReferenceEqualityComparer.Instance));

                if (!isDelegateCreation)
                {
                    Assert.True(dataFlow.AlwaysAssigned.Contains(symbol, ReferenceEqualityComparer.Instance));
                    Assert.True(dataFlow.WrittenInside.Contains(symbol, ReferenceEqualityComparer.Instance));

                    var flowsIn = FlowsIn(dataFlowParent, decl, references);
                    Assert.Equal(flowsIn,
                                 dataFlow.DataFlowsIn.Contains(symbol, ReferenceEqualityComparer.Instance));
                    Assert.Equal(flowsIn,
                                 dataFlow.ReadInside.Contains(symbol, ReferenceEqualityComparer.Instance));

                    Assert.Equal(FlowsOut(dataFlowParent, decl, references),
                                 dataFlow.DataFlowsOut.Contains(symbol, ReferenceEqualityComparer.Instance));
                    Assert.Equal(ReadOutside(dataFlowParent, references),
                                 dataFlow.ReadOutside.Contains(symbol, ReferenceEqualityComparer.Instance));

                    Assert.Equal(WrittenOutside(dataFlowParent, references),
                                 dataFlow.WrittenOutside.Contains(symbol, ReferenceEqualityComparer.Instance));
                }
            }

VerifyNotInScope(model, refs[1]);
var symbol = (ILocalSymbol)model.GetDeclaredSymbol(decl.Designation);
Assert.Equal("System.Int32", symbol.Type.ToTestDisplayString());
Expand Down