dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.92k stars 4.02k forks source link

VB data flow analysis inaccurate when variables uninitialized in a loop #3672

Open gafter opened 9 years ago

gafter commented 9 years ago

The following proposed test (which would be appropriate in RegionAnalysisTests.vb) demonstrates that Roslyn's data flow analysis API for VB does not recognize that a value of an "uninitialized variable" may flow from one iteration of a loop to the next. As a result, the extract-method operation on the region produces code that is semantically different from the original.

        <Fact()>
        Public Sub TestUninitializedVariableInLoop()
            Dim analysis = CompileAndAnalyzeDataFlow(
      <compilation name="TestDataFlowLateCall">
          <file name="a.b">
Imports System

Module Module1
    Sub Main(args As String())
        For i = 0 To 4
[|
            Dim nullableDec As Decimal?
            If i Mod 2 = 0 Then
                If nullableDec Is Nothing Then nullableDec = 0D
                nullableDec += 1
            End If
            Console.WriteLine(nullableDec)
|]
        Next
    End Sub
End Module
</file>
      </compilation>)
            Assert.Equal("i, nullableDec", GetSymbolNamesJoined(analysis.DataFlowsIn))
            Assert.Equal("nullableDec", GetSymbolNamesJoined(analysis.DataFlowsOut))
        End Sub
gafter commented 9 years ago

To be clear, this test fails today. DataFlowsIn is "i" and DataFlowsOut is empty.

gafter commented 9 years ago

/cc @AnthonyDGreen @heejaechang @AlekseyTs @ljw1004