-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Fix IList.Contains to work with multidimensional arrays #119583
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
It currently fails because it uses IndexOf, but there's no fundamental reason this shouldn't work with multidimensional arrays, and it breaks expectations that it doesn't work. We can just fall back to enumerating if the Rank isn't 1.
There 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
This PR fixes the IList.Contains method to work with multidimensional arrays. Previously, Contains always failed for multidimensional arrays because it relied on IndexOf, which only supports single-dimensional arrays. The fix implements a fallback enumeration approach for multidimensional arrays while maintaining the efficient IndexOf-based implementation for single-dimensional arrays.
Key changes:
- Modified Array.IList.Contains to check array rank and use enumeration for multidimensional arrays
- Added comprehensive test coverage for both value and reference type multidimensional arrays
- Added LINQ integration test to verify the fix works with existing LINQ operations
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/Array.cs | Core fix implementing rank check and enumeration fallback for multidimensional arrays |
| src/libraries/System.Runtime/tests/System.Runtime.Tests/System/ArrayTests.cs | Enhanced test coverage for multidimensional arrays and IList.Contains behavior |
| src/libraries/System.Linq/tests/OfTypeTests.cs | Integration test verifying LINQ operations work with multidimensional arrays |
There 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.
Thanks
|
Tagging subscribers to this area: @dotnet/area-system-runtime |
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/ArrayTests.cs
Outdated
Show resolved
Hide resolved
| bool IList.Contains(object? value) | ||
| { | ||
| return IndexOf(this, value) >= this.GetLowerBound(0); | ||
| // IndexOf only works for single-dimensional arrays. |
There 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.
Should this be fixed in IndexOf instead?
There 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.
What would the output of IndexOf be in the context of MD arrays?
There 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.
I considered that, but wasn't sure what semantics we would want for IndexOf in terms of the actual index it computes.
My intent is to backport this change to 10. If we're ok at this point defining what those semantics would be, I could do that instead.
There 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.
I suppose the type does implement IList so an MD array index is technically speaking well defined.
There 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.
I suppose the type does implement
IListso an MD array index is technically speaking well defined.
You mean because of the indexer? That throws for MD arrays, too.
There 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.
My thinking was that for 10 we fix Contains in order to fix the LINQ issue, and then for 11 if desired we can look at declaring the ordering and making the other members work... doing so will unblock some other optimizations we previously tried in LINQ but had to back out because the incoming IList wasn't actually usable, and just in general remove unexpected surprises.
There 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.
You mean because of the indexer? That throws for MD arrays, too
Ah, ok. It is weird that IList.Count does not throw, but the indexer throws.
Agreed :)
There 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.
That won't work for all MD arrays as they can have more than Array.MaxLength items and therefore have values outside the bounds of int.MaxValue
There 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.
That won't work for all MD arrays
What is the "that" in this statement? IndexOf? That's a good point, too, in addition to ordering, you might have indices too large to return.
There 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.
Any of the IList APIs that deal with indexes risk not working with MD arrays, so while we could update them to work in some cases (and likely almost any real world case), it is important to note (at least in the impl, potentially in the documentation) that some MD arrays are "incompatible" with those APIs and so we need to have the fallback paths regardless.
Why do we need to fix this issue in 10? Was there some change in Linq that exposed this bug? |
|
Ok, I read the issue - this was exposed by #119569 (comment) . Have we looked around for other IList implementations with the same problem? The safe fix for .NET 10 would be to revert the optimization. |
Yes, OfType().Contains() uses IList.Contains now. I'd rather fix Array's broken implementation than revert the optimization. |
It's rarely used in LINQ, which is mostly based on |
|
runtime/src/libraries/System.Linq/src/System/Linq/OfType.SpeedOpt.cs Lines 178 to 188 in 3823740
This optimization seems problematic. The code below correctly outputs using System;
using System.Collections.Generic;
using System.Linq;
List<D> source = new List<D> { new D(1) };
D d2 = new(2);
Console.WriteLine(source.OfType<S>().Contains(d2));
class S : IEquatable<S>
{
public bool Equals(S? other) => other is not null;
public override bool Equals(object? obj) => obj is S;
public override int GetHashCode() => 0;
}
class D : S, IEquatable<D>
{
public D(int x) => X = x;
public int X { get; }
public bool Equals(D? other) => other is not null && X == other.X;
public override bool Equals(object? obj) => obj is D d && Equals(d);
public override int GetHashCode() => X;
} |
|
That example looks to violate the implementation requirements of
|
| // IndexOf only works for single-dimensional arrays. | ||
| if (Rank == 1) | ||
| { | ||
| return IndexOf(this, value) >= GetLowerBound(0); |
There 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.
This has integer overflow bug.
using System.Collections;
IList a = Array.CreateInstance(typeof(int), [5], [Int32.MinValue]);
Console.WriteLine(a.Contains(42));
prints true, but it should print false.
There 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.
Looks like we return GetLowerBound(0) - 1 in the case that it isn't found, so this should likely be the following, which indicates it was found regardless of length (provided it is Rank == 1)
| return IndexOf(this, value) >= GetLowerBound(0); | |
| return IndexOf(this, value) != (GetLowerBound(0) - 1); |
There 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.
Shall I fix that in this PR as well? I didn't introduce this line, I just wrapped it in the if.
There 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.
I think so. It is part of end-to-end of making IList.Contains work correctly for non-SZ arrays.
Violate what? |
The issue here is in: Console.WriteLine(((S)source[0]).Equals(d2));
vs
Console.WriteLine(source[0].Equals(d2));You've defined two different For |
|
@tannergooding you are right. What about this example: using System;
using System.Collections.Generic;
using System.Linq;
List<S> source = new List<S> { new S() };
Console.WriteLine(source.OfType<D>().Contains(new D(1)));
class S
{
public bool Equals(S? other) => other is not null;
public override bool Equals(object? obj) => obj is S;
public override int GetHashCode() => 0;
}
class D : S
{
public D(int x) => X = x;
public int X { get; }
public bool Equals(D? other) => other is not null && X == other.X;
public override bool Equals(object? obj) => obj is D d && Equals(d);
public override int GetHashCode() => X;
} |
|
That correctly reports
When implementing either You can write all kinds of incorrect code and invalid implementations that violate this, but the bug for that is on the type author not on LINQ or the built-in collection types. |
The output is |
I see. That looks like a bug in CC. @stephentoub |
|
Replaced by #119600 |
That example is also problematic. x.Equals(y) != y.Equals(x) |
It currently fails because it uses IndexOf, but there's no fundamental reason this shouldn't work with multidimensional arrays, and it breaks expectations that it doesn't work. We can just fall back to enumerating if the Rank isn't 1.
Fixes #119569
We need to fix the issue in 10, and fixing Array itself seems like the right answer. If there are objections to backporting this fix, please let me know. This is taking something that previously always threw an exception and now makes it work. (There are probably a couple more array interface implementations we could fix subsequently to work with multidimensional arrays, as well, e.g. CopyTo, a problem we've bumped into from LINQ as well in the past.)