Skip to content

Commit ae8148c

Browse files
committed
Fixed a unit test failure due to incorrect extension matching
1 parent 0986b26 commit ae8148c

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

Microsoft.Toolkit.HighPerformance/Enumerables/ReadOnlySpanTokenizer{T}.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,16 @@ public bool MoveNext()
7676
{
7777
this.start = newEnd;
7878

79-
int index = this.span.Slice(newEnd).IndexOf(this.separator);
79+
// We need to call this extension explicitly or the extension method resolution rules for the C# compiler
80+
// will end up picking Microsoft.Toolkit.HighPerformance.ReadOnlySpanExtensions.IndexOf instead, even
81+
// though the latter takes the parameter via a readonly reference. This is because the "in" modifier is
82+
// implicit, which makes the signature compatible, and because extension methods are matched in such a
83+
// way that methods "closest" to where they're used are preferred. Since this type shares the same root
84+
// namespace, this makes that extension a better match, so that it overrides the MemoryExtensions one.
85+
// This is not a problem for consumers of this package, as their code would be outside of the
86+
// Microsoft.Toolkit.HighPerformance namespace, so both extensions would be "equally distant", so that
87+
// when they're both in scope it will be possible to choose which one to use by adding an explicit "in".
88+
int index = System.MemoryExtensions.IndexOf(this.span.Slice(newEnd), this.separator);
8089

8190
// Extract the current subsequence
8291
if (index >= 0)

0 commit comments

Comments
 (0)