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
19.11k stars 4.04k forks source link

[VB] SemanticModel.GetOperation returns Nothing instead of IFieldInitializerOperation for modified identifier syntax #26794

Open mavasani opened 6 years ago

mavasani commented 6 years ago

Below test fails as SemanticModel.GetOperation returns Nothing instead of an IFieidlInitializerOperation:

        <Fact>
        Public Sub SimpleArrayCreation_WithImplicitArrayInitializer_02()
            Dim source = <![CDATA[
Class C
    Private s1(10) As Integer'BIND:"s1(10)"
End Class
    ]]>.Value

            Dim expectedOperationTree = <![CDATA[
IFieldInitializerOperation (Field: C.s1 As System.Int32()) (OperationKind.FieldInitializer, Type: null) (Syntax: 's1(10)')
  IArrayCreationOperation (OperationKind.ArrayCreation, Type: System.Int32(), IsImplicit) (Syntax: 's1(10)')
    Dimension Sizes(1):
        IBinaryOperation (BinaryOperatorKind.Add, Checked) (OperationKind.BinaryOperator, Type: System.Int32, Constant: 11, IsImplicit) (Syntax: '10')
          Left: 
            ILiteralOperation (OperationKind.Literal, Type: System.Int32, Constant: 10) (Syntax: '10')
          Right: 
            ILiteralOperation (OperationKind.Literal, Type: System.Int32, Constant: 1, IsImplicit) (Syntax: '10')
    Initializer: 
      null
]]>.Value

            Dim expectedDiagnostics = String.Empty

            VerifyOperationTreeAndDiagnosticsForTest(Of ModifiedIdentifierSyntax)(source, expectedOperationTree, expectedDiagnostics)
        End Sub

Related to bug https://github.com/dotnet/roslyn/issues/26780, but this one seems to be an underlying binder issue. Bound tree contains a BoundFieldInitializer node with the ModifiedIdentifierSyntax as its underlying syntax, but GetMemberSemanticModel returns Nothing for this node when invoked from GetOperation invocation on this node.

mavasani commented 6 years ago

While fixing this issue, please also add a flow analysis test for this case to verify that field initializer is rewritten into an assignment operation with the field reference as the target.