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

Define equals manually for Swift JitInterface InlineArray types. #104843

Merged
merged 8 commits into from
Jul 18, 2024
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
Loading