未验证 提交 56ce0562 编写于 作者: C Charles Stoner 提交者: GitHub

Use inherited properties for records (#44705)

上级 eebb3e44
......@@ -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);
......
......@@ -473,7 +473,7 @@ internal static OverriddenOrHiddenMembersResult MakeInterfaceOverriddenOrHiddenM
///
/// In incorrect or imported code, it is possible that both currTypeBestMatch and hiddenBuilder will be populated.
/// </remarks>
private static void FindOverriddenOrHiddenMembersInType(
internal static void FindOverriddenOrHiddenMembersInType(
Symbol member,
bool memberIsFromSomeCompilation,
NamedTypeSymbol memberContainingType,
......
......@@ -2933,11 +2933,13 @@ private void AddSynthesizedRecordMembersIfNecessary(MembersAndInitializersBuilde
return;
}
// 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);
}
}
// Positional record
......@@ -3006,7 +3008,8 @@ void addProperties(ImmutableArray<ParameterSymbol> recordParameters)
foreach (ParameterSymbol param in recordParameters)
{
var property = new SynthesizedRecordPropertySymbol(this, param, diagnostics);
if (!memberSignatures.ContainsKey(property))
if (!memberSignatures.ContainsKey(property) &&
!hidesInheritedMember(property, this))
{
members.Add(property);
members.Add(property.GetMethod);
......@@ -3016,12 +3019,37 @@ void addProperties(ImmutableArray<ParameterSymbol> recordParameters)
}
}
static bool hidesInheritedMember(Symbol symbol, NamedTypeSymbol type)
{
while ((type = type.BaseTypeNoUseSiteDiagnostics) is object)
{
OverriddenOrHiddenMembersHelpers.FindOverriddenOrHiddenMembersInType(
symbol,
memberIsFromSomeCompilation: true,
memberContainingType: symbol.ContainingType,
currType: type,
out var bestMatch,
out bool hasSameKindNonMatch,
out var hiddenBuilder);
if (hiddenBuilder is object)
{
hiddenBuilder.Free();
return true;
}
if (bestMatch is object)
{
return true;
}
}
return false;
}
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);
}
}
......@@ -3031,7 +3059,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
members.Add(hashCode);
}
}
......
......@@ -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
......
// 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;
......@@ -10,7 +9,6 @@
using System.Collections.Immutable;
using System.Reflection;
using Microsoft.Cci;
using Microsoft.CodeAnalysis.PooledObjects;
namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
......@@ -118,4 +116,4 @@ internal override void GenerateMethodBody(TypeCompilationState compilationState,
F.CloseMethod(F.Return(F.Literal(0)));
}
}
}
\ No newline at end of file
}
// 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.
......@@ -20,7 +20,6 @@ internal sealed class SynthesizedRecordObjEquals : SynthesizedInstanceMethodSymb
public override ImmutableArray<ParameterSymbol> Parameters { get; }
public SynthesizedRecordObjEquals(NamedTypeSymbol containingType, MethodSymbol typedRecordEquals)
{
_typedRecordEquals = typedRecordEquals;
......@@ -152,4 +151,4 @@ internal override void GenerateMethodBody(TypeCompilationState compilationState,
F.CloseMethod(F.Block(ImmutableArray.Create<BoundStatement>(F.Return(expression))));
}
}
}
\ No newline at end of file
}
......@@ -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
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册