Skip to content

Fix IndexOf upper-bound guard in ImmutableArray/List#124967

Open
prozolic wants to merge 2 commits intodotnet:mainfrom
prozolic:indexof
Open

Fix IndexOf upper-bound guard in ImmutableArray/List#124967
prozolic wants to merge 2 commits intodotnet:mainfrom
prozolic:indexof

Conversation

@prozolic
Copy link
Contributor

@prozolic prozolic commented Feb 27, 2026

Change the startIndex upper-bound validation in IndexOf from '< Count' to '<= Count' for ImmutableArray<T>,
ImmutableArray<T>.Builder, and add upper-bound validation in ImmutableList<T>.Node.
This aligns the behavior with Array.IndexOf, which permits index == Count provided count is 0.

Change:

  • Change the startIndex upper-bound validation in IndexOf from '< Count' to '<= Count' for ImmutableArray,
    ImmutableArray<T>.Builder.
  • Add upper-bound validation in ImmutableList<T>.Node to match the behavior of ImmutableArray<T>.
  • Add test case and fix test case to also validate the parameter name as the fix in Use Requires.Range in LastIndexOf #124560.

#124968

Change the startIndex upper-bound validation in IndexOf from
'< Count' to '<= Count' for ImmutableArray<T>,
ImmutableArray<T>.Builder, and add upper-bound validation
in ImmutableList<T>.Node.
This aligns the behavior with Array.IndexOf, which permits
index == Count provided count is 0.
Copilot AI review requested due to automatic review settings February 27, 2026 13:58
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Feb 27, 2026
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-collections
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes the IndexOf upper-bound validation in ImmutableArray<T>, ImmutableArray<T>.Builder, and ImmutableList<T>.Node to align with Array.IndexOf behavior, which allows startIndex == Count when count == 0. The PR is a follow-up to #124560 which fixed similar issues in LastIndexOf.

Changes:

  • Modified upper-bound validation for startIndex/index parameter from < Count to <= Count in IndexOf methods
  • Added upper-bound validation to ImmutableList<T>.Node.IndexOf which was previously missing
  • Enhanced tests to validate parameter names in thrown exceptions and added consistency tests with Array.IndexOf

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
ImmutableArray_1.cs Changed startIndex upper-bound validation from < Length to <= Length in IndexOf
ImmutableArray_1.Builder.cs Changed startIndex upper-bound validation from < Count to <= Count in IndexOf
ImmutableList_1.Node.cs Added upper-bound check to index validation (from single >= 0 check to >= 0 && <= Count), added redundant count upper-bound validation
IndexOfTests.cs Added indexParameterName parameter to test signature, enhanced tests to validate exception parameter names and added edge case tests
ImmutableArrayTest.cs Added IndexOfConsistentWithArray test and updated test calls to pass "startIndex" parameter
ImmutableArrayBuilderTest.cs Added IndexOfConsistentWithArray test and updated test calls to pass "startIndex" parameter
ImmutableListTest.cs Added IndexOfConsistentWithArray test and updated test calls to pass "index" parameter
ImmutableListBuilderTest.cs Updated test calls to pass "index" parameter

Requires.Range(count <= this.Count, nameof(count));
if (count == 0 && index == 0)
{
return -1;
Copy link
Member

Choose a reason for hiding this comment

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

Why is this fast path needed?

Copy link
Contributor Author

@prozolic prozolic Feb 27, 2026

Choose a reason for hiding this comment

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

I added to match the behavior of ImmutableArray.IndexOf.
This fast path can be removed because -1 is returned even without it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Collections community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants