-
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 inherited properties for records #44705
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4c0770a
Use inherited properties
cston 7aac024
Fix tests
cston 7a6ff04
Misc.
cston f0921cf
Handle duplicate properties
cston b6f906e
Match updated spec
cston 454320b
Fix formatting
cston 5c34ef4
Merge remote-tracking branch 'upstream/features/records' into 44616
cston f8e388b
Merge remote-tracking branch 'upstream/features/records' into 44616
cston 46b87c4
Misc.
cston File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2946,11 +2946,13 @@ private void AddSynthesizedRecordMembersIfNecessary(MembersAndInitializersBuilde | |
BinderFactory binderFactory = this.DeclaringCompilation.GetBinderFactory(paramList.SyntaxTree); | ||
var binder = binderFactory.GetBinder(paramList); | ||
|
||
// PROTOTYPE: need to check base members as well | ||
var memberSignatures = s_duplicateMemberSignatureDictionary.Allocate(); | ||
foreach (var member in members) | ||
{ | ||
memberSignatures.Add(member, member); | ||
if (!memberSignatures.ContainsKey(member)) | ||
{ | ||
memberSignatures.Add(member, member); | ||
} | ||
} | ||
|
||
var ctor = addCtor(paramList); | ||
|
@@ -3000,25 +3002,58 @@ void addCloneMethod() | |
|
||
void addProperties(ImmutableArray<ParameterSymbol> recordParameters) | ||
{ | ||
foreach (ParameterSymbol param in ctor.Parameters) | ||
var inheritedProperties = PooledDictionary<string, PropertySymbol>.GetInstance(); | ||
HashSet<DiagnosticInfo>? useSiteDiagnostics = null; | ||
addInheritedProperties(inheritedProperties, this, this, ref useSiteDiagnostics); | ||
foreach (ParameterSymbol param in recordParameters) | ||
{ | ||
var property = new SynthesizedRecordPropertySymbol(this, param, diagnostics); | ||
if (!memberSignatures.ContainsKey(property)) | ||
if (!memberSignatures.ContainsKey(property) && | ||
!(inheritedProperties.TryGetValue(property.Name, out var inheritedProperty) && matchesRecordProperty(inheritedProperty, param.TypeWithAnnotations))) | ||
{ | ||
members.Add(property); | ||
members.Add(property.GetMethod); | ||
members.Add(property.SetMethod); | ||
members.Add(property.BackingField); | ||
} | ||
} | ||
inheritedProperties.Free(); | ||
} | ||
|
||
static bool matchesRecordProperty(PropertySymbol property, TypeWithAnnotations requiredType) | ||
{ | ||
Debug.Assert(property.ParameterCount == 0); | ||
|
||
return !property.IsStatic && | ||
property.RefKind == RefKind.None && | ||
requiredType.Equals(property.TypeWithAnnotations, TypeCompareKind.AllIgnoreOptions); | ||
} | ||
|
||
static void addInheritedProperties(Dictionary<string, PropertySymbol> properties, NamedTypeSymbol within, NamedTypeSymbol type, ref HashSet<DiagnosticInfo>? useSiteDiagnostics) | ||
{ | ||
type = type.BaseTypeNoUseSiteDiagnostics; | ||
if (type is null) | ||
{ | ||
return; | ||
} | ||
addInheritedProperties(properties, within, type, ref useSiteDiagnostics); | ||
foreach (var member in type.GetMembers()) | ||
{ | ||
if (member is PropertySymbol property && | ||
property.ParameterCount == 0 && | ||
AccessCheck.IsSymbolAccessible(property, within, ref useSiteDiagnostics)) | ||
{ | ||
properties[property.Name] = property; | ||
} | ||
} | ||
} | ||
|
||
void addObjectEquals(MethodSymbol thisEquals) | ||
{ | ||
var objEquals = new SynthesizedRecordObjEquals(this, thisEquals); | ||
if (!memberSignatures.ContainsKey(objEquals)) | ||
{ | ||
// PROTOTYPE: Don't add if the overridden method is sealed | ||
// https://github.com/dotnet/roslyn/issues/44617: Don't add if the overridden method is sealed | ||
members.Add(objEquals); | ||
} | ||
} | ||
|
@@ -3028,7 +3063,7 @@ void addHashCode() | |
var hashCode = new SynthesizedRecordGetHashCode(this); | ||
if (!memberSignatures.ContainsKey(hashCode)) | ||
{ | ||
// PROTOTYPE: Don't add if the overridden method is sealed | ||
// https://github.com/dotnet/roslyn/issues/44617: Don't add if the overridden method is sealed | ||
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. Same here. |
||
members.Add(hashCode); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I believe this behavior is correct now. The last LDM confirmed that it is an error if these methods are sealed, which I think is the current behavior.
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.
Thanks. I'll address with the
Equals
changes in a separate PR.