Incorrect loop bound in SizeOptIListSelectIterator.ToList
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 64/100
Research direction
Find SizeOptIListSelectIterator<TSource, TResult>.ToList() in the .NET runtime source and read the iterator implementation introduced in PR #118156. Reproduce the issue with <UseSizeOptimizedLinq>true</UseSizeOptimizedLinq> using the supplied reflection example. Done when the override returns the selected items and the result matches normal query enumeration; the issue suggests fixing or removing this non-public path.
Written by the indexing model from the issue text.
Description
Description
SizeOptIListSelectIterator<TSource, TResult>.ToList() checks the destination list’s Count immediately after constructing it with a capacity. Since Count is zero, the loop never executes and the override returns an empty list for a nonempty source.
Admittedly, this path is non-public and functionally dead, but it hides a ticking time bomb, so would be nice to fix for good hygiene (or remove altogether)
Reproduction Steps
With true
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
IEnumerable<int> query = new[] { 1, 2, 3 }.Select(x => x * 10);
MethodInfo method = query.GetType().GetMethod(
"ToList",
BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
?? throw new InvalidOperationException("ToList override not found");
List<int> result = (List<int>)(method.Invoke(query, null)
?? throw new InvalidOperationException("ToList returned null"));
Console.WriteLine(query.GetType().Name);
Console.WriteLine($"Reflection: [{string.Join(", ", result)}]");
Console.WriteLine($"Public API: [{string.Join(", ", query.ToList())}]");
Expected behavior
printing [10, 20, 30], matching enumeration of the query
Actual behavior
prints []
Regression?
The erroneous loop was included when this iterator was introduced in #118156
Known Workarounds
No workaround is needed
Configuration
Windows 10, x64
Reproduced on .NET 10, 11 rc3 and main
Other information
List<TResult> list = new List<TResult>(_source.Count);
for (int i = 0; i < list.Count; i++)
{
list.Add(_selector(_source[i]));
}
return list;
The constructor initializes Capacity, not Count
- Dominant language
- C#
- Stars
- 18.4k
- Forks
- 5.8k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 579
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/runtime
-
DbTransaction.SupportsSavepoints docs say Release/ReleaseAsync are expected to throw, but they are no-ops by designPossibly taken @alaricd claimed this 1 day ago. Openarea-System.Transactions untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
dotnet/runtime#137040 · 1 comment ·
Maintainers usually reply within 1 day
-
FileSystemGlobbing: Matcher throws ArgumentOutOfRangeException when the rootDir is the same value as the filePossibly taken @makazeu claimed this 1 day ago. Openarea-Extensions-FileSystem
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
dotnet/runtime#136989 · 1 comment ·
Maintainers usually reply within 1 day
-
Configuration binder source generator emits unqualified List<T> when binding arrays without implicit usingsPossibly taken @makazeu claimed this 1 day ago. Openarea-Extensions-Configuration bug source-generator
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
dotnet/runtime#136972 · 1 comment ·
Maintainers usually reply within 1 day
-
[wasi] .NET 10: WasmEnableSIMD with AOT fails because WasiApp.targets has -msimd128 commented outOpenarea-Build-mono untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
PropertyChangedEventArgs should have a nice DebuggerDisplayPossibly taken A pull request linked to this issue is open or already merged. Openarea-System.ComponentModel untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
dotnet/runtime#136608 · 2 comments ·
Maintainers usually reply within 1 day
Similar issues
-
needs-triage
Difficulty 1/5 1-3 hours Newbie friendliness 76/100
Azure/azure-sdk-for-net#64658 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
dotnet/aspnetcore#70370 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
NethermindEth/nethermind#15684 ·
Maintainers usually reply within 1 day
-
area/ai-assistant squad/flyt
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
Altinn/altinn-studio#21798 ·
Maintainers usually reply within 1 day
-
enhancement priority: low
Difficulty 1/5 Under an hour Newbie friendliness 90/100
Devolutions/UniGetUI#5701 · 1 reaction ·
Maintainers usually reply within 1 day