CodeGenMultiDimensionalArray.vb 39.3 KB
Newer Older
1
' Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
P
Pilchie 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

Imports Microsoft.CodeAnalysis.Test.Utilities
Imports Roslyn.Test.Utilities

Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests
    Public Class CodeGenMulti_dimentional
        Inherits BasicTestBase

        <Fact()>
        Public Sub MultidimendionalArrayCreateWithInitializer()
            CompileAndVerify(
            <compilation>
                <file name="a.vb">
public Module A
    Public Sub Main()
        Dim arr As Integer(,) = New Integer(1, 2) {{1, 2, 3}, {1, 2, 3}}
        System.Console.Write(arr(1, 1))
    End Sub
End Module
    </file>
22
            </compilation>, options:=TestOptions.ReleaseExe.WithModuleName("MODULE"),
P
Pilchie 已提交
23 24 25 26 27 28 29 30 31 32
            expectedOutput:="2").
                        VerifyIL("A.Main",
            <![CDATA[
{
  // Code size       31 (0x1f)
  .maxstack  3
  IL_0000:  ldc.i4.2
  IL_0001:  ldc.i4.3
  IL_0002:  newobj     "Integer(*,*)..ctor"
  IL_0007:  dup
33
  IL_0008:  ldtoken    "<PrivateImplementationDetails>.__StaticArrayInitTypeSize=24 <PrivateImplementationDetails>.D64E555B758C5B66DFAC42F18587BB1B3C9BCFA8"
P
Pilchie 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  IL_000d:  call       "Sub System.Runtime.CompilerServices.RuntimeHelpers.InitializeArray(System.Array, System.RuntimeFieldHandle)"
  IL_0012:  ldc.i4.1
  IL_0013:  ldc.i4.1
  IL_0014:  call       "Integer(*,*).Get"
  IL_0019:  call       "Sub System.Console.Write(Integer)"
  IL_001e:  ret
}
]]>)
        End Sub

        <Fact()>
        Public Sub MultidimendionalArrayCreateWithInitializer001()
            CompileAndVerify(
            <compilation>
                <file name="a.vb">
public Module A
    Public Sub Main()
        Dim arr As Integer(,) = New Integer(1, 2) {{1, 2, 3}, {1, Integer.Parse("42"), 3}}
        System.Console.Write(arr(1, 1))
    End Sub
End Module
    </file>
56
            </compilation>, options:=TestOptions.ReleaseExe.WithModuleName("MODULE"),
P
Pilchie 已提交
57 58 59 60 61 62 63 64 65 66
            expectedOutput:="42").
                        VerifyIL("A.Main",
            <![CDATA[
{
  // Code size       49 (0x31)
  .maxstack  5
  IL_0000:  ldc.i4.2
  IL_0001:  ldc.i4.3
  IL_0002:  newobj     "Integer(*,*)..ctor"
  IL_0007:  dup
67
  IL_0008:  ldtoken    "<PrivateImplementationDetails>.__StaticArrayInitTypeSize=24 <PrivateImplementationDetails>.A4B74E064E285570B3499538C5B205C3D0972FDF"
P
Pilchie 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
  IL_000d:  call       "Sub System.Runtime.CompilerServices.RuntimeHelpers.InitializeArray(System.Array, System.RuntimeFieldHandle)"
  IL_0012:  dup
  IL_0013:  ldc.i4.1
  IL_0014:  ldc.i4.1
  IL_0015:  ldstr      "42"
  IL_001a:  call       "Function Integer.Parse(String) As Integer"
  IL_001f:  call       "Integer(*,*).Set"
  IL_0024:  ldc.i4.1
  IL_0025:  ldc.i4.1
  IL_0026:  call       "Integer(*,*).Get"
  IL_002b:  call       "Sub System.Console.Write(Integer)"
  IL_0030:  ret
}
]]>)
        End Sub

        <Fact()>
        Public Sub MultidimendionalArrayCreateWithInitializer002()
            CompileAndVerify(
            <compilation>
                <file name="a.vb">
public Module A
    Public Sub Main()
        Dim arr As Integer(,) = New Integer(1, 2) {{Integer.Parse("1"), Integer.Parse("2"), Integer.Parse("3")}, {Integer.Parse("4"), Integer.Parse("5"), Integer.Parse("6")}}
        System.Console.Write(arr(1, 1))
    End Sub
End Module
    </file>
            </compilation>,
            expectedOutput:="5").
                        VerifyIL("A.Main",
            <![CDATA[
{
  // Code size      128 (0x80)
  .maxstack  5
  IL_0000:  ldc.i4.2
  IL_0001:  ldc.i4.3
  IL_0002:  newobj     "Integer(*,*)..ctor"
  IL_0007:  dup
  IL_0008:  ldc.i4.0
  IL_0009:  ldc.i4.0
  IL_000a:  ldstr      "1"
  IL_000f:  call       "Function Integer.Parse(String) As Integer"
  IL_0014:  call       "Integer(*,*).Set"
  IL_0019:  dup
  IL_001a:  ldc.i4.0
  IL_001b:  ldc.i4.1
  IL_001c:  ldstr      "2"
  IL_0021:  call       "Function Integer.Parse(String) As Integer"
  IL_0026:  call       "Integer(*,*).Set"
  IL_002b:  dup
  IL_002c:  ldc.i4.0
  IL_002d:  ldc.i4.2
  IL_002e:  ldstr      "3"
  IL_0033:  call       "Function Integer.Parse(String) As Integer"
  IL_0038:  call       "Integer(*,*).Set"
  IL_003d:  dup
  IL_003e:  ldc.i4.1
  IL_003f:  ldc.i4.0
  IL_0040:  ldstr      "4"
  IL_0045:  call       "Function Integer.Parse(String) As Integer"
  IL_004a:  call       "Integer(*,*).Set"
  IL_004f:  dup
  IL_0050:  ldc.i4.1
  IL_0051:  ldc.i4.1
  IL_0052:  ldstr      "5"
  IL_0057:  call       "Function Integer.Parse(String) As Integer"
  IL_005c:  call       "Integer(*,*).Set"
  IL_0061:  dup
  IL_0062:  ldc.i4.1
  IL_0063:  ldc.i4.2
  IL_0064:  ldstr      "6"
  IL_0069:  call       "Function Integer.Parse(String) As Integer"
  IL_006e:  call       "Integer(*,*).Set"
  IL_0073:  ldc.i4.1
  IL_0074:  ldc.i4.1
  IL_0075:  call       "Integer(*,*).Get"
  IL_007a:  call       "Sub System.Console.Write(Integer)"
  IL_007f:  ret
}
]]>)
        End Sub

        <Fact()>
        Public Sub MultidimendionalArrayCreateWithInitializer003()
            CompileAndVerify(
            <compilation>
                <file name="a.vb">
Public Module A
    Public Sub Main()
        Dim arr As Integer(,) = New Integer(1, -1) {{}, {}}
        System.Console.Write(arr.Length)
    End Sub
End Module
    </file>
            </compilation>,
            expectedOutput:="0").
                        VerifyIL("A.Main",
            <![CDATA[
{
  // Code size       18 (0x12)
  .maxstack  2
  IL_0000:  ldc.i4.2
  IL_0001:  ldc.i4.0
  IL_0002:  newobj     "Integer(*,*)..ctor"
  IL_0007:  callvirt   "Function System.Array.get_Length() As Integer"
  IL_000c:  call       "Sub System.Console.Write(Integer)"
  IL_0011:  ret
}
]]>)
        End Sub

        <Fact()>
        Public Sub MultidimendionalArrayCreateWithInitializer004()
            CompileAndVerify(
            <compilation>
                <file name="a.vb">
Public Module A
    Public Sub Main()
        Dim arr As Integer(,) = New Integer(-1, -1) {}
        System.Console.Write(arr.Length)
    End Sub
End Module
    </file>
            </compilation>,
            expectedOutput:="0").
                        VerifyIL("A.Main",
            <![CDATA[
{
  // Code size       18 (0x12)
  .maxstack  2
  IL_0000:  ldc.i4.0
  IL_0001:  ldc.i4.0
  IL_0002:  newobj     "Integer(*,*)..ctor"
  IL_0007:  callvirt   "Function System.Array.get_Length() As Integer"
  IL_000c:  call       "Sub System.Console.Write(Integer)"
  IL_0011:  ret
}
]]>)
        End Sub

        <Fact()>
        Public Sub MultidimendionalArrayCreate()
            CompileAndVerify(
            <compilation>
                <file name="a.vb">
public Module A
    Public Sub Main()
        Dim arr As Integer(,) = New Integer(1,2) {}
        System.Console.Write(arr.Length)
    End Sub
End Module
    </file>
            </compilation>,
            expectedOutput:="6").
                        VerifyIL("A.Main",
            <![CDATA[
{
  // Code size       18 (0x12)
  .maxstack  2
  IL_0000:  ldc.i4.2
  IL_0001:  ldc.i4.3
  IL_0002:  newobj     "Integer(*,*)..ctor"
  IL_0007:  callvirt   "Function System.Array.get_Length() As Integer"
  IL_000c:  call       "Sub System.Console.Write(Integer)"
  IL_0011:  ret
}
]]>)
        End Sub

        <Fact()>
        Public Sub MultidimendionalArrayCreateGeneric()
            CompileAndVerify(
            <compilation>
                <file name="a.vb">
public Module A
    Public Sub Main()
        foo(Of String)()
        c1(Of Long).foo()
    End Sub

    Class c1(Of T)
        Public Shared Sub foo()
            Dim arr As T(,) = New T(1, 2) {}
            System.Console.Write(arr.Length)
        End Sub
    End Class

    Public Sub foo(Of T)()
        Dim arr As T(,) = New T(1, 2) {}
        System.Console.Write(arr.Length)
    End Sub
End Module
    </file>
            </compilation>,
            expectedOutput:="66").
                        VerifyIL("A.foo(Of T)()",
            <![CDATA[
{
  // Code size       18 (0x12)
  .maxstack  2
  IL_0000:  ldc.i4.2
  IL_0001:  ldc.i4.3
  IL_0002:  newobj     "T(*,*)..ctor"
  IL_0007:  callvirt   "Function System.Array.get_Length() As Integer"
  IL_000c:  call       "Sub System.Console.Write(Integer)"
  IL_0011:  ret
}
]]>)
        End Sub

        <Fact()>
        Public Sub MultidimendionalArrayGetSetAddress()
            CompileAndVerify(
            <compilation>
                <file name="a.vb">
public Module A
    Public Sub Main()
        foo(Of String)("hello")
        c1(Of Long).foo(123)
    End Sub

    Class c1(Of T)
        Public Shared Sub foo(e as T)
            Dim arr As T(,) = New T(2, 3) {}
            arr(1, 2) = e

            System.Console.Write(arr(1, 2).ToString)

            Dim v as T = arr(1, 2)
            System.Console.Write(v.ToString)
        End Sub
    End Class

    Public Sub foo(Of T)(e as T)
        Dim arr As T(,) = New T(2, 3) {}
            arr(1, 2) = e

            System.Console.Write(arr(1, 2).ToString)

            Dim v as T = arr(1, 2)
            System.Console.Write(v.ToString)
    End Sub
End Module
    </file>
            </compilation>,
            expectedOutput:="hellohello123123").
                        VerifyIL("A.foo(Of T)(T)",
            <![CDATA[
{
  // Code size       69 (0x45)
  .maxstack  5
  .locals init (T V_0) //v
  IL_0000:  ldc.i4.3
  IL_0001:  ldc.i4.4
  IL_0002:  newobj     "T(*,*)..ctor"
  IL_0007:  dup
  IL_0008:  ldc.i4.1
  IL_0009:  ldc.i4.2
  IL_000a:  ldarg.0
  IL_000b:  call       "T(*,*).Set"
  IL_0010:  dup
  IL_0011:  ldc.i4.1
  IL_0012:  ldc.i4.2
  IL_0013:  readonly.
  IL_0015:  call       "T(*,*).Address"
  IL_001a:  constrained. "T"
  IL_0020:  callvirt   "Function Object.ToString() As String"
  IL_0025:  call       "Sub System.Console.Write(String)"
  IL_002a:  ldc.i4.1
  IL_002b:  ldc.i4.2
  IL_002c:  call       "T(*,*).Get"
  IL_0031:  stloc.0
  IL_0032:  ldloca.s   V_0
  IL_0034:  constrained. "T"
  IL_003a:  callvirt   "Function Object.ToString() As String"
  IL_003f:  call       "Sub System.Console.Write(String)"
  IL_0044:  ret
}
]]>)
        End Sub

