Incorrect loop bound in SizeOptIListSelectIterator.ToList

Đang mở Phù hợp với người mới
#137,126 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@equmagg đang làm issue này rồi.

Từ ngày 7/11/2026.

  • #137127 của @equmagg — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
64/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
csharp
Lĩnh vực
backend

Hướng nghiên cứu

Tìm SizeOptIListSelectIterator<TSource, TResult>.ToList() trong mã nguồn .NET runtime và đọc phần triển khai iterator được giới thiệu trong PR #118156. Tái hiện sự cố với <UseSizeOptimizedLinq>true</UseSizeOptimizedLinq> bằng ví dụ reflection được cung cấp. Công việc hoàn tất khi override trả về các mục đã chọn và kết quả khớp với việc liệt kê truy vấn thông thường; issue đề xuất sửa hoặc xóa đường dẫn không công khai này.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

area-System.Collections untriaged
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

Ngôn ngữ chính
C#
Star
18.4k
Fork
5.8k
Merge trung bình
2 ngày 12 giờ
Pull request đã merge (30 ngày)
579

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của dotnet/runtime

Tất cả issue của dotnet/runtime

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.