-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Records: allow positional members to be implemented with fields #52480
Changes from all commits
fb4765c
54271fa
5a9bec3
1f026b2
cddf8ab
cda3204
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3460,16 +3460,22 @@ private void AddSynthesizedRecordMembersIfNecessary(MembersAndInitializersBuilde | |
}; | ||
|
||
var memberSignatures = s_duplicateRecordMemberSignatureDictionary.Allocate(); | ||
var fieldsByName = PooledDictionary<string, Symbol>.GetInstance(); | ||
var membersSoFar = builder.GetNonTypeMembers(declaredMembersAndInitializers); | ||
var members = ArrayBuilder<Symbol>.GetInstance(membersSoFar.Count + 1); | ||
foreach (var member in membersSoFar) | ||
{ | ||
switch (member) | ||
{ | ||
case FieldSymbol: | ||
case EventSymbol: | ||
case MethodSymbol { MethodKind: not (MethodKind.Ordinary or MethodKind.Constructor) }: | ||
continue; | ||
case FieldSymbol { Name: var fieldName }: | ||
if (!fieldsByName.ContainsKey(fieldName)) | ||
{ | ||
fieldsByName.Add(fieldName, member); | ||
} | ||
Comment on lines
+3474
to
+3477
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. nit: it feels like this could be replaced with 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. If it's okay, I'll leave as-is. Thanks |
||
continue; | ||
} | ||
|
||
if (!memberSignatures.ContainsKey(member)) | ||
|
@@ -3533,6 +3539,7 @@ private void AddSynthesizedRecordMembersIfNecessary(MembersAndInitializersBuilde | |
addToStringMethod(printMembers); | ||
|
||
memberSignatures.Free(); | ||
fieldsByName.Free(); | ||
|
||
// We put synthesized record members first so that errors about conflicts show up on user-defined members rather than all | ||
// going to the record declaration | ||
|
@@ -3542,8 +3549,10 @@ private void AddSynthesizedRecordMembersIfNecessary(MembersAndInitializersBuilde | |
|
||
return; | ||
|
||
void addDeconstruct(SynthesizedRecordConstructor ctor, ImmutableArray<PropertySymbol> properties) | ||
void addDeconstruct(SynthesizedRecordConstructor ctor, ImmutableArray<Symbol> positionalMembers) | ||
{ | ||
Debug.Assert(positionalMembers.All(p => p is PropertySymbol or FieldSymbol)); | ||
|
||
var targetMethod = new SignatureOnlyMethodSymbol( | ||
WellKnownMemberNames.DeconstructMethodName, | ||
this, | ||
|
@@ -3563,7 +3572,7 @@ void addDeconstruct(SynthesizedRecordConstructor ctor, ImmutableArray<PropertySy | |
|
||
if (!memberSignatures.TryGetValue(targetMethod, out Symbol? existingDeconstructMethod)) | ||
{ | ||
members.Add(new SynthesizedRecordDeconstruct(this, ctor, properties, memberOffset: members.Count, diagnostics)); | ||
members.Add(new SynthesizedRecordDeconstruct(this, ctor, positionalMembers, memberOffset: members.Count, diagnostics)); | ||
} | ||
else | ||
{ | ||
|
@@ -3722,9 +3731,9 @@ void addToStringMethod(MethodSymbol printMethod) | |
} | ||
} | ||
|
||
ImmutableArray<PropertySymbol> addProperties(ImmutableArray<ParameterSymbol> recordParameters) | ||
ImmutableArray<Symbol> addProperties(ImmutableArray<ParameterSymbol> recordParameters) | ||
{ | ||
var existingOrAddedMembers = ArrayBuilder<PropertySymbol>.GetInstance(recordParameters.Length); | ||
var existingOrAddedMembers = ArrayBuilder<Symbol>.GetInstance(recordParameters.Length); | ||
int addedCount = 0; | ||
foreach (ParameterSymbol param in recordParameters) | ||
{ | ||
|
@@ -3739,16 +3748,26 @@ ImmutableArray<PropertySymbol> addProperties(ImmutableArray<ParameterSymbol> rec | |
ImmutableArray<CustomModifier>.Empty, | ||
isStatic: false, | ||
ImmutableArray<PropertySymbol>.Empty); | ||
|
||
if (!memberSignatures.TryGetValue(targetProperty, out var existingMember)) | ||
if (!memberSignatures.TryGetValue(targetProperty, out var existingMember) | ||
&& !fieldsByName.TryGetValue(param.Name, out existingMember)) | ||
{ | ||
existingMember = OverriddenOrHiddenMembersHelpers.FindFirstHiddenMemberIfAny(targetProperty, memberIsFromSomeCompilation: true); | ||
isInherited = true; | ||
} | ||
|
||
// There should be an error if we picked a member that is hidden | ||
// This will be fixed in C# 9 as part of 16.10. Tracked by https://github.com/dotnet/roslyn/issues/52630 | ||
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. Do we plan to do this after feature merge? 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. Yes. I'll make the breaking change in 16.10 and both changes will meet in |
||
|
||
if (existingMember is null) | ||
{ | ||
addProperty(new SynthesizedRecordPropertySymbol(this, syntax, param, isOverride: false, diagnostics)); | ||
} | ||
else if (existingMember is FieldSymbol { IsStatic: false } field | ||
&& field.TypeWithAnnotations.Equals(param.TypeWithAnnotations, TypeCompareKind.AllIgnoreOptions)) | ||
{ | ||
Binder.CheckFeatureAvailability(syntax, MessageID.IDS_FeaturePositionalFieldsInRecords, diagnostics); | ||
existingOrAddedMembers.Add(field); | ||
} | ||
else if (existingMember is PropertySymbol { IsStatic: false, GetMethod: { } } prop | ||
&& prop.TypeWithAnnotations.Equals(param.TypeWithAnnotations, TypeCompareKind.AllIgnoreOptions)) | ||
{ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
It looks like for fields the only thing we are going to compare is the Name. I think we should consider simply using a separate dictionary for that and avoiding an introduction of SignatureOnlyFieldSymbol with the only purpose to lookup a field by name. #Closed