350
        <WorkItem(542259, "DevDiv")>
P
Pilchie 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
        <Fact>
        Public Sub MixMultiAndJaggedArray()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Imports System
Module Module1
    Sub Main()
        Dim x = New Exception(,,) {}
        Dim y = New Exception(,,) {{{}}}
        Dim z = New Exception(,)() {}
    End Sub
End Module
    </file>
</compilation>).VerifyDiagnostics()
        End Sub

        ' Declaration multi- dimensional array
        <Fact>
        Public Sub DeclarationmultiDimensionalArray()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Imports System
Module Module1
    Sub Main()
        Dim myArray1 As Integer(,) = New Integer(-1, -1) {}
        Dim myArray2 As Integer(,) = New Integer(3, 1) {}
        Dim myArray3 As Integer(,,) = New Integer(3, 1, 2) {}
        Dim myArray4 As Integer(,) = New Integer(2147483646, 2147483646) {}
        Dim myArray5 As Integer(,) = New Integer(2147483648UI - 1, 2147483648UI - 1) {}
    End Sub
End Module
    </file>
</compilation>).VerifyIL("Module1.Main", <![CDATA[
{
  // Code size       58 (0x3a)
  .maxstack  3
  IL_0000:  ldc.i4.0
  IL_0001:  ldc.i4.0
  IL_0002:  newobj     "Integer(*,*)..ctor"
  IL_0007:  pop
  IL_0008:  ldc.i4.4
  IL_0009:  ldc.i4.2
  IL_000a:  newobj     "Integer(*,*)..ctor"
  IL_000f:  pop
  IL_0010:  ldc.i4.4
  IL_0011:  ldc.i4.2
  IL_0012:  ldc.i4.3
  IL_0013:  newobj     "Integer(*,*,*)..ctor"
  IL_0018:  pop
  IL_0019:  ldc.i4     0x7fffffff
  IL_001e:  ldc.i4     0x7fffffff
  IL_0023:  newobj     "Integer(*,*)..ctor"
  IL_0028:  pop
  IL_0029:  ldc.i4     0x80000000
  IL_002e:  ldc.i4     0x80000000
  IL_0033:  newobj     "Integer(*,*)..ctor"
  IL_0038:  pop
  IL_0039:  ret
}
]]>)
        End Sub

        ' Declaration multi- dimensional array
        <Fact>
        Public Sub DeclarationmultiDimensionalArray_1()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Imports System
