' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. Imports System.Linq.Enumerable Imports System.Xml.Linq Imports Microsoft.CodeAnalysis Imports Microsoft.CodeAnalysis.Test.Utilities Imports Microsoft.CodeAnalysis.Text Imports Microsoft.CodeAnalysis.VisualBasic Imports Microsoft.CodeAnalysis.VisualBasic.Symbols Imports Microsoft.CodeAnalysis.VisualBasic.Syntax Imports Microsoft.CodeAnalysis.VisualBasic.UnitTests.Emit Imports Roslyn.Test.Utilities Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests Public Class BindingMemberInitializerTests Inherits BasicTestBase Public Sub SimpleObjectInitialization() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializationWithFieldOnRight() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerEmptyInitializers() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerMissingIdentifierInInitializer() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerOnlyDotIdentifierInInitializer() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerMissingExpressionInInitializer() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerKeyKeywordInInitializer() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerMultipleInitializations() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerInitializingObject() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerInitializingSameClass() Dim source = Option Strict On Imports System Class C1 Public Field1 as String = "Hello World!" Public Field2 as String Public Shared Sub Main() Dim c as new C1() c.Field1 = "No greeting today" c.DoStuff() End Sub Public Sub DoStuff() Dim c1 as New C1() With {.Field2 = .Field1} Console.WriteLine(c1.Field2) End Sub End Class CompileAndVerify(source, ) End Sub Public Sub ObjectInitializerInitializeSharedFieldOnNewInstance() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerInitializeNonExistentField() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerInitializeInaccessibleField() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerInitializeNonWriteableMember() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerInitializeReadOnlyProperty() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerInitializeReadOnlyField() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerPropertyWithInaccessibleSet() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerTypeIsErrorType() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of MethodBlockSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerNewTWith() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerTypeParametersInInitializers() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerNestedInWithStatement_1() Dim source = Imports System Class C1 Public Field As Integer = 42 End Class Class C3 Public Field2 As Integer = 23 End Class Class C2 Public Shared Sub Main() Dim x As New C1() x.Field = 23 ' test that initializer shadows fields With x Dim y As New C1() With {.Field = .Field}'BIND:"New C1() With {.Field = .Field}" Console.WriteLine(y.Field) ' should be 42 End With End Sub End Class CompileAndVerify(source, ) Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source.Value, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerNestedInWithStatement_2() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerNestedInitializers_1() Dim source = Imports System Class C1 Public Field As Integer = 1 Public FieldC2 as C2 End Class Class C2 Public Field As Integer = 2 End Class Class C3 Public Shared Sub Main() Dim x As New C1() With {.Field = 23, .FieldC2 = New C2() With {.Field = 42}}'BIND:"New C1() With {.Field = 23, .FieldC2 = New C2() With {.Field = 42}}" Console.WriteLine(x.Field) Console.WriteLine(x.FieldC2.Field) End Sub End Class CompileAndVerify(source, ) Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source.Value, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerNestedInitializers_2() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerCaptureFieldForLambda() Dim source = Imports System Class C1 Public Field As Integer = 42 Public Field2 As Func(Of Integer) End Class Class C1(Of T) Public Field As T End Class Class C2 Public Shared Sub Main() Dim y as new C1() y.Field = 23 Dim x As New C1 With {.Field2 = Function() As Integer'BIND:"New C1 With {.Field2 = Function() As Integer" Return .Field End Function} x.Field = 42 Console.WriteLine(x.Field2.Invoke()) End Sub End Class CompileAndVerify(source, ) Dim expectedOperationTree = As System.Int32 IReturnStatement (OperationKind.ReturnStatement) (Syntax: 'Return .Field') ReturnedValue: IFieldReferenceExpression: C1.Field As System.Int32 (OperationKind.FieldReferenceExpression, Type: System.Int32) (Syntax: '.Field') Instance Receiver: IOperation: (OperationKind.None) (Syntax: 'New C1 With ... d Function}') ILabeledStatement (Label: exit) (OperationKind.LabeledStatement) (Syntax: 'End Function') Statement: null IReturnStatement (OperationKind.ReturnStatement) (Syntax: 'End Function') ReturnedValue: ILocalReferenceExpression: (OperationKind.LocalReferenceExpression, Type: System.Int32) (Syntax: 'End Function') ]]>.Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source.Value, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerUsedInFieldInitializers() Dim source = Imports System Class C1 Public Field As Integer = 42 End Class Class C2 Private PrivateField As Integer = 23 Public C1Inst As C1 = New C1() With {.Field = PrivateField}'BIND:"New C1() With {.Field = PrivateField}" Public Shared Sub Main() Console.WriteLine((new C2()).C1Inst.Field) End Sub End Class CompileAndVerify(source, ) Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source.Value, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerFlowAnalysisVisitsInitializers() Dim source = Class C1 Public RefTypeField As C2 End Class Class C2 Public Function CreateC2() As C2 Return Nothing End Function Public Shared Sub Main() Dim y As C2 Dim x As New C1 With {.RefTypeField = y.CreateC2} End Sub End Class Dim compilation = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime(source) AssertTheseDiagnostics(compilation, BC42104: Variable 'y' is used before it has been assigned a value. A null reference exception could result at runtime. Dim x As New C1 With {.RefTypeField = y.CreateC2} ~ ) ' Yeah! We did not have this in Dev10 :) End Sub Public Sub ObjectInitializerInitializePropertyWithOptionalParameters() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerMemberAccessOnInitExpressionAllowsAllFields() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerUsingInitializedTargetInInitializerValueType() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerWithLifting_1() Dim source = .Value Dim expectedOperationTree = As System.Object IReturnStatement (OperationKind.ReturnStatement) (Syntax: 'Return .Field') ReturnedValue: IConversionExpression (Implicit, TryCast: False, Unchecked) (OperationKind.ConversionExpression, Type: System.Object) (Syntax: '.Field') Conversion: CommonConversion (Exists: True, IsIdentity: False, IsNumeric: False, IsReference: True, IsUserDefined: False) (MethodSymbol: null) Operand: IFieldReferenceExpression: C2.Field As System.Func(Of System.Object) (OperationKind.FieldReferenceExpression, Type: System.Func(Of System.Object)) (Syntax: '.Field') Instance Receiver: IOperation: (OperationKind.None) (Syntax: 'As New C2 W ... d Function}') ILabeledStatement (Label: exit) (OperationKind.LabeledStatement) (Syntax: 'End Function') Statement: null IReturnStatement (OperationKind.ReturnStatement) (Syntax: 'End Function') ReturnedValue: ILocalReferenceExpression: (OperationKind.LocalReferenceExpression, Type: System.Object) (Syntax: 'End Function') ISimpleAssignmentExpression (OperationKind.SimpleAssignmentExpression, Type: System.Func(Of System.Object)) (Syntax: '.Field2 = F ... nd Function') Left: IFieldReferenceExpression: C2.Field2 As System.Func(Of System.Object) (OperationKind.FieldReferenceExpression, Type: System.Func(Of System.Object)) (Syntax: 'Field2') Instance Receiver: IOperation: (OperationKind.None) (Syntax: 'As New C2 W ... d Function}') Right: IConversionExpression (Implicit, TryCast: False, Unchecked) (OperationKind.ConversionExpression, Type: System.Func(Of System.Object)) (Syntax: 'Function() ... nd Function') Conversion: CommonConversion (Exists: True, IsIdentity: False, IsNumeric: False, IsReference: False, IsUserDefined: False) (MethodSymbol: null) Operand: IAnonymousFunctionExpression (Symbol: Function () As System.Object) (OperationKind.AnonymousFunctionExpression, Type: null) (Syntax: 'Function() ... nd Function') IBlockStatement (3 statements, 1 locals) (OperationKind.BlockStatement) (Syntax: 'Function() ... nd Function') Locals: Local_1: As System.Object IReturnStatement (OperationKind.ReturnStatement) (Syntax: 'Return .Field') ReturnedValue: IConversionExpression (Implicit, TryCast: False, Unchecked) (OperationKind.ConversionExpression, Type: System.Object) (Syntax: '.Field') Conversion: CommonConversion (Exists: True, IsIdentity: False, IsNumeric: False, IsReference: True, IsUserDefined: False) (MethodSymbol: null) Operand: IFieldReferenceExpression: C2.Field As System.Func(Of System.Object) (OperationKind.FieldReferenceExpression, Type: System.Func(Of System.Object)) (Syntax: '.Field') Instance Receiver: IOperation: (OperationKind.None) (Syntax: 'As New C2 W ... d Function}') ILabeledStatement (Label: exit) (OperationKind.LabeledStatement) (Syntax: 'End Function') Statement: null IReturnStatement (OperationKind.ReturnStatement) (Syntax: 'End Function') ReturnedValue: ILocalReferenceExpression: (OperationKind.LocalReferenceExpression, Type: System.Object) (Syntax: 'End Function') ]]>.Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerDictionaryLookupOperatorSupported() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerInField() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ObjectInitializerNoStackOverflowFor150LevelsOfNesting() Dim source = Option Strict On Imports System Class Cust Public x As Cust Public y As Integer End Class Class C2 Public Shared Sub Main() Dim c As Cust = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = New Cust With {.y = 1, .x = nothing} }}}}}}}}}} }}}}}}}}}} }}}}}}}}}} }}}}}}}}}} }}}}}}}}}} }}}}}}}}}} }}}}}}}}}} }}}}}}}}}} }}}}}}}}}} }}}}}}}}}} }}}}}}}}}} }}}}}}}}}} }}}}}}}}}} }}}}}}}}}} End Sub End Class ' NOTE: Dev10 handled 416 levels of nesting, both algorithms are recursive, so there's not much to ' do for us here :( Dim compilation = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime(source) AssertTheseDiagnostics(compilation, ) End Sub Public Sub ObjectInitializerReferencingItself() Dim source = Structure NonEmptyStructure Public RefField1 As String Public ValField1 As Integer Public Property Goo As String End Structure Structure EmptyStructure Public Property Goo1 As Integer Public Property Goo2 As String End Structure Class RefClass Public RefField1 As String Public ValField1 As Integer Public Property Goo As String Public Property Goo1 As Integer Public Property Goo2 As String End Class Interface IMissingStuff2 Property Goo1 As Integer Property Goo2 As String End Interface Class ObjectInitializerClass Public Sub TypeParameterNotDefined(Of T As {IMissingStuff2, New})() Dim var20 As New T() With {.Goo1 = .Goo2.Length} ' Receiver type unknown, no warning Dim var21 As New T() With {.Goo1 = var21.Goo2.Length} ' Receiver type unknown, no warning Dim var22 As T = New T() With {.Goo1 = .Goo2.Length} ' Receiver type unknown, no warning Dim var23 As T = New T() With {.Goo1 = var23.Goo2.Length} ' Receiver type unknown, no warning End Sub Public Sub TypeParameterAsStructure(Of T As {Structure, IMissingStuff2})() Dim var24 As New T() With {.Goo1 = .Goo2.Length} ' no local referenced, no warning Dim var25 As New T() With {.Goo1 = var25.Goo2.Length} ' inplace initialized, no warning Dim var26 As T = New T() With {.Goo1 = .Goo2.Length} ' no local referenced, no warning Dim var27 As T = New T() With {.Goo1 = var27.Goo2.Length} ' temporary used, warning Dim var28, var29 As New T() With {.Goo1 = var28.Goo2.Length} ' no local referenced, no warning Dim var30, var31 As New T() With {.Goo1 = var31.Goo2.Length} ' Receiver type unknown, no warning End Sub Public Sub DoStuff_3(Of T As {RefClass, New})() Dim var32 As New T() With {.Goo1 = .Goo2.Length} ' no local referenced, no warning Dim var33 As New T() With {.Goo1 = var33.Goo2.Length} ' not yet initialized, warning Dim var34 As T = New T() With {.Goo1 = .Goo2.Length} ' no local referenced, no warning Dim var35 As T = New T() With {.Goo1 = var35.Goo2.Length} ' not yet initialized, warning Dim var36, var37 As New T() With {.Goo1 = var36.Goo2.Length} ' not yet initialized, warning Dim var38, var39 As New T() With {.Goo1 = var39.Goo2.Length} ' not yet initialized, warning End Sub Public Shared Sub Main() Dim var01 As New NonEmptyStructure() With {.ValField1 = var01.RefField1.Length, .RefField1 = var01.Goo} ' no warnings Dim var02, var03 As New NonEmptyStructure() With {.ValField1 = var03.RefField1.Length, .RefField1 = var03.Goo} ' warnings Dim var04, var05 As New NonEmptyStructure() With {.RefField1 = var04.Goo} ' no warnings Dim var06 As NonEmptyStructure = New NonEmptyStructure() With {.ValField1 = var06.RefField1.Length, .RefField1 = var06.Goo} ' warnings Dim var07 As New EmptyStructure() With {.Goo1 = var07.Goo2.Length} ' no warnings Dim var08, var09 As New EmptyStructure() With {.Goo1 = var09.Goo2.Length} ' warnings Dim var10, var11 As New EmptyStructure() With {.Goo1 = var10.Goo2.Length} ' no warnings Dim var12 As EmptyStructure = New EmptyStructure() With {.Goo1 = var12.Goo2.Length} ' warnings Dim var13 As New RefClass() With {.ValField1 = var13.RefField1.Length, .RefField1 = var13.Goo} ' no warnings Dim var14, var15 As New RefClass() With {.ValField1 = var15.RefField1.Length, .RefField1 = var15.Goo} ' warnings Dim var16, var17 As New RefClass() With {.ValField1 = var16.RefField1.Length, .RefField1 = var16.Goo} ' no warnings Dim var18 As RefClass = New RefClass() With {.ValField1 = var18.RefField1.Length, .RefField1 = var18.Goo} ' warnings Dim var19 = New RefClass() With {.ValField1 = var18.RefField1.Length, .RefField1 = var18.Goo} ' warnings End Sub End Class Class CObjInitBase(Of T) Public Overridable Sub TypeParameterValueTypeAsClassConstraint(Of U As {T, IMissingStuff2})() End Sub End Class Class CObjInitDerived Inherits CObjInitBase(Of NonEmptyStructure) Public Overrides Sub TypeParameterValueTypeAsClassConstraint(Of U As {NonEmptyStructure, IMissingStuff2})() Dim uinst1 As New U() With {.Goo1 = uinst1.Goo2.Length} Dim uinst2 As U = New U() With {.Goo1 = uinst2.Goo2.Length} End Sub End Class Dim compilation = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime(source) AssertTheseDiagnostics(compilation, BC42109: Variable 'var27' is used before it has been assigned a value. A null reference exception could result at runtime. Make sure the structure or all the reference members are initialized before use Dim var27 As T = New T() With {.Goo1 = var27.Goo2.Length} ' temporary used, warning ~~~~~ BC42109: Variable 'var31' is used before it has been assigned a value. A null reference exception could result at runtime. Make sure the structure or all the reference members are initialized before use Dim var30, var31 As New T() With {.Goo1 = var31.Goo2.Length} ' Receiver type unknown, no warning ~~~~~ BC42104: Variable 'var33' is used before it has been assigned a value. A null reference exception could result at runtime. Dim var33 As New T() With {.Goo1 = var33.Goo2.Length} ' not yet initialized, warning ~~~~~ BC42104: Variable 'var35' is used before it has been assigned a value. A null reference exception could result at runtime. Dim var35 As T = New T() With {.Goo1 = var35.Goo2.Length} ' not yet initialized, warning ~~~~~ BC42104: Variable 'var36' is used before it has been assigned a value. A null reference exception could result at runtime. Dim var36, var37 As New T() With {.Goo1 = var36.Goo2.Length} ' not yet initialized, warning ~~~~~ BC42104: Variable 'var39' is used before it has been assigned a value. A null reference exception could result at runtime. Dim var38, var39 As New T() With {.Goo1 = var39.Goo2.Length} ' not yet initialized, warning ~~~~~ BC42104: Variable 'RefField1' is used before it has been assigned a value. A null reference exception could result at runtime. Dim var02, var03 As New NonEmptyStructure() With {.ValField1 = var03.RefField1.Length, .RefField1 = var03.Goo} ' warnings ~~~~~~~~~~~~~~~ BC42109: Variable 'var03' is used before it has been assigned a value. A null reference exception could result at runtime. Make sure the structure or all the reference members are initialized before use Dim var02, var03 As New NonEmptyStructure() With {.ValField1 = var03.RefField1.Length, .RefField1 = var03.Goo} ' warnings ~~~~~ BC42104: Variable 'RefField1' is used before it has been assigned a value. A null reference exception could result at runtime. Dim var06 As NonEmptyStructure = New NonEmptyStructure() With {.ValField1 = var06.RefField1.Length, .RefField1 = var06.Goo} ' warnings ~~~~~~~~~~~~~~~ BC42109: Variable 'var06' is used before it has been assigned a value. A null reference exception could result at runtime. Make sure the structure or all the reference members are initialized before use Dim var06 As NonEmptyStructure = New NonEmptyStructure() With {.ValField1 = var06.RefField1.Length, .RefField1 = var06.Goo} ' warnings ~~~~~ BC42109: Variable 'var09' is used before it has been assigned a value. A null reference exception could result at runtime. Make sure the structure or all the reference members are initialized before use Dim var08, var09 As New EmptyStructure() With {.Goo1 = var09.Goo2.Length} ' warnings ~~~~~ BC42109: Variable 'var12' is used before it has been assigned a value. A null reference exception could result at runtime. Make sure the structure or all the reference members are initialized before use Dim var12 As EmptyStructure = New EmptyStructure() With {.Goo1 = var12.Goo2.Length} ' warnings ~~~~~ BC42104: Variable 'var13' is used before it has been assigned a value. A null reference exception could result at runtime. Dim var13 As New RefClass() With {.ValField1 = var13.RefField1.Length, .RefField1 = var13.Goo} ' no warnings ~~~~~ BC42104: Variable 'var15' is used before it has been assigned a value. A null reference exception could result at runtime. Dim var14, var15 As New RefClass() With {.ValField1 = var15.RefField1.Length, .RefField1 = var15.Goo} ' warnings ~~~~~ BC42104: Variable 'var16' is used before it has been assigned a value. A null reference exception could result at runtime. Dim var16, var17 As New RefClass() With {.ValField1 = var16.RefField1.Length, .RefField1 = var16.Goo} ' no warnings ~~~~~ BC42104: Variable 'var18' is used before it has been assigned a value. A null reference exception could result at runtime. Dim var18 As RefClass = New RefClass() With {.ValField1 = var18.RefField1.Length, .RefField1 = var18.Goo} ' warnings ~~~~~ BC42109: Variable 'uinst2' is used before it has been assigned a value. A null reference exception could result at runtime. Make sure the structure or all the reference members are initialized before use Dim uinst2 As U = New U() With {.Goo1 = uinst2.Goo2.Length} ~~~~~~ ) End Sub Public Sub Bug567976() Dim source = Module Program 'Sub Main() Dim b13() As New Integer() {1,2,3} End Sub End Module Dim compilation = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime(source) AssertTheseDiagnostics(compilation, BC30053: Arrays cannot be declared with 'New'. Dim b13() As New Integer() {1,2,3} ~~~~~ BC30205: End of statement expected. Dim b13() As New Integer() {1,2,3} ~ BC30429: 'End Sub' must be preceded by a matching 'Sub'. End Sub ~~~~~~~ ) End Sub Public Sub Bug599393() Dim source = End Class Public Module Module1 Sub Main() Try '<><%= ======================================================================================= %=> Dim cust = New Cust With {.Cust = 1, .Name = .Value, .e = .(0)} Dim cust2 = New Cust With {.Cust = 1, .Name = .Name, .e = ...(0)} Console.writeline("Scenario 8") Console.writeline(.ToString) Console.writeline( cust.e.ToString) Console.writeline( "Scenario 8.1") Console.writeline(.ToString) Console.writeline( cust2.e.ToString) Catch Finally End Try End Sub End Module ]]> Dim compilation = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntimeAndReferences(source, additionalRefs:=XmlReferences) CompileAndVerify(compilation) End Sub End Class End Namespace