dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.61k stars 4.56k forks source link

IStructuralEquatable.Equals on array throws for multidimensional arrays #66472

Open roji opened 2 years ago

roji commented 2 years ago

Description

Array implements IStructuralEquatable, but its Equals implementation throws if a multi-dimensional array is passed.

Reproduction Steps

var arr1 = new int[2,2];
var arr2 = new int[2,2];

Console.WriteLine(typeof(IStructuralEquatable).IsAssignableFrom(arr1.GetType())); // True
Console.WriteLine(StructuralComparisons.StructuralEqualityComparer.Equals(arr1, arr2)); // throws

Expected behavior

The method is expected to return true or false.

Actual behavior

Exception:

Unhandled exception. System.ArgumentException: Array was not a one-dimensional array.
   at System.Array.GetValue(Int32 index)
   at System.Array.System.Collections.IStructuralEquatable.Equals(Object other, IEqualityComparer comparer)
   at System.Collections.StructuralEqualityComparer.Equals(Object x, Object y)
   at Program.<Main>$(String[] args) in /home/roji/projects/test/Program.cs:line 20

Regression?

No response

Known Workarounds

One can avoid using IStructuralEquatable or StructuralComparisons.StructuralEqualityComparer specifically for multidimensional arrays.

Configuration

.NET 6, Ubuntu impish, x64.

Other information

No response

dotnet-issue-labeler[bot] commented 2 years ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ghost commented 2 years ago

Tagging subscribers to this area: @dotnet/area-system-runtime See info in area-owners.md if you want to be subscribed.

Issue Details
### Description Array implements IStructuralEquatable, but its Equals implementation throws if a multi-dimensional array is passed. ### Reproduction Steps ```c# var arr1 = new int[2,2]; var arr2 = new int[2,2]; Console.WriteLine(typeof(IStructuralEquatable).IsAssignableFrom(arr1.GetType())); // True Console.WriteLine(StructuralComparisons.StructuralEqualityComparer.Equals(arr1, arr2)); // throws ``` ### Expected behavior The method is expected to return true or false. ### Actual behavior Exception: ``` Unhandled exception. System.ArgumentException: Array was not a one-dimensional array. at System.Array.GetValue(Int32 index) at System.Array.System.Collections.IStructuralEquatable.Equals(Object other, IEqualityComparer comparer) at System.Collections.StructuralEqualityComparer.Equals(Object x, Object y) at Program.
$(String[] args) in /home/roji/projects/test/Program.cs:line 20 ``` ### Regression? _No response_ ### Known Workarounds One can avoid using IStructuralEquatable or StructuralComparisons.StructuralEqualityComparer specifically for multidimensional arrays. ### Configuration .NET 6, Ubuntu impish, x64. ### Other information _No response_
Author: roji
Assignees: -
Labels: `area-System.Runtime`, `untriaged`
Milestone: -
tannergooding commented 2 years ago

This impacts CC. @dotnet/area-system-collections since it flows through StructuralEqualityComparer

CC. @jkotas, @stephentoub on if there are any known limitations that might prevent this from being handled.