Skip to content
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 9 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ internal static int GetParameterCount(this Symbol member)
case SymbolKind.Property:
return ((PropertySymbol)member).ParameterCount;
case SymbolKind.Event:
case SymbolKind.Field:
return 0;
default:
throw ExceptionUtilities.UnexpectedValue(member.Kind);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Copy link
Member

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.

Copy link
Member Author

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.

members.Add(objEquals);
}
}
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

members.Add(hashCode);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Microsoft.CodeAnalysis.CSharp.Symbols
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// 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;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Reflection;
using Microsoft.Cci;
using Microsoft.CodeAnalysis.PooledObjects;

namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
Expand Down Expand Up @@ -118,4 +116,4 @@ internal override void GenerateMethodBody(TypeCompilationState compilationState,
F.CloseMethod(F.Return(F.Literal(0)));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

Expand All @@ -20,7 +20,6 @@ internal sealed class SynthesizedRecordObjEquals : SynthesizedInstanceMethodSymb

public override ImmutableArray<ParameterSymbol> Parameters { get; }


public SynthesizedRecordObjEquals(NamedTypeSymbol containingType, MethodSymbol typedRecordEquals)
{
_typedRecordEquals = typedRecordEquals;
Expand Down Expand Up @@ -152,4 +151,4 @@ internal override void GenerateMethodBody(TypeCompilationState compilationState,
F.CloseMethod(F.Block(ImmutableArray.Create<BoundStatement>(F.Return(expression))));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
// 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;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Reflection;
using System.Text;
using Microsoft.Cci;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
Expand Down
Loading