Skip to content

[REVISIT NEEDS SPEC] Respect bounds in indexOfSlice [ci: last-only] #10385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

som-snytt
Copy link
Contributor

@som-snytt som-snytt commented Apr 30, 2023

Fixes scala/bug#12780
Fixes scala/bug#12781

The non-trivial tweak is L51 of the test: from can't can be after the last elements, even to match an empty sequence.

The "generic" slow path, with unknown size, matches nothing [Ed empty] once the sequence is empty.

@scala-jenkins scala-jenkins added this to the 2.13.12 milestone Apr 30, 2023
@SethTisue SethTisue modified the milestones: 2.13.12, 2.13.11 Apr 30, 2023
@SethTisue SethTisue added the library:collections PRs involving changes to the standard collection library label Apr 30, 2023
@SethTisue SethTisue requested a review from a team April 30, 2023 14:58
@som-snytt
Copy link
Contributor Author

som-snytt commented May 1, 2023

TIL there is a scalacheck. ./test/scalacheck/scala/collection/IndexOfSliceTest.scala

sbt:root> scalacheck/testOnly scala.collection.IndexOfSliceTest

Edit: not SliceOfToast

@SethTisue SethTisue modified the milestones: 2.13.11, 2.13.12 May 1, 2023
@som-snytt som-snytt changed the title Respect bounds in indexOfSlice Respect bounds in indexOfSlice [ci: last-only] May 2, 2023
@som-snytt som-snytt force-pushed the issue/12780-index-of-slice branch from c9d2b46 to 109a7ee Compare May 2, 2023 03:09
@som-snytt
Copy link
Contributor Author

som-snytt commented May 2, 2023

As sjrd pointed out on the ticket, it's desirable for indexOfSlice to return an index for slice, not an index for an element via apply.

The fix is for the case where source seq and slice do not both have known lengths.

If the source seq has a known length, we can distinguish from == length and from == length + 1. Use drop to skip.

Otherwise, we count the prefix elements up to from.

@som-snytt som-snytt force-pushed the issue/12780-index-of-slice branch from d704e60 to d82b613 Compare May 2, 2023 17:02
@som-snytt som-snytt marked this pull request as ready for review May 2, 2023 18:54
@som-snytt som-snytt force-pushed the issue/12780-index-of-slice branch from 6365ca7 to fcb49ab Compare May 10, 2023 19:56
@som-snytt
Copy link
Contributor Author

I dozed briefly with my head on the keyboard and had a dream in which tpolecat commanded me to remove any usages of return where possible.

The added commit "returns them", as in returning product to Costco. Also rebased, as it incurs only one CI run.

I will leave the PR unsquashed for a short period for review purposes.

@som-snytt som-snytt force-pushed the issue/12780-index-of-slice branch from fcb49ab to fc4739f Compare May 27, 2023 18:15
@lrytz
Copy link
Member

lrytz commented Jun 2, 2023

I feel like we need to agree on, and spec, out-of-bounds handing before fixing individual issues. scala/bug#12795 (comment)

@som-snytt
Copy link
Contributor Author

The sjrd justification is that it returns a number >= the given start index.

scala> xs.indexOfSlice(Nil, -11)
val res10: Int = 0   // was -11

scala> xs.indexOfSlice(Nil, 11)
val res11: Int = -1

I suspect that only operations requiring an existing element ("the index of an existing element") should throw, paradigmatically, apply.

@SethTisue SethTisue modified the milestones: 2.13.12, 2.13.13 Aug 8, 2023
@SethTisue SethTisue marked this pull request as draft September 19, 2023 08:23
@lrytz lrytz modified the milestones: 2.13.13, 2.13.14 Nov 28, 2023
@som-snytt som-snytt force-pushed the issue/12780-index-of-slice branch from fc4739f to 721fc2f Compare February 16, 2024 20:20
@SethTisue SethTisue modified the milestones: 2.13.14, 2.13.15 Mar 13, 2024
@som-snytt som-snytt closed this Mar 20, 2024
@SethTisue SethTisue removed this from the 2.13.15 milestone Mar 21, 2024
@som-snytt som-snytt changed the title Respect bounds in indexOfSlice [ci: last-only] [REVISIT NEEDS SPEC] Respect bounds in indexOfSlice [ci: last-only] Jan 27, 2025
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.

Nil.startsWith(Nil, -1) && Nil.startsWith(Nil, 2) Seq(1).indexOfSlice(Nil, -2)==-2
4 participants