Groovy
  1. Groovy
  2. GROOVY-4665

incorrect behavior of array subscript operator with reverse ranges

    Details

    • Type: Bug Bug
    • Status: 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

      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

        Transition Time In Source Status Execution Times Last Executer Last Execution Date
        Open Open Resolved Resolved
        958d 5h 14m 1 Pascal Schumacher 22/Sep/13 03:08
        Resolved Resolved Closed Closed
        22d 14h 45m 1 Paul King 14/Oct/13 17:53
        Mark Thomas made changes -
        Workflow jira [ 12972685 ] Default workflow, editable Closed status [ 12980521 ]
        Mark Thomas made changes -
        Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
        Mark Thomas made changes -
        Workflow jira [ 12733597 ] Default workflow, editable Closed status [ 12745388 ]
        Mark Thomas made changes -
        Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Pascal Schumacher made changes -
        Field Original Value New Value
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Fixed [ 1 ]
        Fix Version/s 2.2.0-beta-3 [ 19569 ]
        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.
        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?)
        Pertino Juan created issue -

          People

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

            Dates

            • Created:
              Updated:
              Resolved:

              Development