Skip to content

Commit 6415297

Browse files
author
msftbot[bot]
authored
Merge pull request #45952 from dotnet/merges/master-to-features/target-typing
Merge master to features/target-typing
2 parents 8a0457a + 0535a47 commit 6415297

20 files changed

+870
-63
lines changed

src/Compilers/CSharp/Portable/Symbols/OverriddenOrHiddenMembersHelpers.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,22 @@ private static OverriddenOrHiddenMembersResult MakeOverriddenOrHiddenMembersWork
129129
return MakeInterfaceOverriddenOrHiddenMembers(member, memberIsFromSomeCompilation);
130130
}
131131

132-
Symbol bestMatch = null;
133-
ArrayBuilder<Symbol> hiddenBuilder = null;
132+
ArrayBuilder<Symbol> hiddenBuilder;
133+
ImmutableArray<Symbol> overriddenMembers;
134+
ImmutableArray<Symbol> runtimeOverriddenMembers;
135+
FindOverriddenOrHiddenMembers(member, containingType, memberIsFromSomeCompilation, out hiddenBuilder, out overriddenMembers, out runtimeOverriddenMembers);
136+
137+
ImmutableArray<Symbol> hiddenMembers = hiddenBuilder == null ? ImmutableArray<Symbol>.Empty : hiddenBuilder.ToImmutableAndFree();
138+
return OverriddenOrHiddenMembersResult.Create(overriddenMembers, hiddenMembers, runtimeOverriddenMembers);
139+
}
134140

141+
private static void FindOverriddenOrHiddenMembers(Symbol member, NamedTypeSymbol containingType, bool memberIsFromSomeCompilation,
142+
out ArrayBuilder<Symbol> hiddenBuilder,
143+
out ImmutableArray<Symbol> overriddenMembers,
144+
out ImmutableArray<Symbol> runtimeOverriddenMembers)
145+
{
146+
Symbol bestMatch = null;
147+
hiddenBuilder = null;
135148
for (NamedTypeSymbol currType = containingType.BaseTypeNoUseSiteDiagnostics;
136149
(object)currType != null && (object)bestMatch == null && hiddenBuilder == null;
137150
currType = currType.BaseTypeNoUseSiteDiagnostics)
@@ -149,12 +162,19 @@ private static OverriddenOrHiddenMembersResult MakeOverriddenOrHiddenMembersWork
149162

150163
// Based on bestMatch, find other methods that will be overridden, hidden, or runtime overridden
151164
// (in bestMatch.ContainingType).
152-
ImmutableArray<Symbol> overriddenMembers;
153-
ImmutableArray<Symbol> runtimeOverriddenMembers;
154165
FindRelatedMembers(member.IsOverride, memberIsFromSomeCompilation, member.Kind, bestMatch, out overriddenMembers, out runtimeOverriddenMembers, ref hiddenBuilder);
166+
}
155167

156-
ImmutableArray<Symbol> hiddenMembers = hiddenBuilder == null ? ImmutableArray<Symbol>.Empty : hiddenBuilder.ToImmutableAndFree();
157-
return OverriddenOrHiddenMembersResult.Create(overriddenMembers, hiddenMembers, runtimeOverriddenMembers);
168+
public static Symbol FindFirstHiddenMemberIfAny(Symbol member, bool memberIsFromSomeCompilation)
169+
{
170+
ArrayBuilder<Symbol> hiddenBuilder;
171+
FindOverriddenOrHiddenMembers(member, member.ContainingType, memberIsFromSomeCompilation, out hiddenBuilder,
172+
overriddenMembers: out _, runtimeOverriddenMembers: out _);
173+
174+
Symbol result = hiddenBuilder?.FirstOrDefault();
175+
hiddenBuilder?.Free();
176+
177+
return result;
158178
}
159179

160180
/// <summary>

