Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

MemoryExtensions.LastIndexOf ignores a custom comparer for bitwise-equatable element types

Đang mở Phù hợp với người mới
#136,281 2 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

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

Từ ngày 23/10/2026.

  • #136376 của @aw0lid — đ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
74/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 overload của MemoryExtensions.LastIndexOf<T> nhận hai giá trị ReadOnlySpan<T> và một IEqualityComparer<T> trong mã nguồn runtime. So sánh điều kiện đường dẫn nhanh theo bit của overload này với các overload comparer liên quan được đề cập trong issue, sau đó kiểm tra các bài kiểm thử hiện có cho overload này. Mục tiêu là comparer tùy chỉnh được cung cấp quyết định kết quả đối với các kiểu có thể so sánh theo bit, đồng thời hành vi của comparer mặc định vẫn được kiểm thử.

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

Mô tả

area-System.Runtime untriaged
Description

MemoryExtensions.LastIndexOf<T>(ReadOnlySpan<T>, ReadOnlySpan<T>, IEqualityComparer<T>?) appears to ignore a non-default comparer when T is known to be bitwise equatable.

The implementation takes the bitwise fast path whenever RuntimeHelpers.IsBitwiseEquatable<T>() returns true, without checking whether comparer is null or EqualityComparer<T>.Default.

As a result, a custom comparer is not invoked for types such as byte and char, and the returned index can differ from the result implied by the API contract.

The related comparer overloads generally restrict bitwise fast paths to the default comparer, for example:

comparer is null || comparer == EqualityComparer<T>.Default
Reproduction Steps
using System;
using System.Collections.Generic;

ReadOnlySpan<byte> source = [1, 2, 3];
ReadOnlySpan<byte> value = [9];

var comparer = new AllBytesEqualComparer();
var index = source.LastIndexOf(value, comparer);

Console.WriteLine(index);

sealed class AllBytesEqualComparer : IEqualityComparer<byte>
{
    public bool Equals(byte x, byte y) => true;

    public int GetHashCode(byte value) => 0;
}
Expected behavior

The custom comparer should be used.

Because the comparer considers every pair of bytes equal, the one-element sequence should match at the last valid position, and the result should be: 2

Actual behavior

The result is: -1

Regression?

No known regression

Known Workarounds

Avoid this overload for bitwise-equatable element types when using a custom comparer, and implement the sequence search manually.

Configuration

.NET SDK:
Version: 10.0.402
Commit: a8dd0577c5

Runtime Environment:
OS Name: Windows
OS Version: 10.0.19045
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\10.0.402\

Other information

The fast-path condition currently appears to be equivalent to:

if (RuntimeHelpers.IsBitwiseEquatable<T>())
{
    // Bitwise search.
}

This bypasses the supplied comparer.

A possible fix may be to restrict the fast path to the default comparer:

if (RuntimeHelpers.IsBitwiseEquatable<T>() && (comparer is null || comparer == EqualityComparer<T>.Default))
{
    // Bitwise search.
}
Ngôn ngữ chính
C#
Star
18.4k
Fork
5.7k
Merge trung bình
2 ngày 18 giờ
Pull request đã merge (30 ngày)
646

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.