Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-8966

Exclusive NumberRange is converted to inclusive when used by List.getAt(range).

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.5.4
    • 3.0.0, 2.5.12
    • groovy-runtime
    • None

    Description

      When constructing an exclusive range, if the types of the left and right sides are explicitly typed and aren't both int the result at runtime is an instance of `NumberRange`.  When this is passed to the List.getAt helper method it calls `DefaultGroovyMethodsSupport.subListBorders` which at line 88 returns an instance of `RangeInfo` for an inclusive range.

      Contrast this with the code in IntRange which explicitly checks the `inclusive` flag and returns the correct range info.

      The following code blocks runs:

      def array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      int x = 10
      println array[x..<11]
      

      The following code produces an IndexOutOfBounds exception:

      def array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      int x = 10
      println array[x..<11L]
      
      // Also broken
      def array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      long x = 10
      println array[x..<11L]
      
      // Another broken variant
      def array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      Long x = 10
      println array[x..<11L]

      Attachments

        Issue Links

          Activity

            People

              daniel_sun Daniel Sun
              sfitts Sean Fitts
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 0.5h
                  0.5h