Module Module1
    Sub Main()
        Dim myArray6 As Integer(,,,,) = Nothing
        myArray6 = New Integer(4, 5, 8, 3, 6) {}
    End Sub
End Module
    </file>
</compilation>).VerifyIL("Module1.Main", <![CDATA[
{
  // Code size       13 (0xd)
  .maxstack  5
  IL_0000:  ldc.i4.5
  IL_0001:  ldc.i4.6
  IL_0002:  ldc.i4.s   9
  IL_0004:  ldc.i4.4
  IL_0005:  ldc.i4.7
  IL_0006:  newobj     "Integer(*,*,*,*,*)..ctor"
  IL_000b:  pop
  IL_000c:  ret
}
]]>)
        End Sub

        ' Declaration multi- dimensional array
        <Fact>
        Public Sub DeclarationmultiDimensionalArray_2()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Imports System
Module Module1
    Private Delegate Sub myDelegate(myString As String)
    Sub Main()
        Dim myArray1 As myInterface(,) = New myInterface(3, 1) {}
        Dim myArray2 As myDelegate(,) = New myDelegate(3, 1) {}
        Dim myArray3 = New Integer(Number.One, Number.Two) {} 
    End Sub
End Module
Interface myInterface
End Interface
Enum Number
    One
    Two
End Enum
    </file>
</compilation>).VerifyIL("Module1.Main", <![CDATA[
{
  // Code size       25 (0x19)
  .maxstack  2
  IL_0000:  ldc.i4.4
  IL_0001:  ldc.i4.2
  IL_0002:  newobj     "myInterface(*,*)..ctor"
  IL_0007:  pop
  IL_0008:  ldc.i4.4
  IL_0009:  ldc.i4.2
  IL_000a:  newobj     "Module1.myDelegate(*,*)..ctor"
  IL_000f:  pop
  IL_0010:  ldc.i4.1
  IL_0011:  ldc.i4.2
  IL_0012:  newobj     "Integer(*,*)..ctor"
  IL_0017:  pop
  IL_0018:  ret
}
]]>)
        End Sub

        ' Declaration multi- dimensional array
        <Fact>
        Public Sub DeclarationmultiDimensionalArray_3()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Class C1
    Public Shared Sub Main()
        Dim myLength As Integer = 3
        Dim arr As Integer(,) = New Integer(myLength, 1) {}
    End Sub
    Private Class A
        Private x As Integer = 1
        Private arr As Integer(,) = New Integer(x, 4) {}
    End Class
End Class
    </file>
</compilation>).VerifyIL("C1.Main", <![CDATA[
{
  // Code size       11 (0xb)
  .maxstack  2
  IL_0000:  ldc.i4.3
  IL_0001:  ldc.i4.1
  IL_0002:  add.ovf
  IL_0003:  ldc.i4.2
  IL_0004:  newobj     "Integer(*,*)..ctor"
  IL_0009:  pop
  IL_000a:  ret
}
]]>)
        End Sub

        ' Declaration multi- dimensional array
        <Fact>
        Public Sub DeclarationmultiDimensionalArray_5()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Imports System
Module Module1
    Sub Main()
        Dim myArray8 As Integer(,) = New Integer(-1, 4) {}
        Dim arrDouble#(,) = New Double(1, 2) {}
        Dim arrDecimal@(,) = New Decimal(1, 2) {}
        Dim arrString$(,) = New String(1, 2) {}
        Dim arrInteger%(,) = New Integer(1, 2) {}
        Dim arrLong&amp;(,) = New Long(1, 2) {}
        Dim arrSingle!(,) = New Single(1, 2) {}
    End Sub
End Module
    </file>
