Skip to content

Conversation

sjrd
Copy link
Member

@sjrd sjrd commented Mar 26, 2020

This is necessary for Scala.js, where ArrayIndexOutOfBoundsExceptions are Undefined Behavior, and cannot be reliably caught.

Those methods throw a NoSuchElementException for an empty vector, and those exceptions have never been considered UB in Scala.js before. It is therefore a breach of contract for Vector.{head,last} to become UB on empty vectors.

There are many other places where we catch AIOOBEs, but in those other places we throw an IndexOutOfBoundsException instead. While it is not quite correct to turn them into UBs either, at least there is precedent for OOBEs to be considered UB in Scala.js. So this commit does not change them, being afraid of the consequences to JVM performance.

It is still bad, though (index out of bounds on Scala collections were never UB before), but at least it is a bit less bad than before.


This combines with #8828 to fix scala/bug#11920.

This is necessary for Scala.js, where
`ArrayIndexOutOfBoundsException`s are Undefined Behavior, and
cannot be reliably caught.

Those methods throw a `NoSuchElementException` for an empty vector,
and those exceptions have never been considered UB in Scala.js
before. It is therefore a breach of contract for
`Vector.{head,last}` to become UB on empty vectors.

There are many other places where we catch AIOOBEs, but in those
other places we throw an `IndexOutOfBoundsException` instead.
While it is not quite correct to turn them into UBs either, at
*least* there is precedent for OOBEs to be considered UB in
Scala.js. So this commit does not change them, being afraid of the
consequences to JVM performance.

It *is* still bad, though (index out of bounds on Scala collections
were never UB before), but at least it is a bit less bad than
before.
@sjrd sjrd force-pushed the fix-vector-head-last-scalajs branch from 3fe31ad to c04b78c Compare March 26, 2020 13:39
@SethTisue SethTisue added the library:collections PRs involving changes to the standard collection library label Mar 26, 2020
Copy link
Contributor

@szeiger szeiger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't run new benchmarks yet but this is basically the same as the changes in apply. It will cost us a few percent of performance to avoid accessing invalid array indices.

@szeiger szeiger merged commit cbac7d2 into scala:2.13.x Mar 31, 2020
@sjrd sjrd deleted the fix-vector-head-last-scalajs branch March 31, 2020 12:58
@sjrd
Copy link
Member Author

sjrd commented Mar 31, 2020

Thanks!

@SethTisue SethTisue changed the title Do not rely on ArrayIndexOOBE in Vector.{head,last}. Do not rely on ArrayIndexOOBE in new-Vector Vector.{head,last}. Mar 31, 2020
hamzaremmal pushed a commit to hamzaremmal/scala3 that referenced this pull request May 2, 2025
…alajs

Do not rely on `ArrayIndexOOBE` in `Vector.{head,last}`.
hamzaremmal pushed a commit to scala/scala3 that referenced this pull request May 7, 2025
…alajs

Do not rely on `ArrayIndexOOBE` in `Vector.{head,last}`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
library:collections PRs involving changes to the standard collection library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The new Vector relies on catch AIOOBEs, which are UB in Scala.js
5 participants