Skip to content

Commit a8d6cbf

Browse files
authored
Standard Library Sync: September 24th, 2025 (#24063)
Superseed #24062 Steps to reproduce: ```shell git clone git@github.com:scala/scala.git --single-branch --no-tags --branch 2.13.x embed cd embed git filter-repo --path src/library --path-rename src/library:library/src git filter-repo --message-callback 'return re.sub(rb"(?<!\w)#(\d+)", rb"scala/scala#\1", message)' git remote add scala3 git@github.com:scala/scala3.git git fetch scala3 git replace --graft b594c22eac364ca2449a2700506b4f6b3f571b86 2308509 git filter-repo --proceed git checkout -b stdlib-history git checkout -b sync-24-09-2025 scala3/main git merge stdlib-history # Also fix the two conflicts that occurred git push scala3 ```
2 parents cbc3edb + 07afe85 commit a8d6cbf

File tree

12 files changed

+292
-171
lines changed

12 files changed

+292
-171
lines changed

library/src/scala/collection/Factory.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,10 @@ trait IterableFactory[+CC[_]] extends Serializable, caps.Pure {
149149
def newBuilder[A]: Builder[A, CC[A]]
150150

151151
/** Produces a $coll containing the results of some element computation a number of times.
152-
* @param n the number of elements contained in the $coll.
153-
* @param elem the element computation
154-
* @return A $coll that contains the results of `n` evaluations of `elem`.
155-
*/
152+
* @param n the number of elements contained in the $coll.
153+
* @param elem the element computation
154+
* @return A $coll that contains the results of `n` evaluations of `elem`.
155+
*/
156156
def fill[A](n: Int)(elem: => A): CC[A]^{elem} = from(new View.Fill(n)(elem))
157157

158158
/** Produces a two-dimensional $coll containing the results of some element computation a number of times.

library/src/scala/collection/IterableOnce.scala

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import scala.math.{Numeric, Ordering}
2424
import scala.reflect.ClassTag
2525
import scala.runtime.{AbstractFunction1, AbstractFunction2}
2626

27+
import IterableOnce.elemsToCopyToArray
28+
2729
/**
2830
* A template trait for collections which can be traversed either once only
2931
* or one or more times.
@@ -267,16 +269,25 @@ object IterableOnce {
267269
@inline implicit def iterableOnceExtensionMethods[A](it: IterableOnce[A]): IterableOnceExtensionMethods[A] =
268270
new IterableOnceExtensionMethods[A](it)
269271

270-
/** Computes the number of elements to copy to an array from a source IterableOnce
271-
*
272-
* @param srcLen the length of the source collection
273-
* @param destLen the length of the destination array
274-
* @param start the index in the destination array at which to start copying elements to
275-
* @param len the requested number of elements to copy (we may only be able to copy less than this)
276-
* @return the number of elements that will be copied to the destination array
277-
*/
278-
@inline private[collection] def elemsToCopyToArray(srcLen: Int, destLen: Int, start: Int, len: Int): Int =
279-
math.max(math.min(math.min(len, srcLen), destLen - start), 0)
272+
/** Computes the number of elements to copy to an array from a source IterableOnce.
273+
*
274+
* If `start` is less than zero, it is taken as zero.
275+
* If any of the length inputs is less than zero, the computed result is zero.
276+
*
277+
* The result is the smaller of the remaining capacity in the destination and the requested count.
278+
*
279+
* @param srcLen the length of the source collection
280+
* @param destLen the length of the destination array
281+
* @param start the index in the destination array at which to start copying elements
282+
* @param len the requested number of elements to copy (we may only be able to copy less than this)
283+
* @return the number of elements that will be copied to the destination array
284+
*/
285+
@inline private[collection] def elemsToCopyToArray(srcLen: Int, destLen: Int, start: Int, len: Int): Int = {
286+
val limit = math.min(len, srcLen)
287+
val capacity = if (start < 0) destLen else destLen - start
288+
val total = math.min(capacity, limit)
289+
math.max(0, total)
290+
}
280291

281292
/** Calls `copyToArray` on the given collection, regardless of whether or not it is an `Iterable`. */
282293
@inline private[collection] def copyElemsToArray[A, B >: A](elems: IterableOnce[A]^,
@@ -438,6 +449,8 @@ transparent trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOn
438449
* @return a $coll containing the elements greater than or equal to
439450
* index `from` extending up to (but not including) index `until`
440451
* of this $coll.
452+
* @example
453+
* `List('a', 'b', 'c', 'd', 'e').slice(1, 3) == List('b', 'c')`
441454
*/
442455
def slice(from: Int, until: Int): C^{this}
443456

@@ -987,58 +1000,70 @@ transparent trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOn
9871000

9881001
/** Copies elements to an array, returning the number of elements written.
9891002
*
990-
* Fills the given array `xs` starting at index `start` with values of this $coll.
1003+
* Fills the given array `dest` starting at index `start` with values of this $coll.
9911004
*
9921005
* Copying will stop once either all the elements of this $coll have been copied,
9931006
* or the end of the array is reached.
9941007
*
995-
* @param xs the array to fill.
1008+
* @param dest the array to fill.
9961009
* @tparam B the type of the elements of the array.
9971010
* @return the number of elements written to the array
9981011
*
9991012
* @note Reuse: $consumesIterator
10001013
*/
10011014
@deprecatedOverriding("This should always forward to the 3-arg version of this method", since = "2.13.4")
1002-
def copyToArray[B >: A](xs: Array[B]): Int = copyToArray(xs, 0, Int.MaxValue)
1015+
def copyToArray[B >: A](@deprecatedName("xs", since="2.13.17") dest: Array[B]): Int =
1016+
copyToArray(dest, start = 0, n = Int.MaxValue)
10031017

10041018
/** Copies elements to an array, returning the number of elements written.
10051019
*
1006-
* Fills the given array `xs` starting at index `start` with values of this $coll.
1020+
* Fills the given array `dest` starting at index `start` with values of this $coll.
10071021
*
10081022
* Copying will stop once either all the elements of this $coll have been copied,
10091023
* or the end of the array is reached.
10101024
*
1011-
* @param xs the array to fill.
1025+
* @param dest the array to fill.
10121026
* @param start the starting index of xs.
10131027
* @tparam B the type of the elements of the array.
10141028
* @return the number of elements written to the array
10151029
*
10161030
* @note Reuse: $consumesIterator
10171031
*/
10181032
@deprecatedOverriding("This should always forward to the 3-arg version of this method", since = "2.13.4")
1019-
def copyToArray[B >: A](xs: Array[B], start: Int): Int = copyToArray(xs, start, Int.MaxValue)
1033+
def copyToArray[B >: A](@deprecatedName("xs", since="2.13.17") dest: Array[B], start: Int): Int =
1034+
copyToArray(dest, start = start, n = Int.MaxValue)
10201035

1021-
/** Copy elements to an array, returning the number of elements written.
1036+
/** Copies elements to an array and returns the number of elements written.
10221037
*
1023-
* Fills the given array `xs` starting at index `start` with at most `len` elements of this $coll.
1038+
* Fills the given array `dest` starting at index `start` with at most `n` elements of this $coll.
10241039
*
10251040
* Copying will stop once either all the elements of this $coll have been copied,
1026-
* or the end of the array is reached, or `len` elements have been copied.
1041+
* or the end of the array is reached, or `n` elements have been copied.
10271042
*
1028-
* @param xs the array to fill.
1043+
* If `start` is less than zero, it is taken as zero.
1044+
*
1045+
* @param dest the array to fill.
10291046
* @param start the starting index of xs.
1030-
* @param len the maximal number of elements to copy.
1047+
* @param n the maximal number of elements to copy.
10311048
* @tparam B the type of the elements of the array.
10321049
* @return the number of elements written to the array
10331050
*
10341051
* @note Reuse: $consumesIterator
10351052
*/
1036-
def copyToArray[B >: A](xs: Array[B], start: Int, len: Int): Int = {
1053+
def copyToArray[B >: A](
1054+
@deprecatedName("xs", since="2.13.17") dest: Array[B],
1055+
start: Int,
1056+
@deprecatedName("len", since="2.13.17") n: Int
1057+
): Int = {
10371058
val it = iterator
10381059
var i = start
1039-
val end = start + math.min(len, xs.length - start)
1060+
val srclen = knownSize match {
1061+
case -1 => dest.length
1062+
case k => k
1063+
}
1064+
val end = start + elemsToCopyToArray(srclen, dest.length, start, n)
10401065
while (i < end && it.hasNext) {
1041-
xs(i) = it.next()
1066+
dest(i) = it.next()
10421067
i += 1
10431068
}
10441069
i - start
@@ -1244,7 +1269,7 @@ transparent trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOn
12441269
* @param pf the partial function
12451270
* @return an option value containing pf applied to the first
12461271
* value for which it is defined, or `None` if none exists.
1247-
* @example `Seq("a", 1, 5L).collectFirst({ case x: Int => x*10 }) = Some(10)`
1272+
* @example `Seq("a", 1, 5L).collectFirst { case x: Int => x*10 } = Some(10)`
12481273
*/
12491274
def collectFirst[B](pf: PartialFunction[A, B]^): Option[B] = {
12501275
// Presumably the fastest way to get in and out of a partial function is for a sentinel function to return itself

library/src/scala/collection/immutable/ArraySeq.scala

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,14 @@ object ArraySeq extends StrictOptimizedClassTagSeqFactory[ArraySeq] { self =>
579579
def apply(i: Int): Float = unsafeArray(i)
580580
override def hashCode = MurmurHash3.arraySeqHash(unsafeArray)
581581
override def equals(that: Any) = that match {
582-
case that: ofFloat => Arrays.equals(unsafeArray, that.unsafeArray)
582+
case that: ofFloat =>
583+
val array = unsafeArray
584+
val thatArray = that.unsafeArray
585+
(array eq thatArray) || array.length == thatArray.length && {
586+
var i = 0
587+
while (i < array.length && array(i) == thatArray(i)) i += 1
588+
i >= array.length
589+
}
583590
case _ => super.equals(that)
584591
}
585592
override def iterator: Iterator[Float] = new ArrayOps.ArrayIterator[Float](unsafeArray)
@@ -614,7 +621,14 @@ object ArraySeq extends StrictOptimizedClassTagSeqFactory[ArraySeq] { self =>
614621
def apply(i: Int): Double = unsafeArray(i)
615622
override def hashCode = MurmurHash3.arraySeqHash(unsafeArray)
616623
override def equals(that: Any) = that match {
617-
case that: ofDouble => Arrays.equals(unsafeArray, that.unsafeArray)
624+
case that: ofDouble =>
625+
val array = unsafeArray
626+
val thatArray = that.unsafeArray
627+
(array eq thatArray) || array.length == thatArray.length && {
628+
var i = 0
629+
while (i < array.length && array(i) == thatArray(i)) i += 1
630+
i >= array.length
631+
}
618632
case _ => super.equals(that)
619633
}
620634
override def iterator: Iterator[Double] = new ArrayOps.ArrayIterator[Double](unsafeArray)

library/src/scala/collection/immutable/HashMap.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,8 @@ private final class BitmapIndexedMapNode[K, +V](
13481348
override def hashCode(): Int =
13491349
throw new UnsupportedOperationException("Trie nodes do not support hashing.")
13501350

1351+
override def toString = s"${getClass.getName}@${Integer.toHexString(System.identityHashCode(this))}"
1352+
13511353
override def concat[V1 >: V](that: MapNode[K, V1], shift: Int): BitmapIndexedMapNode[K, V1] = that match {
13521354
case bm: BitmapIndexedMapNode[K, V] @unchecked =>
13531355
if (size == 0) return bm
@@ -2093,6 +2095,8 @@ private final class HashCollisionMapNode[K, +V ](
20932095
override def hashCode(): Int =
20942096
throw new UnsupportedOperationException("Trie nodes do not support hashing.")
20952097

2098+
override def toString = s"${getClass.getName}@${Integer.toHexString(System.identityHashCode(this))}"
2099+
20962100
override def cachedJavaKeySetHashCode: Int = size * hash
20972101

20982102
}

library/src/scala/collection/immutable/List.scala

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,6 @@ sealed abstract class List[+A]
190190
h
191191
}
192192

193-
/** @inheritdoc
194-
*
195-
* @example {{{
196-
* // Given a list
197-
* val letters = List('a','b','c','d','e')
198-
*
199-
* // `slice` returns all elements beginning at index `from` and afterwards,
200-
* // up until index `until` (excluding index `until`.)
201-
* letters.slice(1,3) // Returns List('b','c')
202-
* }}}
203-
*/
204193
override def slice(from: Int, until: Int): List[A] = {
205194
val lo = scala.math.max(from, 0)
206195
if (until <= lo || isEmpty) Nil

0 commit comments

Comments
 (0)