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

Synthesize record properties and ctor #41011

Merged
merged 6 commits into from
Jan 24, 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
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1592,7 +1592,7 @@ private static bool AccessingAutoPropertyFromConstructor(BoundExpression receive
propertySymbol = propertySymbol.OriginalDefinition;
}

var sourceProperty = propertySymbol as SourcePropertySymbol;
var sourceProperty = propertySymbol as SourceOrRecordPropertySymbol;
var propertyIsStatic = propertySymbol.IsStatic;

return (object)sourceProperty != null &&
Expand Down
3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -5933,4 +5933,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_BadRecordDeclaration" xml:space="preserve">
<value>Records must have both a 'data' modifier and parameter list</value>
</data>
<data name="ERR_DuplicateRecordConstructor" xml:space="preserve">
<value>There cannot be a primary constructor and a member constructor with the same parameter types.</value>
</data>
</root>
10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,16 @@ private static BoundBlock BindMethodBody(MethodSymbol method, TypeCompilationSta
return null;
}
}
else if (method is SynthesizedInstanceConstructor ctor)
{
// Synthesized instance constructors may partially synthesize
// their body
var node = ctor.GetNonNullSyntaxNode();
var factory = new SyntheticBoundNodeFactory(ctor, node, compilationState, diagnostics);
var stmts = ArrayBuilder<BoundStatement>.GetInstance();
ctor.GenerateMethodBodyStatements(factory, stmts, diagnostics);
body = BoundBlock.SynthesizedNoLocals(node, stmts.ToImmutableAndFree());
}
else
{
// synthesized methods should return their bound bodies
Expand Down
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1739,6 +1739,7 @@ internal enum ErrorCode

ERR_ExternEventInitializer = 8760,
ERR_BadRecordDeclaration = 8761,
ERR_DuplicateRecordConstructor = 8762,

// Note: you will need to re-generate compiler code after adding warnings (eng\generate-compiler-code.cmd)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ private void SynthesizeClosureEnvironments(ArrayBuilder<ClosureDebugInfo> closur
AddSynthesizedMethod(
frame.Constructor,
FlowAnalysisPass.AppendImplicitReturn(
MethodCompiler.BindMethodBody(frame.Constructor, CompilationState, null),
MethodCompiler.BindMethodBody(frame.Constructor, CompilationState, Diagnostics),
agocke marked this conversation as resolved.
Show resolved Hide resolved
frame.Constructor));
}

Expand Down Expand Up @@ -540,7 +540,7 @@ private SynthesizedClosureEnvironment GetStaticFrame(DiagnosticBag diagnostics,
AddSynthesizedMethod(
frame.Constructor,
FlowAnalysisPass.AppendImplicitReturn(
MethodCompiler.BindMethodBody(frame.Constructor, CompilationState, null),
MethodCompiler.BindMethodBody(frame.Constructor, CompilationState, diagnostics),
frame.Constructor));

// add cctor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ private BoundExpression MakeStaticAssignmentOperator(
}
}

#nullable enable