</compilation>).VerifyIL("Module1.Main", <![CDATA[
{
  // Code size       57 (0x39)
  .maxstack  2
  IL_0000:  ldc.i4.0
  IL_0001:  ldc.i4.5
  IL_0002:  newobj     "Integer(*,*)..ctor"
  IL_0007:  pop
  IL_0008:  ldc.i4.2
  IL_0009:  ldc.i4.3
  IL_000a:  newobj     "Double(*,*)..ctor"
  IL_000f:  pop
  IL_0010:  ldc.i4.2
  IL_0011:  ldc.i4.3
  IL_0012:  newobj     "Decimal(*,*)..ctor"
  IL_0017:  pop
  IL_0018:  ldc.i4.2
  IL_0019:  ldc.i4.3
  IL_001a:  newobj     "String(*,*)..ctor"
  IL_001f:  pop
  IL_0020:  ldc.i4.2
  IL_0021:  ldc.i4.3
  IL_0022:  newobj     "Integer(*,*)..ctor"
  IL_0027:  pop
  IL_0028:  ldc.i4.2
  IL_0029:  ldc.i4.3
  IL_002a:  newobj     "Long(*,*)..ctor"
  IL_002f:  pop
  IL_0030:  ldc.i4.2
  IL_0031:  ldc.i4.3
  IL_0032:  newobj     "Single(*,*)..ctor"
  IL_0037:  pop
  IL_0038:  ret
}
]]>)
        End Sub

        ' Initialize multi- dimensional array
        <Fact()>
        Public Sub InitializemultiDimensionalArray()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Option Infer On
Imports System
Imports Microsoft.VisualBasic.Information
Module Module1
    Sub Main()
        Dim myArray1 As Integer(,,) = {{{0, 1}, {2, 3}}, {{4, 5}, {6, 7}}}
        Dim myArray2 As Single(,) = New Single(2, 1) {{CSng(1.0), CSng(2.0)}, {CSng(3.0), CSng(4.0)}, {CSng(5.0), CSng(6.0)}}
        Dim myArray3 As Long(,) = New Long(,) {{1, 2}, {3, 4}, {5, 6}}
        Dim myArray4 As Char(,)
        myArray4 = New Char(2, 1) {{"1"c, "2"c}, {"3"c, "4"c}, {"5"c, "6"c}}
        Dim myArray5 As Decimal(,)
        myArray5 = New Decimal(,) {{CDec(1.0), CDec(2.0)}, {CDec(3.0), CDec(4.0)}, {CDec(5.0), CDec(6.0)}}
        Dim myArray6 As Integer(,) = New Integer(-1, -1) {}
        Dim myArray7 As Integer(,) = New Integer(,) {{}}
        Dim myArray8 As Integer(,,) = New Integer(,,) {{{}}}
        Dim myArray9 As String(,) = New String(2, 1) {{"a"c, "b"c}, {"c"c, "d"c}, {"e"c, "f"c}}
        Console.WriteLine(UBound(myArray1, 1))
        Console.WriteLine(UBound(myArray1, 2))
        Console.WriteLine(UBound(myArray1, 3))
        Console.WriteLine(UBound(myArray2, 1))
        Console.WriteLine(UBound(myArray2, 2))
        Console.WriteLine(UBound(myArray3, 1))
        Console.WriteLine(UBound(myArray3, 2))
        Console.WriteLine(UBound(myArray4, 1))
        Console.WriteLine(UBound(myArray4, 2))
        Console.WriteLine(UBound(myArray5, 1))
        Console.WriteLine(UBound(myArray5, 2))
        Console.WriteLine(UBound(myArray6, 1))
        Console.WriteLine(UBound(myArray6, 2))
        Console.WriteLine(UBound(myArray7, 1))
        Console.WriteLine(UBound(myArray7, 2))
        Console.WriteLine(UBound(myArray8, 1))
        Console.WriteLine(UBound(myArray8, 2))
        Console.WriteLine(UBound(myArray8, 3))
        Console.WriteLine(UBound(myArray9, 1))
        Console.WriteLine(UBound(myArray9, 2))
    End Sub
End Module
    </file>
</compilation>, expectedOutput:=<![CDATA[1
1
1
2
1
2
1
2
1
2
1
-1
-1
0
-1
0
0
-1
2
1]]>)
        End Sub

        ' Use different kinds of var as index upper bound
        <Fact>
        Public Sub DifferentVarAsBound()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Option Infer On        
Imports Microsoft.VisualBasic.Information
Module Module1
    Property prop As Integer
    Sub Main(args As String())
        Dim arr1(3, prop) As Integer
        Dim arr2(3, fun()) As Integer
        Dim x = fun()
        Dim arr3(x, 1) As Integer
        Dim z() As Integer
        Dim y() As Integer
        Dim replyCounts(,) As Short = New Short(UBound(z, 1), UBound(y, 1)) {}
    End Sub
    Function fun() As Integer
        Return 3
    End Function
    Sub foo(x As Integer)
        Dim arr1(3, x) As Integer
    End Sub
End Module
    </file>
</compilation>).VerifyIL("Module1.Main", <![CDATA[
{
  // Code size       67 (0x43)
  .maxstack  3
  .locals init (Integer() V_0, //z
  Integer() V_1) //y
  IL_0000:  ldc.i4.4
  IL_0001:  call       "Function Module1.get_prop() As Integer"
  IL_0006:  ldc.i4.1
  IL_0007:  add.ovf
  IL_0008:  newobj     "Integer(*,*)..ctor"
  IL_000d:  pop
  IL_000e:  ldc.i4.4
  IL_000f:  call       "Function Module1.fun() As Integer"
  IL_0014:  ldc.i4.1
  IL_0015:  add.ovf
  IL_0016:  newobj     "Integer(*,*)..ctor"
  IL_001b:  pop
  IL_001c:  call       "Function Module1.fun() As Integer"
  IL_0021:  ldc.i4.1
  IL_0022:  add.ovf
  IL_0023:  ldc.i4.2
  IL_0024:  newobj     "Integer(*,*)..ctor"
  IL_0029:  pop
  IL_002a:  ldloc.0
  IL_002b:  ldc.i4.1
  IL_002c:  call       "Function Microsoft.VisualBasic.Information.UBound(System.Array, Integer) As Integer"
  IL_0031:  ldc.i4.1
  IL_0032:  add.ovf
  IL_0033:  ldloc.1
  IL_0034:  ldc.i4.1
  IL_0035:  call       "Function Microsoft.VisualBasic.Information.UBound(System.Array, Integer) As Integer"
  IL_003a:  ldc.i4.1
  IL_003b:  add.ovf
  IL_003c:  newobj     "Short(*,*)..ctor"
  IL_0041:  pop
  IL_0042:  ret
}
]]>)
        End Sub

        ' Specify lower bound and up bound for multi-dimensional array
        <Fact>
        Public Sub SpecifyLowerAndUpBound()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Module Module1
    Property prop As Integer
    Sub Main(args As String())
        Dim arr1(0 To 0, 0 To -1) As Integer
    End Sub
