diff --git a/src/Compilers/VisualBasic/Portable/Binding/Binder_Initializers.vb b/src/Compilers/VisualBasic/Portable/Binding/Binder_Initializers.vb index e3bc26cb1abdeafa4c59d37c756c75bc051e3bbd..b9760141b2e73915cc6351f4ddec0bef18f86f12 100644 --- a/src/Compilers/VisualBasic/Portable/Binding/Binder_Initializers.vb +++ b/src/Compilers/VisualBasic/Portable/Binding/Binder_Initializers.vb @@ -299,16 +299,20 @@ Namespace Microsoft.CodeAnalysis.VisualBasic diagnostics) Dim hasErrors = False - If fieldSymbols.Length > 1 Then + + ' In speculative semantic model scenarios equalsValueOrAsNewSyntax might have no parent. + If equalsValueOrAsNewSyntax.Parent IsNot Nothing Then Debug.Assert(fieldSymbols.Length = DirectCast(equalsValueOrAsNewSyntax.Parent, VariableDeclaratorSyntax).Names.Count) - For Each name In DirectCast(equalsValueOrAsNewSyntax.Parent, VariableDeclaratorSyntax).Names - If Not (name.ArrayRankSpecifiers.IsEmpty AndAlso name.ArrayBounds Is Nothing) Then - ' Arrays cannot be declared with AsNew syntax - ReportDiagnostic(diagnostics, name, ERRID.ERR_AsNewArray) - hasErrors = True - End If - Next + If equalsValueOrAsNewSyntax.Kind() = SyntaxKind.AsNewClause Then + For Each name In DirectCast(equalsValueOrAsNewSyntax.Parent, VariableDeclaratorSyntax).Names + If Not (name.ArrayRankSpecifiers.IsEmpty AndAlso name.ArrayBounds Is Nothing) Then + ' Arrays cannot be declared with AsNew syntax + ReportDiagnostic(diagnostics, name, ERRID.ERR_AsNewArray) + hasErrors = True + End If + Next + End If End If boundInitializers.Add(New BoundFieldOrPropertyInitializer( diff --git a/src/Compilers/VisualBasic/Portable/Binding/Binder_Statements.vb b/src/Compilers/VisualBasic/Portable/Binding/Binder_Statements.vb index 5339175356d280736d9de142dde1488bb8169332..fe8e0d071dc03305a70736b631202ffba509c744 100644 --- a/src/Compilers/VisualBasic/Portable/Binding/Binder_Statements.vb +++ b/src/Compilers/VisualBasic/Portable/Binding/Binder_Statements.vb @@ -994,7 +994,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic #If DEBUG Then For i = 0 To names.Count - 1 Debug.Assert(locals(i).InitializedByAsNew) - Debug.Assert(locals(i).InitializerOpt Is Nothing) + Debug.Assert(locals(i).InitializerOpt Is Nothing OrElse locals(i).InitializerOpt.Kind = BoundKind.BadExpression) Next #End If @@ -1170,7 +1170,12 @@ Namespace Microsoft.CodeAnalysis.VisualBasic If name.ArrayBounds IsNot Nothing Then ' It is an error to have both array bounds and an initializer expression If valueExpression IsNot Nothing Then - ReportDiagnostic(diagnostics, name, ERRID.ERR_InitWithExplicitArraySizes) + If Not isInitializedByAsNew Then + ReportDiagnostic(diagnostics, name, ERRID.ERR_InitWithExplicitArraySizes) + Else + ' Must have reported ERR_AsNewArray already. + Debug.Assert(valueExpression.Kind = BoundKind.BadExpression) + End If Else valueExpression = New BoundArrayCreation(name, boundArrayBounds, Nothing, type) End If diff --git a/src/Compilers/VisualBasic/Test/Semantic/Binding/BindingErrorTests.vb b/src/Compilers/VisualBasic/Test/Semantic/Binding/BindingErrorTests.vb index 96578c67a688c2f2e164a195de503f9e3075cb44..4936239c13c4aac5b2abe8adf767b099d3f31abf 100644 --- a/src/Compilers/VisualBasic/Test/Semantic/Binding/BindingErrorTests.vb +++ b/src/Compilers/VisualBasic/Test/Semantic/Binding/BindingErrorTests.vb @@ -1225,15 +1225,23 @@ BC30049: 'Redim' statement requires an array. Diagnostic(ERRID.ERR_ArrayRankLimit, "(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33)")) End Sub - - Public Sub BC30053ERR_AsNewArray() + + Public Sub BC30053ERR_AsNewArray_01() Dim compilation1 = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime( Module M1 Sub Foo() Dim c() As New System.Exception + Dim d(), e() As New System.Exception + Dim f(), g As New System.Exception + Dim h, i() As New System.Exception End Sub + + Dim x() As New System.Exception + Dim y(), z() As New System.Exception + Dim u(), v As New System.Exception + Dim w, q() As New System.Exception End Module ) @@ -1242,6 +1250,89 @@ BC30049: 'Redim' statement requires an array. BC30053: Arrays cannot be declared with 'New'. Dim c() As New System.Exception ~~~ +BC30053: Arrays cannot be declared with 'New'. + Dim d(), e() As New System.Exception + ~~~ +BC30053: Arrays cannot be declared with 'New'. + Dim d(), e() As New System.Exception + ~~~ +BC30053: Arrays cannot be declared with 'New'. + Dim f(), g As New System.Exception + ~~~ +BC30053: Arrays cannot be declared with 'New'. + Dim h, i() As New System.Exception + ~~~ +BC30053: Arrays cannot be declared with 'New'. + Dim x() As New System.Exception + ~~~ +BC30053: Arrays cannot be declared with 'New'. + Dim y(), z() As New System.Exception + ~~~ +BC30053: Arrays cannot be declared with 'New'. + Dim y(), z() As New System.Exception + ~~~ +BC30053: Arrays cannot be declared with 'New'. + Dim u(), v As New System.Exception + ~~~ +BC30053: Arrays cannot be declared with 'New'. + Dim w, q() As New System.Exception + ~~~ + + CompilationUtils.AssertTheseDiagnostics(compilation1, expectedErrors1) + End Sub + + + Public Sub BC30053ERR_AsNewArray_02() + Dim compilation1 = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime( + + + Module M1 + Sub Foo() + Dim c(1) As New System.Exception + Dim d(1), e(1) As New System.Exception + Dim f(1), g As New System.Exception + Dim h, i(1) As New System.Exception + End Sub + + Dim x(1) As New System.Exception + Dim y(1), z(1) As New System.Exception + Dim u(1), v As New System.Exception + Dim w, q(1) As New System.Exception + End Module + + ) + + Dim expectedErrors1 = +BC30053: Arrays cannot be declared with 'New'. + Dim c(1) As New System.Exception + ~~~ +BC30053: Arrays cannot be declared with 'New'. + Dim d(1), e(1) As New System.Exception + ~~~ +BC30053: Arrays cannot be declared with 'New'. + Dim d(1), e(1) As New System.Exception + ~~~ +BC30053: Arrays cannot be declared with 'New'. + Dim f(1), g As New System.Exception + ~~~ +BC30053: Arrays cannot be declared with 'New'. + Dim h, i(1) As New System.Exception + ~~~ +BC30053: Arrays cannot be declared with 'New'. + Dim x(1) As New System.Exception + ~~~~ +BC30053: Arrays cannot be declared with 'New'. + Dim y(1), z(1) As New System.Exception + ~~~~ +BC30053: Arrays cannot be declared with 'New'. + Dim y(1), z(1) As New System.Exception + ~~~~ +BC30053: Arrays cannot be declared with 'New'. + Dim u(1), v As New System.Exception + ~~~~ +BC30053: Arrays cannot be declared with 'New'. + Dim w, q(1) As New System.Exception + ~~~~ CompilationUtils.AssertTheseDiagnostics(compilation1, expectedErrors1) End Sub diff --git a/src/Compilers/VisualBasic/Test/Semantic/Binding/BindingObjectInitializerTests.vb b/src/Compilers/VisualBasic/Test/Semantic/Binding/BindingObjectInitializerTests.vb index c8fa3ff410af20082e8c570b15159d66bf24af6b..ebbe904571a0be8e424bdf63dddb5a45f22fb33f 100644 --- a/src/Compilers/VisualBasic/Test/Semantic/Binding/BindingObjectInitializerTests.vb +++ b/src/Compilers/VisualBasic/Test/Semantic/Binding/BindingObjectInitializerTests.vb @@ -1315,6 +1315,9 @@ 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} ~