private BoundExpression MakePropertyAssignment(
SyntaxNode syntax,
BoundExpression rewrittenReceiver,
Expand All @@ -292,12 +294,13 @@ private BoundExpression MakePropertyAssignment(
// Rewrite property assignment into call to setter.
var setMethod = property.GetOwnOrInheritedSetMethod();

if ((object)setMethod == null)
if (setMethod is null)
{
Debug.Assert((property as SourcePropertySymbol)?.IsAutoProperty == true,
var autoProp = (SourceOrRecordPropertySymbol)property;
Debug.Assert(autoProp.IsAutoProperty,
"only autoproperties can be assignable without having setters");

var backingField = (property as SourcePropertySymbol).BackingField;
var backingField = autoProp.BackingField;
return _factory.AssignmentExpression(
_factory.Field(rewrittenReceiver, backingField),
rewrittenRight);
Expand All @@ -323,7 +326,7 @@ private BoundExpression MakePropertyAssignment(
// Save expression value to a temporary before calling the
// setter, and restore the temporary after the setter, so the
// assignment can be used as an embedded expression.
TypeSymbol exprType = rewrittenRight.Type;
TypeSymbol? exprType = rewrittenRight.Type;

LocalSymbol rhsTemp = _factory.SynthesizedLocal(exprType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,11 @@ public int GetHashCode(Symbol member)

// CONSIDER: modify hash for constraints?

hash = Hash.Combine(member.GetMemberArity(), hash);
hash = Hash.Combine(member.GetParameterCount(), hash);
if (member.Kind != SymbolKind.Field)
{
hash = Hash.Combine(member.GetMemberArity(), hash);
hash = Hash.Combine(member.GetParameterCount(), hash);
}
}
return hash;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2323,6 +2323,7 @@ internal void AddOrWrapTupleMembers(SourceMemberContainerTypeSymbol type)
case TypeKind.Struct:
CheckForStructBadInitializers(builder, diagnostics);
CheckForStructDefaultConstructors(builder.NonTypeNonIndexerMembers, isEnum: false, diagnostics: diagnostics);
AddSynthesizedRecordMembersIfNecessary(builder, diagnostics);
AddSynthesizedConstructorsIfNecessary(builder.NonTypeNonIndexerMembers, builder.StaticInitializers, diagnostics);
break;

Expand All @@ -2334,6 +2335,7 @@ internal void AddOrWrapTupleMembers(SourceMemberContainerTypeSymbol type)
case TypeKind.Class:
case TypeKind.Interface:
case TypeKind.Submission:
AddSynthesizedRecordMembersIfNecessary(builder, diagnostics);
// No additional checking required.
AddSynthesizedConstructorsIfNecessary(builder.NonTypeNonIndexerMembers, builder.StaticInitializers, diagnostics);
break;
Expand Down Expand Up @@ -2846,9 +2848,19 @@ private void CheckForStructBadInitializers(MembersAndInitializersBuilder builder
}
}

private void AddSynthesizedRecordMembersIfNecessary(ArrayBuilder<Symbol> members, DiagnosticBag diagnostics)
private void AddSynthesizedRecordMembersIfNecessary(MembersAndInitializersBuilder builder, DiagnosticBag diagnostics)
{
Debug.Assert(declaration.Kind == DeclarationKind.Class || declaration.Kind == DeclarationKind.Struct);
switch (declaration.Kind)
{
case DeclarationKind.Class:
case DeclarationKind.Struct:
break;

default:
return;
}

var members = builder.NonTypeNonIndexerMembers;

ParameterListSyntax? paramList = null;
foreach (SingleTypeDeclaration decl in declaration.Declarations)
Expand Down Expand Up @@ -2901,6 +2913,21 @@ private void AddSynthesizedRecordMembersIfNecessary(ArrayBuilder<Symbol> members
{
members.Add(ctor);
}
else
{
diagnostics.Add(ErrorCode.ERR_DuplicateRecordConstructor, paramList.Location);
}

foreach (ParameterSymbol param in ctor.Parameters)
{
var property = new SynthesizedRecordPropertySymbol(this, param);
if (!memberSignatures.Contains(property))
{
members.Add(property);
members.Add(property.GetMethod);
members.Add(property.BackingField);
}
}
}

private void AddSynthesizedConstructorsIfNecessary(ArrayBuilder<Symbol> members, ArrayBuilder<ImmutableArray<FieldOrPropertyInitializer>> staticInitializers, DiagnosticBag diagnostics)
Expand All @@ -2909,7 +2936,6 @@ private void AddSynthesizedConstructorsIfNecessary(ArrayBuilder<Symbol> members,
{
case DeclarationKind.Class:
case DeclarationKind.Struct:
AddSynthesizedRecordMembersIfNecessary(members, diagnostics);
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,9 @@ private void CheckMembersAgainstBaseType(
}
}
}
else
else if (property is SourcePropertySymbol sourceProperty)
{
var isNewProperty = ((SourcePropertySymbol)property).IsNew;
var isNewProperty = sourceProperty.IsNew;
CheckNonOverrideMember(property, isNewProperty, property.OverriddenOrHiddenMembers, diagnostics, out suppressAccessors);

if (!suppressAccessors)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
internal abstract class SourceOrRecordPropertySymbol : PropertySymbol, IAttributeTargetSymbol
{
public Location Location { get; }

public SourceOrRecordPropertySymbol(Location location)
{
Location = location;
}

internal abstract SynthesizedBackingFieldSymbol BackingField { get; }

internal abstract bool IsAutoProperty { get; }

internal abstract bool HasPointerType { get; }

public abstract SyntaxList<AttributeListSyntax> AttributeDeclarationSyntaxList { get; }

protected abstract IAttributeTargetSymbol AttributesOwner { get; }

protected abstract AttributeLocation AllowedAttributeLocations { get; }

protected abstract AttributeLocation DefaultAttributeLocation { get; }

IAttributeTargetSymbol IAttributeTargetSymbol.AttributesOwner => AttributesOwner;

AttributeLocation IAttributeTargetSymbol.AllowedAttributeLocations => AllowedAttributeLocations;

AttributeLocation IAttributeTargetSymbol.DefaultAttributeLocation => DefaultAttributeLocation;
}
}
Loading