Skip to content

Commit d3cc45b

Browse files
committed
Fixed the HasEqualityOperators on generic value types.
1 parent 0af2ebb commit d3cc45b

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

src/ImmutableObjectGraph.Generation.Tests/TestSources/ImmutableWithComplexStructField.Tests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,15 @@ public void StructWithOperatorsRecyclesObjectWithSameValue()
6060
var v2 = v1.With(someStructFieldWithOperator: s12);
6161
Assert.Same(v1, v2);
6262
}
63+
64+
[Fact]
65+
public void GenericStructWithOperatorsRecyclesObjectWithSameValue()
66+
{
67+
var v = new object();
68+
var s12 = new SomeGenericStructWithOperator<object>(v);
69+
var v1 = ImmutableWithComplexStructField.Create(someGenericStructFieldWithOperator: s12);
70+
var v2 = v1.With(someGenericStructFieldWithOperator: s12);
71+
Assert.Same(v1, v2);
72+
}
6373
}
6474
}

src/ImmutableObjectGraph.Generation.Tests/TestSources/ImmutableWithComplexStructField.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ partial class ImmutableWithComplexStructField
88
readonly SomeStructWithMultipleFields someStructField;
99

1010
readonly SomeStructWithMultipleFieldsAndOperator someStructFieldWithOperator;
11+
12+
readonly SomeGenericStructWithOperator<object> someGenericStructFieldWithOperator;
1113
}
1214

1315
struct SomeStructWithMultipleFields
@@ -55,4 +57,34 @@ public override int GetHashCode()
5557
throw new NotImplementedException();
5658
}
5759
}
60+
61+
struct SomeGenericStructWithOperator<T> where T : class
62+
{
63+
internal SomeGenericStructWithOperator(T field1)
64+
{
65+
Field1 = field1;
66+
}
67+
68+
internal T Field1 { get; }
69+
70+
public static bool operator ==(SomeGenericStructWithOperator<T> one, SomeGenericStructWithOperator<T> two)
71+
{
72+
return one.Field1 == two.Field1;
73+
}
74+
75+
public static bool operator !=(SomeGenericStructWithOperator<T> one, SomeGenericStructWithOperator<T> two)
76+
{
77+
return !(one == two);
78+
}
79+
80+
public override bool Equals(object obj)
81+
{
82+
throw new NotImplementedException();
83+
}
84+
85+
public override int GetHashCode()
86+
{
87+
throw new NotImplementedException();
88+
}
89+
}
5890
}

src/ImmutableObjectGraph.Generation/CodeGen.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ private static bool HasEqualityOperators(ITypeSymbol symbol)
233233

234234
var equalityOperators = from method in symbol.GetMembers().OfType<IMethodSymbol>()
235235
where method.MethodKind == MethodKind.BuiltinOperator || method.MethodKind == MethodKind.UserDefinedOperator
236-
where method.Parameters.Length == 2 && method.Parameters.All(p => p.Type == symbol)
236+
where method.Parameters.Length == 2 && method.Parameters.All(p => p.Type.Equals(symbol))
237237
where method.Name == "op_Equality"
238238
select method;
239239
return equalityOperators.Any();

0 commit comments

Comments
 (0)