提交 96496105 编写于 作者: A AlekseyTs

Ensure error BC30053 is properly reported for field declarations.

Fixed #2424.
上级 6fdefaff
......@@ -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(
......
......@@ -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
......
......@@ -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
<Fact()>
Public Sub BC30053ERR_AsNewArray()
<Fact, WorkItem(2424, "https://github.com/dotnet/roslyn/issues/2424")>
Public Sub BC30053ERR_AsNewArray_01()
Dim compilation1 = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime(
<compilation name="AsNewArray">
<file name="a.vb">
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
</file>
</compilation>)
......@@ -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
~~~
</errors>
CompilationUtils.AssertTheseDiagnostics(compilation1, expectedErrors1)
End Sub
<Fact, WorkItem(2424, "https://github.com/dotnet/roslyn/issues/2424")>
Public Sub BC30053ERR_AsNewArray_02()
Dim compilation1 = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime(
<compilation name="AsNewArray">
<file name="a.vb">
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
</file>
</compilation>)
Dim expectedErrors1 = <errors>
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
~~~~
</errors>
CompilationUtils.AssertTheseDiagnostics(compilation1, expectedErrors1)
End Sub
......
......@@ -1315,6 +1315,9 @@ End Module
Dim compilation = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime(source)
AssertTheseDiagnostics(compilation, <expected>
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}
~
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册