提交 28a42d8b 编写于 作者: V VSadov

New with tuple syntax

上级 37773774
......@@ -315,6 +315,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
resultKind = LookupResultKind.NotCreatable
errorReported = True
Case TypeKind.Array
' the diagnostic that AsNew cannot be used to init arrays has already been reported
' so there is nothing to do here.
......
......@@ -1706,6 +1706,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
ERR_TupleElementNamesAttributeMissing = 37268
ERR_ExplicitTupleElementNamesAttribute = 37269
ERR_TupleLiteralDisallowsTypeChar = 37270
ERR_NewWithTupleTypeSyntax = 37271
' Available 37270
ERR_DuplicateProcDefWithDifferentTupleNames2 = 37271
......
......@@ -864,6 +864,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax
' Here we are after parsing " New Blah(a1,a2)"
If Type.Kind = SyntaxKind.TupleType Then
Type = ReportSyntaxError(Type, ERRID.ERR_NewWithTupleTypeSyntax)
End If
Dim FromToken As KeywordSyntax = Nothing
If TryTokenAsContextualKeyword(CurrentToken, SyntaxKind.FromKeyword, FromToken) Then
......
......@@ -2252,6 +2252,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax
Else
typeName = ParseTypeName()
If typeName.Kind = SyntaxKind.TupleType Then
typeName = ReportSyntaxError(typeName, ERRID.ERR_NewWithTupleTypeSyntax)
End If
If CurrentToken.Kind = SyntaxKind.OpenParenToken Then
' New <Type> ( <Arguments> )
newArguments = ParseParenthesizedArguments()
......
......@@ -7800,6 +7800,15 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
End Get
End Property
'''<summary>
''' Looks up a localized string similar to &apos;New&apos; cannot be used with tuple type. Use a tuple literal expression instead..
'''</summary>
Friend ReadOnly Property ERR_NewWithTupleTypeSyntax() As String
Get
Return ResourceManager.GetString("ERR_NewWithTupleTypeSyntax", resourceCulture)
End Get
End Property
'''<summary>
''' Looks up a localized string similar to Next control variable does not match For loop control variable &apos;{0}&apos;..
'''</summary>
......@@ -10389,11 +10398,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
End Property
'''<summary>
''' Looks up a localized string similar to Tuple member names must be unique..
''' Looks up a localized string similar to Tuple element names must be unique..
'''</summary>
Friend ReadOnly Property ERR_TupleDuplicateMemberName() As String
Friend ReadOnly Property ERR_TupleDuplicateElementName() As String
Get
Return ResourceManager.GetString("ERR_TupleDuplicateMemberName", resourceCulture)
Return ResourceManager.GetString("ERR_TupleDuplicateElementName", resourceCulture)
End Get
End Property
......@@ -10407,20 +10416,29 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
End Property
'''<summary>
''' Looks up a localized string similar to Tuple member name &apos;{0}&apos; is only allowed at position {1}..
''' Looks up a localized string similar to Type characters cannot be used in tuple literals..
'''</summary>
Friend ReadOnly Property ERR_TupleLiteralDisallowsTypeChar() As String
Get
Return ResourceManager.GetString("ERR_TupleLiteralDisallowsTypeChar", resourceCulture)
End Get
End Property
'''<summary>
''' Looks up a localized string similar to Tuple element name &apos;{0}&apos; is only allowed at position {1}..
'''</summary>
Friend ReadOnly Property ERR_TupleReservedMemberName() As String
Friend ReadOnly Property ERR_TupleReservedElementName() As String
Get
Return ResourceManager.GetString("ERR_TupleReservedMemberName", resourceCulture)
Return ResourceManager.GetString("ERR_TupleReservedElementName", resourceCulture)
End Get
End Property
'''<summary>
''' Looks up a localized string similar to Tuple member name &apos;{0}&apos; is disallowed at any position..
''' Looks up a localized string similar to Tuple element name &apos;{0}&apos; is disallowed at any position..
'''</summary>
Friend ReadOnly Property ERR_TupleReservedMemberNameAnyPosition() As String
Friend ReadOnly Property ERR_TupleReservedElementNameAnyPosition() As String
Get
Return ResourceManager.GetString("ERR_TupleReservedMemberNameAnyPosition", resourceCulture)
Return ResourceManager.GetString("ERR_TupleReservedElementNameAnyPosition", resourceCulture)
End Get
End Property
......
......@@ -3166,6 +3166,9 @@
<data name="ERR_TupleLiteralDisallowsTypeChar" xml:space="preserve">
<value>Type characters cannot be used in tuple literals.</value>
</data>
<data name="ERR_NewWithTupleTypeSyntax" xml:space="preserve">
<value>'New' cannot be used with tuple type. Use a tuple literal expression instead.</value>
</data>
<data name="ERR_ExtensionMethodUncallable1" xml:space="preserve">
<value>Extension method '{0}' has type constraints that can never be satisfied.</value>
</data>
......
......@@ -1031,17 +1031,45 @@ Module C
t.a17 = 42
t.a12 = t.a17
console.writeline(t.a12)
TestArray()
TestNullable()
End Sub
Sub TestArray()
Dim t = New (a1 as Integer, a2 as Integer, a3 as Integer, a4 as integer,
a5 as integer, a6 as integer, a7 as integer, a8 as integer,
a9 as integer, a10 as Integer, a11 as Integer, a12 as Integer,
a13 as integer, a14 as integer, a15 as integer, a16 as integer,
a17 as integer, a18 as integer)() {Nothing}
t(0).a17 = 42
t(0).a12 = t(0).a17
console.writeline(t(0).a12)
End Sub
Sub TestNullable()
Dim t as New (a1 as Integer, a2 as Integer, a3 as Integer, a4 as integer,
a5 as integer, a6 as integer, a7 as integer, a8 as integer,
a9 as integer, a10 as Integer, a11 as Integer, a12 as Integer,
a13 as integer, a14 as integer, a15 as integer, a16 as integer,
a17 as integer, a18 as integer)?
console.writeline(t.HasValue)
End Sub
End Module
</file>
</compilation>, expectedOutput:=<![CDATA[
42
42
False
]]>, additionalRefs:=s_valueTupleRefs)
verifier.VerifyIL("C.Main", <![CDATA[
{
// Code size 64 (0x40)
// Code size 74 (0x4a)
.maxstack 2
.locals init (System.ValueTuple(Of Integer, Integer, Integer, Integer, Integer, Integer, Integer, (Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer)) V_0) //t
IL_0000: ldloca.s V_0
......@@ -1060,11 +1088,57 @@ End Module
IL_0030: ldfld "System.ValueTuple(Of Integer, Integer, Integer, Integer, Integer, Integer, Integer, (Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer)).Rest As (Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer)"
IL_0035: ldfld "System.ValueTuple(Of Integer, Integer, Integer, Integer, Integer, Integer, Integer, (Integer, Integer, Integer, Integer)).Item5 As Integer"
IL_003a: call "Sub System.Console.WriteLine(Integer)"
IL_003f: ret
IL_003f: call "Sub C.TestArray()"
IL_0044: call "Sub C.Testullable()"
IL_0049: ret
}
]]>)
End Sub
<Fact>
Public Sub TupleNewLongErr()
Dim comp = CreateCompilationWithMscorlibAndVBRuntime(
<compilation>
<file name="a.vb"><![CDATA[
Imports System
Module C
Sub Main()
Dim t = New (a1 as Integer, a2 as Integer, a3 as Integer, a4 as integer,
a5 as integer, a6 as integer, a7 as integer, a8 as integer,
a9 as integer, a10 as Integer, a11 as Integer, a12 as String,
a13 as integer, a14 as integer, a15 as integer, a16 as integer,
a17 as integer, a18 as integer)
console.writeline(t.a12.Length)
Dim t1 As New (a1 as Integer, a2 as Integer, a3 as Integer, a4 as integer,
a5 as integer, a6 as integer, a7 as integer, a8 as integer,
a9 as integer, a10 as Integer, a11 as Integer, a12 as String,
a13 as integer, a14 as integer, a15 as integer, a16 as integer,
a17 as integer, a18 as integer)
console.writeline(t1.a12.Length)
End Sub
End Module
]]></file>
</compilation>, additionalRefs:=s_valueTupleRefs)
' should not complain about missing constructor
comp.AssertTheseDiagnostics(
<errors>
BC37271: 'New' cannot be used with tuple type. Use a tuple literal expression instead.
Dim t = New (a1 as Integer, a2 as Integer, a3 as Integer, a4 as integer,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
BC37271: 'New' cannot be used with tuple type. Use a tuple literal expression instead.
Dim t1 As New (a1 as Integer, a2 as Integer, a3 as Integer, a4 as integer,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
</errors>)
End Sub
<Fact>
Public Sub TupleLiteralBinding()
......
......@@ -424,12 +424,46 @@ ToString]]>.Value)
expr = ParseExpression("New Moo() From{1,2,3}")
Assert.Equal(SyntaxKind.ObjectCreationExpression, expr.Kind)
End Sub
expr = ParseExpression("New (A, B)")
Assert.Equal(SyntaxKind.ObjectCreationExpression, expr.Kind)
<Fact>
Public Sub ParseNewTuple()
ParseAndVerify(<![CDATA[
Module Module1
Sub Main()
Dim x = New (A, B)
Dim y = New (A, B)()
Dim y = New (A As Integer, B$)
End Sub
End Module
]]>,
<errors>
<error id="37271" message="'New' cannot be used with tuple type. Use a tuple literal expression instead." start="87" end="93"/>
<error id="37271" message="'New' cannot be used with tuple type. Use a tuple literal expression instead." start="126" End="132"/>
<error id="37271" message="'New' cannot be used with tuple type. Use a tuple literal expression instead." start="167" End="185"/>
</errors>
)
expr = ParseExpression("New (A as Integer, B as integer)(1, 2)")
Assert.Equal(SyntaxKind.ObjectCreationExpression, expr.Kind)
ParseAndVerify(<![CDATA[
Module Module1
Sub Main()
Dim x As New (A, B)
Dim y As New (A, B)()
Dim y As New (A As Integer, B$)
End Sub
Public Function Bar() As (Alice As (Alice As Integer, Bob As Integer)(), Bob As Integer)
' this is actually ok, since it is an array
Return (New(Integer, Integer)() {(4, 5)}, 5)
End Function
End Module
]]>,
<errors>
<error id="37271" message="'New' cannot be used with tuple type. Use a tuple literal expression instead." start="88" end="94"/>
<error id="37271" message="'New' cannot be used with tuple type. Use a tuple literal expression instead." start="128" end="134"/>
<error id="37271" message="'New' cannot be used with tuple type. Use a tuple literal expression instead." start="170" end="188"/>
</errors>
)
End Sub
<Fact>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册