Skip to content

Commit

Permalink
Define equals manually for Swift JitInterface InlineArray types. (dot…
Browse files Browse the repository at this point in the history
  • Loading branch information
jkoritzinsky authored Jul 18, 2024
1 parent 2b48fcd commit 71cc424
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/coreclr/tools/Common/JitInterface/CorInfoTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,7 @@ public unsafe struct CORINFO_TYPE_LAYOUT_NODE
public bool hasSignificantPadding { get => _hasSignificantPadding != 0; set => _hasSignificantPadding = value ? (byte)1 : (byte)0; }
}

public struct CORINFO_SWIFT_LOWERING
public struct CORINFO_SWIFT_LOWERING : IEquatable<CORINFO_SWIFT_LOWERING>
{
private byte _byReference;
public bool byReference { get => _byReference != 0; set => _byReference = value ? (byte)1 : (byte)0; }
Expand Down Expand Up @@ -1529,6 +1529,47 @@ private struct LoweredOffsets

public nint numLoweredElements;

public override bool Equals(object obj)
{
return obj is CORINFO_SWIFT_LOWERING other && Equals(other);
}

public bool Equals(CORINFO_SWIFT_LOWERING other)
{
if (byReference != other.byReference)
{
return false;
}

// If both are by-ref, the rest of the bits mean nothing.
if (byReference)
{
return true;
}

return LoweredElements.Slice(0, (int)numLoweredElements).SequenceEqual(other.LoweredElements.Slice(0, (int)other.numLoweredElements))
&& Offsets.Slice(0, (int)numLoweredElements).SequenceEqual(other.Offsets.Slice(0, (int)other.numLoweredElements));
}

public override int GetHashCode()
{
HashCode code = default;
code.Add(byReference);

if (byReference)
{
return code.ToHashCode();
}

for (int i = 0; i < numLoweredElements; i++)
{
code.Add(LoweredElements[i]);
code.Add(Offsets[i]);
}

return code.ToHashCode();
}

// Override for a better unit test experience
public override string ToString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public static IEnumerable<object[]> DiscoverSwiftTypes()
{
expected.Offsets[i] = (uint)naturalOffset.AlignUp(size);
}
else
{
expected.Offsets[i] = (uint)naturalOffset;
}
naturalOffset += size;
}
}
Expand Down

0 comments on commit 71cc424

Please sign in to comment.