src/Compilers/CSharp/Portable/Symbols/SignatureOnlyPropertySymbol.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public SignatureOnlyPropertySymbol(
7575

7676
public override bool IsVirtual { get { throw ExceptionUtilities.Unreachable; } }
7777

78-
public override bool IsOverride { get { throw ExceptionUtilities.Unreachable; } }
78+
public override bool IsOverride => false;
7979

8080
public override bool IsAbstract { get { throw ExceptionUtilities.Unreachable; } }
8181

src/Compilers/CSharp/Portable/Symbols/Source/SourceComplexParameterSymbol.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,18 @@ public override string MetadataName
341341

342342
AttributeLocation IAttributeTargetSymbol.DefaultAttributeLocation => AttributeLocation.Parameter;
343343

344-
AttributeLocation IAttributeTargetSymbol.AllowedAttributeLocations => AttributeLocation.Parameter;
344+
AttributeLocation IAttributeTargetSymbol.AllowedAttributeLocations
345+
{
346+
get
347+
{
348+
if (SynthesizedRecordPropertySymbol.HaveCorrespondingSynthesizedRecordPropertySymbol(this))
349+
{
350+
return AttributeLocation.Parameter | AttributeLocation.Property | AttributeLocation.Field;
351+
}
352+
353+
return AttributeLocation.Parameter;
354+
}
355+
}
345356

346357
/// <summary>
347358
/// Symbol to copy bound attributes from, or null if the attributes are not shared among multiple source parameter symbols.

src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3079,16 +3079,24 @@ ImmutableArray<PropertySymbol> addProperties(ImmutableArray<ParameterSymbol> rec
30793079
{
30803080
bool isInherited = false;
30813081
var syntax = param.GetNonNullSyntaxNode();
3082-
var property = new SynthesizedRecordPropertySymbol(this, syntax, param, isOverride: false, diagnostics);
3083-
if (!memberSignatures.TryGetValue(property, out var existingMember))
3082+
3083+
var targetProperty = new SignatureOnlyPropertySymbol(param.Name,
3084+
this,
3085+
ImmutableArray<ParameterSymbol>.Empty,
3086+
RefKind.None,
3087+
param.TypeWithAnnotations,
3088+
ImmutableArray<CustomModifier>.Empty,
3089+
isStatic: false,
3090+
ImmutableArray<PropertySymbol>.Empty);
3091+
3092+
if (!memberSignatures.TryGetValue(targetProperty, out var existingMember))
30843093
{
3085-
Debug.Assert(property.OverriddenOrHiddenMembers.OverriddenMembers.Length == 0); // property is not virtual and should not have overrides
3086-
existingMember = property.OverriddenOrHiddenMembers.HiddenMembers.FirstOrDefault();
3094+
existingMember = OverriddenOrHiddenMembersHelpers.FindFirstHiddenMemberIfAny(targetProperty, memberIsFromSomeCompilation: true);
30873095
isInherited = true;
30883096
}
30893097
if (existingMember is null)
30903098
{
3091-
addProperty(property);
3099+
addProperty(new SynthesizedRecordPropertySymbol(this, syntax, param, isOverride: false, diagnostics));
30923100
}
30933101
else if (existingMember is PropertySymbol { IsStatic: false, GetMethod: { } } prop
30943102
&& prop.TypeWithAnnotations.Equals(param.TypeWithAnnotations, TypeCompareKind.AllIgnoreOptions))

src/Compilers/CSharp/Portable/Symbols/Source/SourcePropertySymbol.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ private static bool HasInitializer(SyntaxNode syntax)
139139
public override SyntaxList<AttributeListSyntax> AttributeDeclarationSyntaxList
140140
=> ((BasePropertyDeclarationSyntax)CSharpSyntaxNode).AttributeLists;
141141

142+
public override IAttributeTargetSymbol AttributesOwner => this;
143+
142144
private static void GetAccessorDeclarations(
143145
CSharpSyntaxNode syntaxNode,
144146
DiagnosticBag diagnostics,

src/Compilers/CSharp/Portable/Symbols/Source/SourcePropertySymbolBase.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -705,8 +705,6 @@ internal CSharpSyntaxNode CSharpSyntaxNode
705705
}
706706
}
707707

708-
public abstract SyntaxList<AttributeListSyntax> AttributeDeclarationSyntaxList { get; }
709-
710708
internal SyntaxTree SyntaxTree
711709
{
712710
get
@@ -980,7 +978,11 @@ private SynthesizedSealedPropertyAccessor MakeSynthesizedSealedAccessor()
980978

981979
#region Attributes
982980

983-
IAttributeTargetSymbol IAttributeTargetSymbol.AttributesOwner => this;
981+
public abstract SyntaxList<AttributeListSyntax> AttributeDeclarationSyntaxList { get; }
982+
983+
public abstract IAttributeTargetSymbol AttributesOwner { get; }
984+
985+
IAttributeTargetSymbol IAttributeTargetSymbol.AttributesOwner => AttributesOwner;
984986

985987
AttributeLocation IAttributeTargetSymbol.DefaultAttributeLocation => AttributeLocation.Property;
986988

src/Compilers/CSharp/Portable/Symbols/Synthesized/Records/SynthesizedRecordClone.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,12 @@ internal override void GenerateMethodBody(TypeCompilationState compilationState,
119119
{
120120
var F = new SyntheticBoundNodeFactory(this, ContainingType.GetNonNullSyntaxNode(), compilationState, diagnostics);
121121

122-
var members = ContainingType.GetMembers(WellKnownMemberNames.InstanceConstructorName);
122+
var members = ContainingType.InstanceConstructors;
123123
foreach (var member in members)
124124
{
125125
var ctor = (MethodSymbol)member;
126-
if (ctor.ParameterCount == 1 &&
127-
ctor.Parameters[0].Type.Equals(ContainingType, TypeCompareKind.ConsiderEverything))
126+
if (ctor.ParameterCount == 1 && ctor.Parameters[0].RefKind == RefKind.None &&
127+
ctor.Parameters[0].Type.Equals(ContainingType, TypeCompareKind.AllIgnoreOptions))
128128
{
129129
F.CloseMethod(F.Return(F.New(ctor, F.This())));
130130
return;

src/Compilers/CSharp/Portable/Symbols/Synthesized/Records/SynthesizedRecordEqualityContractProperty.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace Microsoft.CodeAnalysis.CSharp.Symbols
1717
{
18-
internal sealed class SynthesizedRecordEqualityContractProperty : SourcePropertySymbolBase, IAttributeTargetSymbol
18+
internal sealed class SynthesizedRecordEqualityContractProperty : SourcePropertySymbolBase
1919
{
2020
internal const string PropertyName = "EqualityContract";
2121

@@ -52,21 +52,17 @@ public SynthesizedRecordEqualityContractProperty(
5252

5353
public override ImmutableArray<SyntaxReference> DeclaringSyntaxReferences => ImmutableArray<SyntaxReference>.Empty;
5454

55-
IAttributeTargetSymbol IAttributeTargetSymbol.AttributesOwner => this;
56-
57-
AttributeLocation IAttributeTargetSymbol.AllowedAttributeLocations => AttributeLocation.None;
55+
public override SyntaxList<AttributeListSyntax> AttributeDeclarationSyntaxList
56+
=> new SyntaxList<AttributeListSyntax>();
5857

59-
AttributeLocation IAttributeTargetSymbol.DefaultAttributeLocation => AttributeLocation.None;
58+
public override IAttributeTargetSymbol AttributesOwner => this;
6059

6160
protected override Location TypeLocation
6261
=> ContainingType.Locations[0];
6362

6463
protected override SyntaxTokenList GetModifierTokens(SyntaxNode syntax)
6564
=> new SyntaxTokenList();
6665

67-
public override SyntaxList<AttributeListSyntax> AttributeDeclarationSyntaxList
68-
=> new SyntaxList<AttributeListSyntax>();
69-
7066
protected override void CheckForBlockAndExpressionBody(CSharpSyntaxNode syntax, DiagnosticBag diagnostics)
7167
{
7268
// Nothing to do here

src/Compilers/CSharp/Portable/Symbols/Synthesized/Records/SynthesizedRecordPropertySymbol.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
namespace Microsoft.CodeAnalysis.CSharp.Symbols
1313
{
14-
internal sealed class SynthesizedRecordPropertySymbol : SourcePropertySymbolBase, IAttributeTargetSymbol
14+
internal sealed class SynthesizedRecordPropertySymbol : SourcePropertySymbolBase
1515
{
16-
public ParameterSymbol BackingParameter { get; }
16+
public SourceParameterSymbol BackingParameter { get; }
1717

1818
public SynthesizedRecordPropertySymbol(
1919
SourceMemberContainerTypeSymbol containingType,
@@ -42,14 +42,11 @@ public SynthesizedRecordPropertySymbol(
4242
hasParameters: false,
4343
diagnostics)
4444
{
45-
BackingParameter = backingParameter;
45+
BackingParameter = (SourceParameterSymbol)backingParameter;
4646
}
4747

48-
IAttributeTargetSymbol IAttributeTargetSymbol.AttributesOwner => this;
4948

50-
AttributeLocation IAttributeTargetSymbol.AllowedAttributeLocations => AttributeLocation.None;
51-
52-
AttributeLocation IAttributeTargetSymbol.DefaultAttributeLocation => AttributeLocation.None;
49+
public override IAttributeTargetSymbol AttributesOwner => BackingParameter as IAttributeTargetSymbol ?? this;
5350

5451
protected override Location TypeLocation
5552
=> ((ParameterSyntax)CSharpSyntaxNode).Type!.Location;
@@ -58,7 +55,7 @@ protected override SyntaxTokenList GetModifierTokens(SyntaxNode syntax)
5855
=> new SyntaxTokenList();
5956

6057
public override SyntaxList<AttributeListSyntax> AttributeDeclarationSyntaxList
61-
=> new SyntaxList<AttributeListSyntax>();
58+
=> BackingParameter.AttributeDeclarationList;
6259

6360
protected override void CheckForBlockAndExpressionBody(CSharpSyntaxNode syntax, DiagnosticBag diagnostics)
6461
{
@@ -112,5 +109,10 @@ protected override TypeWithAnnotations ComputeType(Binder? binder, SyntaxNode sy
112109
protected override bool HasPointerTypeSyntactically
113110
// Since we already bound the type, don't bother looking at syntax
114111
=> TypeWithAnnotations.DefaultType.IsPointerOrFunctionPointer();
112+
113+
public static bool HaveCorrespondingSynthesizedRecordPropertySymbol(SourceParameterSymbol parameter)
114+
{
115+
return parameter.ContainingType.GetMembersUnordered().Any((s, parameter) => (s as SynthesizedRecordPropertySymbol)?.BackingParameter == (object)parameter, parameter);
116+
}
115117
}
116118
}

src/Compilers/CSharp/Portable/Symbols/Synthesized/SynthesizedBackingFieldSymbol.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public SynthesizedBackingFieldSymbol(
4141
}
4242

4343
protected override IAttributeTargetSymbol AttributeOwner
44-
=> _property;
44+
=> _property.AttributesOwner;
4545

4646
internal override Location ErrorLocation
4747
=> _property.Location;

0 commit comments

Comments
 (0)