End Module
    </file>
</compilation>).VerifyIL("Module1.Main", <![CDATA[
{
  // Code size        9 (0x9)
  .maxstack  2
  IL_0000:  ldc.i4.1
  IL_0001:  ldc.i4.0
  IL_0002:  newobj     "Integer(*,*)..ctor"
  IL_0007:  pop
  IL_0008:  ret
}
]]>)
        End Sub

        ' Array creation expression can be part of an anonymous object creation expression
        <Fact>
        Public Sub ArrayCreateAsAnonymous()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Option Infer On
Imports System
Imports Microsoft.VisualBasic.Information
Module Module1
    Sub Main(args As String())
        Dim a0 = New With {
         Key.b4 = New Integer(1, 2) {}, _
         Key.b5 = New Integer(1, 1) {{1, 2}, {2, 3}},
         Key.b6 = New Integer()() {New Integer(1) {}, New Integer(2) {}},
         Key.b7 = New Integer(2)() {},
         Key.b8 = New Integer(1)() {New Integer(0) {}, New Integer(1) {}},
         Key.b9 = New Integer() {1, 2, 3},
         Key.b10 = New Integer(,) {{1, 2}, {2, 3}}
        }
        Console.WriteLine(UBound(a0.b4, 1))
        Console.WriteLine(UBound(a0.b4, 2))
        Console.WriteLine(UBound(a0.b5, 1))
        Console.WriteLine(UBound(a0.b5, 2))
        Console.WriteLine(UBound(a0.b6, 1))
        Console.WriteLine(UBound(a0.b7, 1))
        Console.WriteLine(UBound(a0.b8, 1))
        Console.WriteLine(UBound(a0.b9, 1))
        Console.WriteLine(UBound(a0.b10, 1))
        Console.WriteLine(UBound(a0.b10, 2))
    End Sub
End Module
    </file>
</compilation>, expectedOutput:=<![CDATA[1
2
1
1
1
2
1
2
1
1]]>)
        End Sub

        ' Accessing an array's 0th element should work fine
783
        <WorkItem(528752, "DevDiv")>
P
Pilchie 已提交
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
        <Fact>
        Public Sub AccessZero()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Imports System
Module Module1
    Sub Main(args As String())
        Dim arr As Integer(,) = New Integer(4, 4) {}
        arr(0, 0) = 5
        Console.WriteLine(arr(0, 0))
    End Sub
End Module
    </file>
</compilation>, expectedOutput:=<![CDATA[5]]>).VerifyIL("Module1.Main", <![CDATA[
{
  // Code size       29 (0x1d)
  .maxstack  5
  IL_0000:  ldc.i4.5
  IL_0001:  ldc.i4.5
  IL_0002:  newobj     "Integer(*,*)..ctor"
  IL_0007:  dup
  IL_0008:  ldc.i4.0
  IL_0009:  ldc.i4.0
  IL_000a:  ldc.i4.5
  IL_000b:  call       "Integer(*,*).Set"
  IL_0010:  ldc.i4.0
  IL_0011:  ldc.i4.0
  IL_0012:  call       "Integer(*,*).Get"
  IL_0017:  call       "Sub System.Console.WriteLine(Integer)"
  IL_001c:  ret
}
]]>)
        End Sub

        ' Accessing an array's maxlength element should work fine
820
        <WorkItem(528752, "DevDiv")>
P
Pilchie 已提交
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
        <Fact>
        Public Sub AccessMaxLength()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Imports System
Module Module1
    Sub Main(args As String())
        Dim arr As Integer(,) = New Integer(4, 3) {}
        arr(4, 3) = 5
        Console.WriteLine(arr(4, 3))
    End Sub
End Module
    </file>
</compilation>, expectedOutput:=<![CDATA[5]]>).VerifyIL("Module1.Main", <![CDATA[
{
  // Code size       29 (0x1d)
  .maxstack  5
  IL_0000:  ldc.i4.5
  IL_0001:  ldc.i4.4
  IL_0002:  newobj     "Integer(*,*)..ctor"
  IL_0007:  dup
  IL_0008:  ldc.i4.4
  IL_0009:  ldc.i4.3
  IL_000a:  ldc.i4.5
  IL_000b:  call       "Integer(*,*).Set"
  IL_0010:  ldc.i4.4
  IL_0011:  ldc.i4.3
  IL_0012:  call       "Integer(*,*).Get"
  IL_0017:  call       "Sub System.Console.WriteLine(Integer)"
  IL_001c:  ret
}
]]>)
        End Sub

        ' Accessing an array's -1 element should throw an exception
857
        <WorkItem(528752, "DevDiv")>
P
Pilchie 已提交
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
        <Fact>
        Public Sub AccessLessThanMin()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Imports System
Module Module1
    Sub Main(args As String())
        Dim arr As Integer(,) = New Integer(4, 4) {}
        Try
            arr(-1, 1) = 5
            arr(1, -1) = 5
            arr(-1, -1) = 5
        Catch generatedExceptionName As System.IndexOutOfRangeException
        End Try
    End Sub
End Module
    </file>
</compilation>).VerifyIL("Module1.Main", <![CDATA[
{
  // Code size       52 (0x34)
  .maxstack  4
  .locals init (Integer(,) V_0, //arr
  System.IndexOutOfRangeException V_1) //generatedExceptionName
  IL_0000:  ldc.i4.5
  IL_0001:  ldc.i4.5
  IL_0002:  newobj     "Integer(*,*)..ctor"
  IL_0007:  stloc.0
  .try
{
  IL_0008:  ldloc.0
  IL_0009:  ldc.i4.m1
  IL_000a:  ldc.i4.1
  IL_000b:  ldc.i4.5
  IL_000c:  call       "Integer(*,*).Set"
  IL_0011:  ldloc.0
  IL_0012:  ldc.i4.1
  IL_0013:  ldc.i4.m1
  IL_0014:  ldc.i4.5
  IL_0015:  call       "Integer(*,*).Set"
  IL_001a:  ldloc.0
  IL_001b:  ldc.i4.m1
  IL_001c:  ldc.i4.m1
  IL_001d:  ldc.i4.5
  IL_001e:  call       "Integer(*,*).Set"
  IL_0023:  leave.s    IL_0033
}
  catch System.IndexOutOfRangeException
{
  IL_0025:  dup
  IL_0026:  call       "Sub Microsoft.VisualBasic.CompilerServices.ProjectData.SetProjectError(System.Exception)"
  IL_002b:  stloc.1
  IL_002c:  call       "Sub Microsoft.VisualBasic.CompilerServices.ProjectData.ClearProjectError()"
  IL_0031:  leave.s    IL_0033
}
  IL_0033:  ret
}
]]>)
        End Sub

        ' Accessing an array's maxlength+1 element should throw an exception
919
        <WorkItem(528752, "DevDiv")>
P
Pilchie 已提交
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
        <Fact>
        Public Sub AccessGreaterThanMax()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Imports System
Module Module1
    Sub Main(args As String())
        Dim arr As Integer(,) = New Integer(4, 3) {}
        Try
            arr(5, 3) = 5
            arr(4, 4) = 5
            arr(5, 4) = 5
        Catch
        End Try
    End Sub
End Module
    </file>
</compilation>).VerifyIL("Module1.Main", <![CDATA[
{
  // Code size       50 (0x32)
  .maxstack  4
  .locals init (Integer(,) V_0) //arr
  IL_0000:  ldc.i4.5
  IL_0001:  ldc.i4.4
  IL_0002:  newobj     "Integer(*,*)..ctor"
  IL_0007:  stloc.0
  .try
{
  IL_0008:  ldloc.0
  IL_0009:  ldc.i4.5
  IL_000a:  ldc.i4.3
  IL_000b:  ldc.i4.5
  IL_000c:  call       "Integer(*,*).Set"
  IL_0011:  ldloc.0
  IL_0012:  ldc.i4.4
  IL_0013:  ldc.i4.4
  IL_0014:  ldc.i4.5
  IL_0015:  call       "Integer(*,*).Set"
  IL_001a:  ldloc.0
  IL_001b:  ldc.i4.5
  IL_001c:  ldc.i4.4
  IL_001d:  ldc.i4.5
  IL_001e:  call       "Integer(*,*).Set"
  IL_0023:  leave.s    IL_0031
}
  catch System.Exception
{
  IL_0025:  call       "Sub Microsoft.VisualBasic.CompilerServices.ProjectData.SetProjectError(System.Exception)"
  IL_002a:  call       "Sub Microsoft.VisualBasic.CompilerServices.ProjectData.ClearProjectError()"
  IL_002f:  leave.s    IL_0031
}
  IL_0031:  ret
}
]]>)
        End Sub

        ' Accessing an array's index with a variable of type int, short, byte should work
978
        <WorkItem(528752, "DevDiv")>
P
Pilchie 已提交
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
        <Fact>
        Public Sub AccessWithDifferentType()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Option Infer On
Imports System
Module Module1
    Sub Main(args As String())
        Dim arr As Integer(,) = New Integer(4, 4) {}
        Dim idx As Integer = 2
        Dim idx1 As Byte = 1
        Dim idx3 As Short = 4
        Dim idx4 = 2.0
        Dim idx5 As Long = 3L
        arr(idx, 3) = 100
        arr(idx1, 3) = 100
        arr(idx3, 3) = 100
        arr(cint(idx4), 3) = 100
        arr(cint(idx5), 3) = 100
    End Sub
End Module
    </file>
</compilation>).VerifyIL("Module1.Main", <![CDATA[
{
  // Code size       85 (0x55)
  .maxstack  5
  .locals init (Integer V_0, //idx
  Byte V_1, //idx1
  Short V_2, //idx3
  Double V_3, //idx4
  Long V_4) //idx5
  IL_0000:  ldc.i4.5
  IL_0001:  ldc.i4.5
  IL_0002:  newobj     "Integer(*,*)..ctor"
  IL_0007:  ldc.i4.2
  IL_0008:  stloc.0
  IL_0009:  ldc.i4.1
  IL_000a:  stloc.1
  IL_000b:  ldc.i4.4
  IL_000c:  stloc.2
  IL_000d:  ldc.r8     2
  IL_0016:  stloc.3
  IL_0017:  ldc.i4.3
  IL_0018:  conv.i8
  IL_0019:  stloc.s    V_4
  IL_001b:  dup
  IL_001c:  ldloc.0
  IL_001d:  ldc.i4.3
  IL_001e:  ldc.i4.s   100
  IL_0020:  call       "Integer(*,*).Set"
  IL_0025:  dup
  IL_0026:  ldloc.1
  IL_0027:  ldc.i4.3
  IL_0028:  ldc.i4.s   100
  IL_002a:  call       "Integer(*,*).Set"
  IL_002f:  dup
  IL_0030:  ldloc.2
  IL_0031:  ldc.i4.3
  IL_0032:  ldc.i4.s   100
  IL_0034:  call       "Integer(*,*).Set"
  IL_0039:  dup
  IL_003a:  ldloc.3
  IL_003b:  call       "Function System.Math.Round(Double) As Double"
  IL_0040:  conv.ovf.i4
  IL_0041:  ldc.i4.3
  IL_0042:  ldc.i4.s   100
  IL_0044:  call       "Integer(*,*).Set"
  IL_0049:  ldloc.s    V_4
  IL_004b:  conv.ovf.i4
  IL_004c:  ldc.i4.3
  IL_004d:  ldc.i4.s   100
  IL_004f:  call       "Integer(*,*).Set"
  IL_0054:  ret
}
]]>)
        End Sub

        ' Passing an element to a function as a byVal or byRef parameter should work
1058
        <WorkItem(528752, "DevDiv")>
P
Pilchie 已提交
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
        <Fact>
        Public Sub ArrayAsArgument()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Imports System
Module Module1
    Sub Main(args As String())
        Dim arr As Integer(,) = New Integer(,) {{1, 2}}
        ElementTaker((arr(0, 1)))
        Console.WriteLine(arr(0, 1))
        ElementTaker(arr(0, 1))
        Console.WriteLine(arr(0, 1))
    End Sub
    Public Function ElementTaker(ByRef val As Integer) As Integer
        val = val + 5
        Return val
    End Function
End Module
    </file>
