Skip to content
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

Implement equality comparisons on System.Array #1821

Merged
merged 2 commits into from
Dec 2, 2024

Conversation

BCSharp
Copy link
Member

@BCSharp BCSharp commented Nov 29, 2024

Fixes IndexOutOfRangeException when equating arrays with different index bases (lower bounds).

Example:

>>> from System import *
>>> a = Array.CreateInstance(Int32, (5,), (1,))
>>> b = Array.CreateInstance(Int32, (5,), (1,))
>>> a == b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: Index was outside the bounds of the array.
>>> a != b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: Index was outside the bounds of the array.

This is due to a bug in .NET:

var a = Array.CreateInstance(typeof(int), new[] { 5 }, new[] { 1 });
var b = Array.CreateInstance(typeof(int), new[] { 5 }, new[] { 1 });
((IStructuralEquatable)a).Equals(b, EqualityComparer<int>.Default);  // IndexOutOfRangeException

I'll see if this is already reported. It just shows that nonzero bases are not very popular.

In this PR I'll also cover equating multidimensional arrays, since there is just a little bit to add to address that as well.

@BCSharp
Copy link
Member Author

BCSharp commented Nov 30, 2024

Ordering comparisons are also not working well, but I leave it as that — I don't have old code to submit.

@BCSharp BCSharp marked this pull request as ready for review November 30, 2024 04:48
Copy link
Contributor

@slozier slozier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, seems fine to me. I'll file an issue regarding array ordering comparisons not working since this seems to be a regression from 2.7.

@slozier slozier merged commit c1345b4 into IronLanguages:main Dec 2, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants