Fix IndexOf upper-bound guard in ImmutableArray/List#124967
Fix IndexOf upper-bound guard in ImmutableArray/List#124967prozolic wants to merge 2 commits intodotnet:mainfrom
Conversation
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.
|
Tagging subscribers to this area: @dotnet/area-system-collections |
There was a problem hiding this comment.
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/indexparameter from< Countto<= Countin IndexOf methods - Added upper-bound validation to
ImmutableList<T>.Node.IndexOfwhich 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 |
...raries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Node.cs
Show resolved
Hide resolved
| Requires.Range(count <= this.Count, nameof(count)); | ||
| if (count == 0 && index == 0) | ||
| { | ||
| return -1; |
There was a problem hiding this comment.
Why is this fast path needed?
There was a problem hiding this comment.
I added to match the behavior of ImmutableArray.IndexOf.
This fast path can be removed because -1 is returned even without it.
Change the
startIndexupper-bound validation inIndexOffrom '< Count' to '<= Count' forImmutableArray<T>,ImmutableArray<T>.Builder, and add upper-bound validation inImmutableList<T>.Node.This aligns the behavior with
Array.IndexOf, which permitsindex == Countprovided count is 0.Change:
startIndexupper-bound validation inIndexOffrom '< Count' to '<= Count' for ImmutableArray,ImmutableArray<T>.Builder.ImmutableList<T>.Nodeto match the behavior ofImmutableArray<T>.#124968