</compilation>, expectedOutput:=<![CDATA[2
7]]>).VerifyIL("Module1.Main", <![CDATA[
{
  // Code size       82 (0x52)
  .maxstack  5
  .locals init (Integer V_0)
  IL_0000:  ldc.i4.1
  IL_0001:  ldc.i4.2
  IL_0002:  newobj     "Integer(*,*)..ctor"
  IL_0007:  dup
  IL_0008:  ldc.i4.0
  IL_0009:  ldc.i4.0
  IL_000a:  ldc.i4.1
  IL_000b:  call       "Integer(*,*).Set"
  IL_0010:  dup
  IL_0011:  ldc.i4.0
  IL_0012:  ldc.i4.1
  IL_0013:  ldc.i4.2
  IL_0014:  call       "Integer(*,*).Set"
  IL_0019:  dup
  IL_001a:  ldc.i4.0
  IL_001b:  ldc.i4.1
  IL_001c:  call       "Integer(*,*).Get"
  IL_0021:  stloc.0
  IL_0022:  ldloca.s   V_0
  IL_0024:  call       "Function Module1.ElementTaker(ByRef Integer) As Integer"
  IL_0029:  pop
  IL_002a:  dup
  IL_002b:  ldc.i4.0
  IL_002c:  ldc.i4.1
  IL_002d:  call       "Integer(*,*).Get"
  IL_0032:  call       "Sub System.Console.WriteLine(Integer)"
  IL_0037:  dup
  IL_0038:  ldc.i4.0
  IL_0039:  ldc.i4.1
  IL_003a:  call       "Integer(*,*).Address"
  IL_003f:  call       "Function Module1.ElementTaker(ByRef Integer) As Integer"
  IL_0044:  pop
  IL_0045:  ldc.i4.0
  IL_0046:  ldc.i4.1
  IL_0047:  call       "Integer(*,*).Get"
  IL_004c:  call       "Sub System.Console.WriteLine(Integer)"
  IL_0051:  ret
}
]]>)
        End Sub

        ' Passing an element to a function as a byVal or byRef parameter should work
1127
        <WorkItem(528752, "DevDiv")>
P
Pilchie 已提交
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
        <Fact>
        Public Sub ArrayAsArgument_1()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Imports System
Module Module1
    Sub Main(args As String())
        Dim arr As Integer(,) = New Integer(,) {{1, 2}}
        ElementTaker(arr(0, 1))
        Console.WriteLine(arr(0, 1))
    End Sub
    Public Function ElementTaker(ByVal val As Integer) As Integer
        val = 5
        Return val
    End Function
End Module
    </file>
</compilation>, expectedOutput:=<![CDATA[2]]>).VerifyIL("Module1.Main", <![CDATA[
{
  // Code size       52 (0x34)
  .maxstack  5
  IL_0000:  ldc.i4.1
  IL_0001:  ldc.i4.2
  IL_0002:  newobj     "Integer(*,*)..ctor"
  IL_0007:  dup
  IL_0008:  ldc.i4.0
  IL_0009:  ldc.i4.0
  IL_000a:  ldc.i4.1
  IL_000b:  call       "Integer(*,*).Set"
  IL_0010:  dup
  IL_0011:  ldc.i4.0
  IL_0012:  ldc.i4.1
  IL_0013:  ldc.i4.2
  IL_0014:  call       "Integer(*,*).Set"
  IL_0019:  dup
  IL_001a:  ldc.i4.0
  IL_001b:  ldc.i4.1
  IL_001c:  call       "Integer(*,*).Get"
  IL_0021:  call       "Function Module1.ElementTaker(Integer) As Integer"
  IL_0026:  pop
  IL_0027:  ldc.i4.0
  IL_0028:  ldc.i4.1
  IL_0029:  call       "Integer(*,*).Get"
  IL_002e:  call       "Sub System.Console.WriteLine(Integer)"
  IL_0033:  ret
}
]]>)
        End Sub

        ' Assigning nothing to an array variable
1179
        <WorkItem(528752, "DevDiv")>
P
Pilchie 已提交
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
        <Fact>
        Public Sub AssignNothingToArray()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Imports System
Module Module1
    Public Sub Main()
        Dim arr As Integer(,) = New Integer(0, 1) {{1, 2}}
        Dim arr1 As Integer(,) = Nothing
        arr = Nothing
        arr(0, 1) = 3
        arr1(0, 1) = 3
    End Sub
End Module
    </file>
</compilation>).VerifyIL("Module1.Main", <![CDATA[
{
  // Code size       45 (0x2d)
  .maxstack  5
  IL_0000:  ldc.i4.1
  IL_0001:  ldc.i4.2
  IL_0002:  newobj     "Integer(*,*)..ctor"
  IL_0007:  dup
  IL_0008:  ldc.i4.0
  IL_0009:  ldc.i4.0
  IL_000a:  ldc.i4.1
  IL_000b:  call       "Integer(*,*).Set"
  IL_0010:  dup
  IL_0011:  ldc.i4.0
  IL_0012:  ldc.i4.1
  IL_0013:  ldc.i4.2
  IL_0014:  call       "Integer(*,*).Set"
  IL_0019:  pop
  IL_001a:  ldnull
  IL_001b:  ldnull
  IL_001c:  ldc.i4.0
  IL_001d:  ldc.i4.1
  IL_001e:  ldc.i4.3
  IL_001f:  call       "Integer(*,*).Set"
  IL_0024:  ldc.i4.0
  IL_0025:  ldc.i4.1
  IL_0026:  ldc.i4.3
  IL_0027:  call       "Integer(*,*).Set"
  IL_002c:  ret
}
]]>)
        End Sub

        ' Assigning a smaller array to a bigger array or vice versa should work
        <Fact>
        Public Sub AssignArrayToArray()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Imports System
Imports Microsoft.VisualBasic.Information
Module Program
    Public Sub Main()
        Dim arr1 As Integer(,) = New Integer(4, 1) {{1, 2}, {3, 4}, {5, 6}, {8, 9}, {100, 1210}}
        Dim arr2 As Integer(,) = New Integer(2, 1) {{6, 7}, {8, 1}, {2, 12}}
        arr1 = arr2
        Dim arr3 As Integer(,) = New Integer(1, 1) {{6, 7}, {9, 8}}
        Dim arr4 As Integer(,) = New Integer(2, 2) {{1, 2, 3}, {4, 5, 6}, {8, 0, 2}}
        arr3 = arr4
        Console.WriteLine(UBound(arr1, 1))
        Console.WriteLine(UBound(arr1, 2))
        Console.WriteLine(UBound(arr2, 1))
        Console.WriteLine(UBound(arr2, 2))
        Console.WriteLine(UBound(arr3, 1))
        Console.WriteLine(UBound(arr3, 2))
        Console.WriteLine(UBound(arr4, 1))
        Console.WriteLine(UBound(arr4, 2))
    End Sub
End Module
    </file>
