groovy
  1. groovy
  2. GROOVY-4665

incorrect behavior of array subscript operator with reverse ranges

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.7.7, 1.8-beta-4
    • Fix Version/s: 2.2.0-rc-1
    • Component/s: groovy-jdk
    • Labels:
      None
    • Environment:
      any
    • Number of attachments :
      0

      Description

      reverse ranges like X..-Y are used in lists to represent forward ranges of the form X..list.size()-Y.
      this is not the case with arrays, as these special cases return a list that match the reverse range.

      this can easily be tested with this snippet

      def list = [1,2,3,4]
      int[] array = list
      assert list[2..-1] == [3,4]
      assert array[2..-1] == [3,4]
      

      which currently yields

      Assertion failed: 
      
      assert array[2..-1] == [3,4]
             |    |       |
             |    |       false
             |    [3, 2, 1, 4]
             [1, 2, 3, 4]
      

      we can track this to the protected DGM.primitiveArrayGet(Object,Range) that is called from DGM.getAt(int[],Range) and its siblings.

      this method just iterates the range to generate the returned list, whereas the list equivalent DGM.getAt(List,Range) makes use of DGMS.subListBorders(int,Range) to detect the reverse ranges.

        Activity

        Hide
        Pertino Juan added a comment - - edited

        note the title might be misleading, as only the described special cases work differently than expected.
        in fact, the problem is that the returned list matches exactly what the reverse range asked for...
        but you get the idea.

        (can issues be edited?)

        Show
        Pertino Juan added a comment - - edited note the title might be misleading, as only the described special cases work differently than expected. in fact, the problem is that the returned list matches exactly what the reverse range asked for... but you get the idea. (can issues be edited?)
        Hide
        Pascal Schumacher added a comment -

        The snippet works with Groovy-2.2.0-beta-3. I guess this was fixed by recent changes to the range operator in relation to lists.

        Show
        Pascal Schumacher added a comment - The snippet works with Groovy-2.2.0-beta-3. I guess this was fixed by recent changes to the range operator in relation to lists.
        Pascal Schumacher made changes -
        Field Original Value New Value
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 2.2.0-beta-3 [ 19569 ]
        Resolution Fixed [ 1 ]
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            Unassigned
            Reporter:
            Pertino Juan
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: