Scala Programming Language
  1. Scala Programming Language
  2. SI-6632

ListBuffer's insert, insertAll, update and updated accept negative positions

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Critical Critical
    • Resolution: Fixed
    • Affects Version/s: Scala 2.9.2, Scala 2.10.0-RC1, Scala 2.10.0-RC2, Scala 2.10.0, Scala 2.11.0-M1
    • Fix Version/s: Scala 2.11.0-RC1
    • Component/s: Collections
    • Environment:

      Scala version 2.9.2, Scala version 2.11.0-20121023-093421-b480d99107

      Description

      None of the following operations should succeed:

      scala> import collection.mutable.ListBuffer
      import collection.mutable.ListBuffer
      
      scala> val lb = ListBuffer('a, 'b, 'c, 'd, 'e)
      lb: scala.collection.mutable.ListBuffer[Symbol] = ListBuffer('a, 'b, 'c, 'd, 'e)
      
      scala> lb.insert(-1, 'x)
      
      scala> lb
      res19: scala.collection.mutable.ListBuffer[Symbol] = ListBuffer('a, 'x, 'b, 'c, 'd, 'e)
      
      scala> lb.insertAll(-2, Array('y, 'z))
      
      scala> lb
      res21: scala.collection.mutable.ListBuffer[Symbol] = ListBuffer('a, 'y, 'z, 'x, 'b, 'c, 'd, 'e)
      
      scala> lb.update(-3, 'u)
      
      scala> lb
      res23: scala.collection.mutable.ListBuffer[Symbol] = ListBuffer('a, 'u, 'z, 'x, 'b, 'c, 'd, 'e)
      
      scala> lb.updated(-4, 'd)
      res24: scala.collection.mutable.ListBuffer[Symbol] = ListBuffer('d, 'u, 'z, 'x, 'b, 'c, 'd, 'e)
      

      The documentation even specifies:

      IndexOutOfBoundsException
      if the index n is not in the valid range 0 <= n <= length. 
      

        Issue Links

          Activity

          Hide
          Seth Tisue added a comment -

          +1 on fixing for 2.11

          Show
          Seth Tisue added a comment - +1 on fixing for 2.11
          Hide
          Simon Ochsenreither added a comment -

          Yes, we should fix it. That it hasn't been fixed yet was just due to my limited time available, not the lack of desire.

          Imho a good way to start would be to survey whether there is some kind of implicit consensus in the other implementations on how to behave and it's just this one which behaves differently, or whether implementations' behavior in error cases is all over the place.
          Additionally, it would make sense to see how other languages/libraries deal with those kinds of input and try to learn whether their approach works better than maybe the straightforward approach we might gather while looking at Scala's collections solely.

          Show
          Simon Ochsenreither added a comment - Yes, we should fix it. That it hasn't been fixed yet was just due to my limited time available, not the lack of desire. Imho a good way to start would be to survey whether there is some kind of implicit consensus in the other implementations on how to behave and it's just this one which behaves differently, or whether implementations' behavior in error cases is all over the place. Additionally, it would make sense to see how other languages/libraries deal with those kinds of input and try to learn whether their approach works better than maybe the straightforward approach we might gather while looking at Scala's collections solely.
          Hide
          Simon Ochsenreither added a comment -

          This is kind of fishy, too:

          scala> import collection.mutable.{ArrayBuffer => ListBuffer}
          import collection.mutable.{ArrayBuffer=>ListBuffer}
          
          scala> val lb = ListBuffer('a, 'b, 'c, 'd, 'e)
          lb: scala.collection.mutable.ArrayBuffer[Symbol] = ArrayBuffer('a, 'b, 'c, 'd, 'e)
          
          scala> lb.updated(-4, 'd)
          res7: scala.collection.mutable.ArrayBuffer[Symbol] = ArrayBuffer('d, 'b, 'c, 'd, 'e)
          
          scala> lb.updated(12, 'd)
          java.lang.UnsupportedOperationException: empty.tail
            at scala.collection.TraversableLike$class.tail(TraversableLike.scala:449)
            at scala.collection.mutable.IndexedSeqLike$$anon$1.scala$collection$IndexedSeqOptimized$$super$tail(IndexedSeqLike.scala:52)
            at scala.collection.IndexedSeqOptimized$class.tail(IndexedSeqOptimized.scala:129)
            at scala.collection.mutable.IndexedSeqLike$$anon$1.tail(IndexedSeqLike.scala:52)
            at scala.collection.SeqLike$class.updated(SeqLike.scala:516)
            at scala.collection.AbstractSeq.updated(Seq.scala:41)
            ... 32 elided
          
          Show
          Simon Ochsenreither added a comment - This is kind of fishy, too: scala> import collection.mutable.{ArrayBuffer => ListBuffer} import collection.mutable.{ArrayBuffer=>ListBuffer} scala> val lb = ListBuffer('a, 'b, 'c, 'd, 'e) lb: scala.collection.mutable.ArrayBuffer[Symbol] = ArrayBuffer('a, 'b, 'c, 'd, 'e) scala> lb.updated(-4, 'd) res7: scala.collection.mutable.ArrayBuffer[Symbol] = ArrayBuffer('d, 'b, 'c, 'd, 'e) scala> lb.updated(12, 'd) java.lang.UnsupportedOperationException: empty.tail at scala.collection.TraversableLike$class.tail(TraversableLike.scala:449) at scala.collection.mutable.IndexedSeqLike$$anon$1.scala$collection$IndexedSeqOptimized$$super$tail(IndexedSeqLike.scala:52) at scala.collection.IndexedSeqOptimized$class.tail(IndexedSeqOptimized.scala:129) at scala.collection.mutable.IndexedSeqLike$$anon$1.tail(IndexedSeqLike.scala:52) at scala.collection.SeqLike$class.updated(SeqLike.scala:516) at scala.collection.AbstractSeq.updated(Seq.scala:41) ... 32 elided
          Hide
          Adriaan Moors added a comment -

          Since 2.11.0-RC1 is one week away, pushing all non-blockers without PR to 2.11.1-RC1. Please undo the change if I missed work in progress.

          Show
          Adriaan Moors added a comment - Since 2.11.0-RC1 is one week away, pushing all non-blockers without PR to 2.11.1-RC1. Please undo the change if I missed work in progress.
          Hide
          Rex Kerr added a comment -

          Fixed updated. Waiting for Jenkins to tell me the fallout, if any.

          https://github.com/scala/scala/pull/3530

          Show
          Rex Kerr added a comment - Fixed updated. Waiting for Jenkins to tell me the fallout, if any. https://github.com/scala/scala/pull/3530

            People

            • Assignee:
              Rex Kerr
              Reporter:
              Simon Ochsenreither
            • Votes:
              0 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development