</compilation>, expectedOutput:=<![CDATA[2
1
2
1
2
2
2
2]]>)
        End Sub

        ' Access index by enum
1267
        <WorkItem(528752, "DevDiv")>
P
Pilchie 已提交
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
        <Fact>
        Public Sub AccessIndexByEnum()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Imports System
Module Module1
    Public Sub Main()
        Dim cube As Integer(,) = New Integer(1, 2) {}
        cube(Number.One, Number.Two) = 1
        Console.WriteLine(cube(Number.One, Number.Two))
    End Sub
End Module
Enum Number
    One
    Two
End Enum
    </file>
</compilation>, expectedOutput:=<![CDATA[1]]>).VerifyIL("Module1.Main", <![CDATA[
{
  // Code size       29 (0x1d)
  .maxstack  5
  IL_0000:  ldc.i4.2
  IL_0001:  ldc.i4.3
  IL_0002:  newobj     "Integer(*,*)..ctor"
  IL_0007:  dup
  IL_0008:  ldc.i4.0
  IL_0009:  ldc.i4.1
  IL_000a:  ldc.i4.1
  IL_000b:  call       "Integer(*,*).Set"
  IL_0010:  ldc.i4.0
  IL_0011:  ldc.i4.1
  IL_0012:  call       "Integer(*,*).Get"
  IL_0017:  call       "Sub System.Console.WriteLine(Integer)"
  IL_001c:  ret
}
]]>)
        End Sub

        ' Assigning a struct variable to an element should work
1308
        <WorkItem(528752, "DevDiv")>
P
Pilchie 已提交
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
        <Fact>
        Public Sub AssigningStructToElement()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Imports System
Module Module1
    Public Sub Main()
        Dim arr As myStruct(,) = New myStruct(2, 1) {}
        Dim ms As myStruct
        ms.x = 4
        ms.y = 5
        arr(2, 0) = ms
    End Sub
End Module
Structure myStruct
    Public x As Integer
    Public y As Integer
End Structure
    </file>
</compilation>).VerifyIL("Module1.Main", <![CDATA[
{
  // Code size       32 (0x20)
  .maxstack  4
  .locals init (myStruct V_0) //ms
  IL_0000:  ldc.i4.3
  IL_0001:  ldc.i4.2
  IL_0002:  newobj     "myStruct(*,*)..ctor"
  IL_0007:  ldloca.s   V_0
  IL_0009:  ldc.i4.4
  IL_000a:  stfld      "myStruct.x As Integer"
  IL_000f:  ldloca.s   V_0
  IL_0011:  ldc.i4.5
  IL_0012:  stfld      "myStruct.y As Integer"
  IL_0017:  ldc.i4.2
  IL_0018:  ldc.i4.0
  IL_0019:  ldloc.0
  IL_001a:  call       "myStruct(*,*).Set"
  IL_001f:  ret
}
]]>)
        End Sub

        ' Using foreach on a multi-dimensional array
1353
        <WorkItem(528752, "DevDiv")>
P
Pilchie 已提交
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
        <Fact>
        Public Sub TravallingMultiDemensionalArray()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Imports System
Module Module1
    Public Sub Main()
        Dim arr As Integer(,,) = New Integer(,,) {{{1, 2}, {4, 5}}}
        For Each i As Integer In arr
            Console.WriteLine(i)
        Next
    End Sub
End Module
    </file>
</compilation>, expectedOutput:=<![CDATA[1
2
4
5]]>)
        End Sub

        ' Overload method by different dimension of array
        <Fact>
        Public Sub OverloadByDiffDimensionArray()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Module Program
    Sub Main(args As String())
    End Sub
    Sub foo(ByRef arg(,,) As Integer)
    End Sub
    Sub foo(ByRef arg(,) As Integer)
    End Sub
    Sub foo(ByRef arg() As Integer)
    End Sub
End Module
    </file>
</compilation>).VerifyDiagnostics()
        End Sub

        ' Multi-dimensional array args  could not as entry point 
        <Fact()>
        Public Sub MultidimensionalArgsAsEntryPoint()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Class B
    Public Shared Sub Main(args As String(,))
    End Sub
    Public Shared Sub Main()
    End Sub
End Class
Class M1
    Public Shared Sub Main(args As String(,))
    End Sub
End Class
    </file>
</compilation>).VerifyDiagnostics()
        End Sub

        ' Declare multi-dimensional and Jagged array
        <Fact>
        Public Sub MixedArray()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Imports Microsoft.VisualBasic.Information
Class program
    Public Shared Sub Main()
        Dim x = New Integer(,)() {}
        Dim y(,)() As Byte = New Byte(,)() {{New Byte() {2, 1, 3}}, {New Byte() {3, 0}}}
        System.Console.WriteLine(UBound(y, 1))
        System.Console.WriteLine(UBound(y, 2))
    End Sub
End Class
    </file>
</compilation>, expectedOutput:=<![CDATA[1
0]]>)
        End Sub

        ' Parse an Attribute instance that takes a generic type with an generic argument that is a multi-dimensional array
        <Fact>
        Public Sub Generic()
            CompileAndVerify(
<compilation>
    <file name="a.vb">
Imports System
&lt;TypeAttribute(GetType(Program(Of [String])(,)))&gt; _
Public Class Foo
End Class
Class Program(Of T)
End Class
Class TypeAttribute
    Inherits Attribute
    Public Sub New(value As Type)
    End Sub
End Class
    </file>
</compilation>).VerifyDiagnostics()
        End Sub


        <Fact()>
        Public Sub MDArrayTypeRef()
            Dim csCompilation = CreateCSharpCompilation("CS",
            <![CDATA[
public class A
{
    public static readonly string[,] dummy = new string[2, 2] { { "", "M" }, { "S", "SM" } };
}]]>,
                compilationOptions:=New Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
            csCompilation.VerifyDiagnostics()

            Dim vbCompilation = CreateVisualBasicCompilation("VB",
            <![CDATA[
Module Module1
    Sub Main()
        Dim x = A.dummy
        System.Console.Writeline(x(1,1))
    End Sub
End Module]]>,
A
angocke 已提交
1476
                compilationOptions:=New VisualBasicCompilationOptions(OutputKind.ConsoleApplication),
P
Pilchie 已提交
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
                referencedCompilations:={csCompilation})
            Dim vbVerifier = CompileAndVerify(vbCompilation,
                expectedOutput:=<![CDATA[
SM
]]>)
            vbVerifier.VerifyDiagnostics()
        End Sub


    End Class

1488
End Namespace