-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Use new record equality specification #43626
Changes from all commits
30779f8
2912e87
f1e0f4d
71f948b
8c50aa1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,15 +2,13 @@ | |
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
#nullable enable | ||
|
||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis.CSharp.Emit; | ||
using Microsoft.CodeAnalysis.CSharp.Symbols; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.PooledObjects; | ||
using Microsoft.CodeAnalysis.RuntimeMembers; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Roslyn.Utilities; | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp.Symbols | ||
{ | ||
|
@@ -253,6 +251,61 @@ internal override void GenerateMethodBody(TypeCompilationState compilationState, | |
/// </summary> | ||
internal static partial class MethodBodySynthesizer | ||
{ | ||
/// <summary> | ||
/// Given a set of fields, produce an expression that is true when all of the given fields on | ||
/// `this` are equal to the fields on <paramref name="otherReceiver" /> according to the | ||
/// default EqualityComparer. | ||
/// </summary> | ||
public static BoundExpression GenerateFieldEquals<TList>( | ||
BoundExpression? initialExpression, | ||
BoundExpression otherReceiver, | ||
TList fields, | ||
SyntheticBoundNodeFactory F) where TList : IReadOnlyList<FieldSymbol> | ||
{ | ||
Debug.Assert(fields.Count > 0); | ||
|
||
// Expression: | ||
// | ||
// System.Collections.Generic.EqualityComparer<T_1>.Default.Equals(this.backingFld_1, value.backingFld_1) | ||
// ... | ||
// && System.Collections.Generic.EqualityComparer<T_N>.Default.Equals(this.backingFld_N, value.backingFld_N) | ||
|
||
// prepare symbols | ||
var equalityComparer_get_Default = F.WellKnownMethod( | ||
WellKnownMember.System_Collections_Generic_EqualityComparer_T__get_Default); | ||
var equalityComparer_Equals = F.WellKnownMethod( | ||
WellKnownMember.System_Collections_Generic_EqualityComparer_T__Equals); | ||
|
||
NamedTypeSymbol equalityComparerType = equalityComparer_Equals.ContainingType; | ||
|
||
BoundExpression? retExpression = initialExpression; | ||
|
||
// Compare fields | ||
foreach (var field in fields) | ||
{ | ||
// Prepare constructed comparer | ||
var constructedEqualityComparer = equalityComparerType.Construct(field.Type); | ||
|
||
// System.Collections.Generic.EqualityComparer<T_index>. | ||
// Default.Equals(this.backingFld_index, local.backingFld_index)' | ||
BoundExpression nextEquals = F.Call( | ||
F.StaticCall(constructedEqualityComparer, | ||
equalityComparer_get_Default.AsMember(constructedEqualityComparer)), | ||
equalityComparer_Equals.AsMember(constructedEqualityComparer), | ||
F.Field(F.This(), field), | ||
F.Field(otherReceiver, field)); | ||
|
||
// Generate 'retExpression' = 'retExpression && nextEquals' | ||
retExpression = retExpression is null | ||
? nextEquals | ||
: F.LogicalAnd(retExpression, nextEquals); | ||
} | ||
|
||
RoslynDebug.AssertNotNull(retExpression); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This assertion does not appear justified. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Earlier There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is there always at least one iteration of the foreach? According to the spec a record can have an empty parameter list. In reply to: 418268985 [](ancestors = 418268985) |
||
|
||
return retExpression; | ||
} | ||
|
||
/// <summary> | ||
/// Construct a body for a method containing a call to a single other method with the same signature (modulo name). | ||
/// </summary> | ||
|
@@ -263,12 +316,11 @@ internal static partial class MethodBodySynthesizer | |
internal static BoundBlock ConstructSingleInvocationMethodBody(SyntheticBoundNodeFactory F, MethodSymbol methodToInvoke, bool useBaseReference) | ||
{ | ||
var argBuilder = ArrayBuilder<BoundExpression>.GetInstance(); | ||
//var refKindBuilder = ArrayBuilder<RefKind>.GetInstance(); | ||
|
||
RoslynDebug.AssertNotNull(F.CurrentFunction); | ||
foreach (var param in F.CurrentFunction.Parameters) | ||
{ | ||
argBuilder.Add(F.Parameter(param)); | ||
//refKindBuilder.Add(param.RefKind); | ||
} | ||
|
||
BoundExpression invocation = F.Call(useBaseReference ? (BoundExpression)F.Base(baseType: methodToInvoke.ContainingType) : F.This(), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is this justified? Please add a PROTOYPE, issue, or bullet in the test plan.