Skip to content

Inconsistent copyToArray semantics between IterableOnceOps and Vector (and some other collections). #12948

Open
scala/scala
#10691
@noresttherein

Description

@noresttherein

Reproduction steps

Scala version: 2.13.12
I made another attempt to bring semantics of my collections exactly in line with the standard library and failed miserably.
I hinted at it #12795, but it gets worse.

	println(Vector(1).copyToArray(new Array[Int](1), Int.MinValue, 1))
	println(List(1).copyToArray(new Array[Int](1), Int.MinValue, 1))

Problem

0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -2147483648 out of bounds for length 20268
	at scala.runtime.ScalaRunTime$.array_update(ScalaRunTime.scala:75)
	at scala.collection.IterableOnceOps.copyToArray(IterableOnce.scala:926)
	at scala.collection.IterableOnceOps.copyToArray$(IterableOnce.scala:921)
	at scala.collection.AbstractIterable.copyToArray(Iterable.scala:933)
	at Playground$.delayedEndpoint$Playground$1(Playground.scala:20)
	at Playground$delayedInit$body.apply(Playground.scala:8)

Coincidentally,

	println(List(1).copyToArray(new Array[Int](1), -1, -1))
	println(List(1).copyToArray(new Array[Int](1), Int.MinValue, -1))

yields the same result:

0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -2147483648 out of bounds for length 1
	at scala.runtime.ScalaRunTime$.array_update(ScalaRunTime.scala:75)
	at scala.collection.IterableOnceOps.copyToArray(IterableOnce.scala:926)
	at scala.collection.IterableOnceOps.copyToArray$(IterableOnce.scala:921)
	at scala.collection.AbstractIterable.copyToArray(Iterable.scala:933)
	at Playground$.delayedEndpoint$Playground$1(Playground.scala:22)
	at Playground$delayedInit$body.apply(Playground.scala:8)

I know one probably has to be autistic to be bothered by it enough to fix it, given limited resources, but is there an official policy of what should happen in both of these scenarios that I can adopt?
``
<soapbox>These are exactly consequences of the philosophy of deriving API semantics from the simplest possible implementation, rather than the simplest possible specification, as defended in one of my past bugs``.
I don't like that negative start index is accepted if the amount to copy is zero, but at least collections using `IterableOnce.elemsToCopyToArray` are somewhat consistent in it. I champion 'always reject negative index' cause in large part because it guards the rest of the code from an underflow.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions