-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Fix DefaultIfEmptyIterator.TryGetElementAt returning default value for out-of-bounds indices #119838
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
Conversation
…ment not found Co-authored-by: eiriktsarpalis <[email protected]>
…tElementAt Co-authored-by: eiriktsarpalis <[email protected]>
DefaultIfEmptyIterator<TSource>.TryGetElementAt returns _default when not foundThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Fixes a regression in .NET 9 where ElementAtOrDefault on DefaultIfEmpty iterators incorrectly returned the custom default value instead of default(TSource) for out-of-bounds indices.
- Modified
TryGetElementAtto return the custom default only for index 0, anddefault(TSource)for out-of-bounds indices - Added comprehensive regression tests covering both value and reference types with various out-of-bounds scenarios
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.Linq/src/System/Linq/DefaultIfEmpty.SpeedOpt.cs | Fixed TryGetElementAt logic to return correct defaults for out-of-bounds access |
| src/libraries/System.Linq/tests/DefaultIfEmptyTests.cs | Added regression test verifying ElementAtOrDefault behavior for out-of-bounds indices |
|
/backport to release/10.0-staging |
|
Started backporting to release/10.0-staging: https://github.com/dotnet/runtime/actions/runs/17829880616 |
|
/backport to release/9.0-staging |
|
Started backporting to release/9.0-staging: https://github.com/dotnet/runtime/actions/runs/17829888883 |
|
@eiriktsarpalis an error occurred while backporting to "release/10.0-staging", please check the run log for details! Error: The specified backport target branch "release/10.0-staging" wasn't found in the repo. |
|
/backport to release/10.0 |
|
Started backporting to release/10.0: https://github.com/dotnet/runtime/actions/runs/17830033506 |
…r out-of-bounds indices (dotnet#119838) * Initial plan * Fix DefaultIfEmptyIterator.TryGetElementAt to return default when element not found Co-authored-by: eiriktsarpalis <[email protected]> * Add regression test and finalize fix for DefaultIfEmptyIterator.TryGetElementAt Co-authored-by: eiriktsarpalis <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: eiriktsarpalis <[email protected]>
|
/backport to release/10.0-rc2 |
|
Started backporting to release/10.0-rc2: https://github.com/dotnet/runtime/actions/runs/17952465675 |
Fixes a regression introduced in .NET 9 where
ElementAtOrDefaultonDefaultIfEmptyiterators was incorrectly returning the custom default value instead ofdefault(TSource)for out-of-bounds indices.Problem:
The issue was in
DefaultIfEmptyIterator<TSource>.TryGetElementAtwhich always returned_default(the custom default value) regardless of whether the element was found, instead of returningdefault(TSource)whenfoundis false.Root Cause:
The method was setting
found = trueonly for index 0 but always returning_default, even for out-of-bounds indices where it should returndefault(TSource).Fix:
TryGetElementAtto return_defaultonly whenindex == 0andfound = truedefault(TSource)for all other out-of-bounds indices wherefoundremains falseBehavior After Fix:
Array.Empty<int>().DefaultIfEmpty(999).ElementAtOrDefault(0)→ Returns999(unchanged)Array.Empty<int>().DefaultIfEmpty(999).ElementAtOrDefault(1)→ Returns0(was999)Array.Empty<string>().DefaultIfEmpty("default").ElementAtOrDefault(1)→ Returnsnull(was"default")The fix ensures
ElementAtOrDefaultbehaves consistently with the documented contract of returningdefault(TSource)for out-of-bounds access.Fixes #119834.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.