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

Fix region analysis of == containing ?. #53687

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
28 changes: 26 additions & 2 deletions src/Compilers/CSharp/Portable/FlowAnalysis/AbstractFlowPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2363,8 +2363,32 @@ protected virtual void VisitBinaryOperatorChildren(ArrayBuilder<BoundBinaryOpera
&& canLearnFromOperator(binary)
&& isKnownNullOrNotNull(binary.Right))
{
VisitRvalue(binary.Right);
Meet(ref stateWhenNotNull, ref State);
if (_nonMonotonicTransfer)
{
// In this very specific scenario, we need to do extra work to track unassignments for region analysis.
// See `AbstractFlowPass.VisitCatchBlockWithAnyTransferFunction` for a similar scenario in catch blocks.
Optional<TLocalState> oldState = NonMonotonicState;
NonMonotonicState = ReachableBottomState();

VisitRvalue(binary.Right);

var tempStateValue = NonMonotonicState.Value;
Join(ref stateWhenNotNull, ref tempStateValue);
if (oldState.HasValue)
{
var oldStateValue = oldState.Value;
Join(ref oldStateValue, ref tempStateValue);
oldState = oldStateValue;
}

NonMonotonicState = oldState;
}
else
{
VisitRvalue(binary.Right);
Meet(ref stateWhenNotNull, ref State);
}

var isNullConstant = binary.Right.ConstantValue?.IsNull == true;
SetConditionalState(isNullConstant == isEquals(binary)
? (State, stateWhenNotNull)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2546,6 +2546,76 @@ public string M()
Assert.Null(GetSymbolNamesJoined(dataFlowAnalysis.CapturedOutside));
}

[Theory]
[InlineData("c?.M0(x = 0)")]
[InlineData("c!.M0(x = 0)")]
public void CondAccess_Equals_DataFlowsOut_01(string leftOperand)
{
var dataFlowAnalysis = CompileAndAnalyzeDataFlowExpression(@"
#nullable enable
class C
{
bool M0(object? obj) => false;

public static void M(C? c)
{
int x = 0;
if (" + leftOperand + @" == /*<bind>*/c!.M0(x = 0)/*</bind>*/)
{
x.ToString();
}

x.ToString();
}
}
");
Assert.Null(GetSymbolNamesJoined(dataFlowAnalysis.VariablesDeclared));
Assert.Equal("x", GetSymbolNamesJoined(dataFlowAnalysis.AlwaysAssigned));
Assert.Equal("c", GetSymbolNamesJoined(dataFlowAnalysis.DataFlowsIn));
Assert.Equal("x", GetSymbolNamesJoined(dataFlowAnalysis.DataFlowsOut));
Assert.Equal("c", GetSymbolNamesJoined(dataFlowAnalysis.ReadInside));
Assert.Equal("c, x", GetSymbolNamesJoined(dataFlowAnalysis.ReadOutside));
Assert.Equal("x", GetSymbolNamesJoined(dataFlowAnalysis.WrittenInside));
Assert.Equal("c, x", GetSymbolNamesJoined(dataFlowAnalysis.WrittenOutside));
Copy link
Member

@333fred 333fred Jun 3, 2021

Choose a reason for hiding this comment

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

c

Why is c considered written anywhere in this function? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It appears that this is how parameters are handled in general. For example, the following test will pass in the main branch:

        [Fact]
        public void Test1()
        {
            var results = CompileAndAnalyzeDataFlowExpression(@"
class C
{
    static void M(int i)
    {
        System.Console.Write(/*<bind>*/i/*</bind>*/);
    }
}
");
            Assert.Equal("i", GetSymbolNamesJoined(results.WrittenOutside));
        }

My speculation is that it might be useful to model a parameter as having been "written" at the beginning of a method.

Assert.Null(GetSymbolNamesJoined(dataFlowAnalysis.Captured));
Assert.Null(GetSymbolNamesJoined(dataFlowAnalysis.CapturedInside));
Assert.Null(GetSymbolNamesJoined(dataFlowAnalysis.CapturedOutside));
}

[Theory]
[InlineData("c?.M0(x = 0)")]
[InlineData("c!.M0(x = 0)")]
public void CondAccess_Equals_DataFlowsOut_02(string leftOperand)
{
var dataFlowAnalysis = CompileAndAnalyzeDataFlowExpression(@"
#nullable enable
class C
{
bool M0(object? obj) => false;

public static void M(C? c)
{
int x = 0;
if (" + leftOperand + @" == /*<bind>*/c!.M0(x = 0)/*</bind>*/)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the test case where behavior is affected by the implementation change. Without the change, we don't include x in the DataFlowsOut set in this scenario.

{
x.ToString();
}
}
}
");
Assert.Null(GetSymbolNamesJoined(dataFlowAnalysis.VariablesDeclared));
Assert.Equal("x", GetSymbolNamesJoined(dataFlowAnalysis.AlwaysAssigned));
Assert.Equal("c", GetSymbolNamesJoined(dataFlowAnalysis.DataFlowsIn));
Assert.Equal("x", GetSymbolNamesJoined(dataFlowAnalysis.DataFlowsOut));
Assert.Equal("c", GetSymbolNamesJoined(dataFlowAnalysis.ReadInside));
Assert.Equal("c, x", GetSymbolNamesJoined(dataFlowAnalysis.ReadOutside));
Assert.Equal("x", GetSymbolNamesJoined(dataFlowAnalysis.WrittenInside));
Assert.Equal("c, x", GetSymbolNamesJoined(dataFlowAnalysis.WrittenOutside));
Assert.Null(GetSymbolNamesJoined(dataFlowAnalysis.Captured));
Assert.Null(GetSymbolNamesJoined(dataFlowAnalysis.CapturedInside));
Assert.Null(GetSymbolNamesJoined(dataFlowAnalysis.CapturedOutside));
}

#endregion

#region "Statements"
Expand Down