Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/coreclr/vm/comutilnative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1732,6 +1732,7 @@ static ValueTypeHashCodeStrategy GetHashCodeStrategy(MethodTable* mt, QCall::Obj
// if we get an object reference we get the hash code out of that
Comment thread
huoyaoyuan marked this conversation as resolved.
if (*(Object**)((BYTE *)objHandle.Get()->UnBox() + *fieldOffset + field->GetOffsetUnsafe()) != NULL)
{
*fieldOffset += field->GetOffsetUnsafe();
ret = ValueTypeHashCodeStrategy::ReferenceField;
Comment thread
huoyaoyuan marked this conversation as resolved.
}
else
Expand All @@ -1745,17 +1746,17 @@ static ValueTypeHashCodeStrategy GetHashCodeStrategy(MethodTable* mt, QCall::Obj
CorElementType fieldType = field->GetFieldType();
if (fieldType == ELEMENT_TYPE_R8)
{
*fieldOffset = field->GetOffsetUnsafe();
*fieldOffset += field->GetOffsetUnsafe();
ret = ValueTypeHashCodeStrategy::DoubleField;
}
else if (fieldType == ELEMENT_TYPE_R4)
{
*fieldOffset = field->GetOffsetUnsafe();
*fieldOffset += field->GetOffsetUnsafe();
ret = ValueTypeHashCodeStrategy::SingleField;
}
else if (fieldType != ELEMENT_TYPE_VALUETYPE)
{
*fieldOffset = field->GetOffsetUnsafe();
*fieldOffset += field->GetOffsetUnsafe();
*fieldSize = field->LoadSize();
ret = ValueTypeHashCodeStrategy::FastGetHashCode;
}
Expand All @@ -1767,7 +1768,7 @@ static ValueTypeHashCodeStrategy GetHashCodeStrategy(MethodTable* mt, QCall::Obj
MethodTable* fieldMT = fieldTH.GetMethodTable();
if (CanCompareBitsOrUseFastGetHashCode(fieldMT))
{
*fieldOffset = field->GetOffsetUnsafe();
*fieldOffset += field->GetOffsetUnsafe();
*fieldSize = field->LoadSize();
ret = ValueTypeHashCodeStrategy::FastGetHashCode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,21 @@ public static void StructContainsPointerCompareTest()
Assert.True(obj1.Equals(obj2));
Assert.Equal(obj1.GetHashCode(), obj2.GetHashCode());
}

[Fact]
public static void StructContainsPointerNestedCompareTest()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Because these two values are not the same the fact they aren't equal makes sense. We should also have a test where they are equal to validate the other direction.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Well all the other tests are only testing the equal case. Returning different hash code is just an implementation detail. The test should be reversed and only test equal case instead.

{
StructContainsPointerNested obj1 = new StructContainsPointerNested();
obj1.o = null;
obj1.value.value = 1;

StructContainsPointerNested obj2 = new StructContainsPointerNested();
obj1.o = null;
obj1.value.value = 2;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
obj1.o = null;
obj1.value.value = 2;
obj2.o = null;
obj2.value.value = 2;

I assume?


Assert.False(obj1.Equals(obj2));
Assert.NotEqual(obj1.GetHashCode(), obj2.GetHashCode());
}

public struct S
{
Expand Down Expand Up @@ -392,5 +407,11 @@ public struct StructContainsPointer
public double value1;
public double value2;
}

public struct StructContainsPointerNested
{
public object o;
public StructNonOverriddenEqualsOrGetHasCode value;
}
}
}