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

Record structs with InlineArray attribute compares only by the first value. #73504

Closed
Ilchert opened this issue May 16, 2024 · 6 comments · Fixed by #74072
Closed

Record structs with InlineArray attribute compares only by the first value. #73504

Ilchert opened this issue May 16, 2024 · 6 comments · Fixed by #74072

Comments

@Ilchert
Copy link

Ilchert commented May 16, 2024

Related to the dotnet/runtime/issues/96943.

Version Used:
dotnet SDK 8.0.204

Steps to Reproduce:

  1. Create record struct with InlineArray attribute
  2. Call equals.
  3. Record structs with InlineArray attribute compares only by the first value.
var a1 = new A();
a1[0]=1;
a1[1]=2;

var a2 = new A();
a2[0] = 1;
a2[1] = 1;


Console.WriteLine(a1.Equals(a2));

[InlineArray(2)]
record struct A
{
   public int P;
}

sharplab

Expected Behavior:
Should compare all values in inline array and print false in this case.

Actual Behavior:
Compares only the first element.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels May 16, 2024
@Ilchert Ilchert changed the title Incorrect comparation in InlineArray records Record structs with InlineArray attribute compares only by the first value. May 16, 2024
@jaredpar
Copy link
Member

This combination of features (record and InlineArray) is not particularly useful. @AlekseyTs did we consider making this combination an error at declaration? Can't see any good reasons to allow this.

@AlekseyTs
Copy link
Contributor

did we consider making this combination an error at declaration? Can't see any good reasons to allow this.

This is an oversight, there was no intent to support record-like behavior for inline arrays.

@jaredpar jaredpar added Bug Feature - Inline Arrays and removed untriaged Issues and PRs which have not yet been triaged by a lead labels May 20, 2024
@jaredpar jaredpar added this to the 17.12 milestone May 20, 2024
@lambdageek
Copy link
Member

Related runtime issue dotnet/runtime#96943 where we're considering changes to the default behavior of ValueType.Equals for inline arrays

@333fred
Copy link
Member

333fred commented Jun 19, 2024

Discussed in LDM on the 17th.

@AlekseyTs
Copy link
Contributor

From https://github.com/dotnet/csharplang/blob/main/meetings/2024/LDM-2024-06-17.md#conclusion-3:

Applying InlineArrayAttribute to a record struct will be an error.

@AaronRobinsonMSFT
Copy link
Member

The runtime is changing the behavior of the default Equals() and GetHashCode() to throw whenever a type is marked with InlineArrayAttribute.

See dotnet/runtime#103612.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants