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

Add support for nominal records #44774

Merged
merged 1 commit into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all 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/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -6110,7 +6110,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<value>init-only setters</value>
</data>
<data name="ERR_BadRecordDeclaration" xml:space="preserve">
<value>Records must have both a 'data' modifier and non-empty parameter list</value>
<value>A positional record must have both a 'data' modifier and non-empty 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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2927,36 +2927,39 @@ private void AddSynthesizedRecordMembersIfNecessary(MembersAndInitializersBuilde
}
}

if (paramList is null)
if (paramList is null && !_declModifiers.HasFlag(DeclarationModifiers.Data))
{
if (_declModifiers.HasFlag(DeclarationModifiers.Data))
{
diagnostics.Add(ErrorCode.ERR_BadRecordDeclaration, declaration.NameLocations[0]);
}
// Not a record
return;
}

if (paramList.ParameterCount == 0 || !_declModifiers.HasFlag(DeclarationModifiers.Data))
{
// PROTOTYPE: The semantics of an empty parameter list have not been decided. Error
// for now
diagnostics.Add(ErrorCode.ERR_BadRecordDeclaration, paramList.Location);
}

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);
}

var ctor = addCtor(paramList);
// Positional record
Copy link
Member

Choose a reason for hiding this comment

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

Positional record [](start = 15, length = 17)

nit: positional members?

if (!(paramList is null))
{
// PROTOTYPE: The semantics of an empty parameter list have not been decided. Error
// for now
if (paramList.ParameterCount == 0 || !_declModifiers.HasFlag(DeclarationModifiers.Data))
{
diagnostics.Add(ErrorCode.ERR_BadRecordDeclaration, paramList.Location);
}

BinderFactory binderFactory = this.DeclaringCompilation.GetBinderFactory(paramList.SyntaxTree);
var binder = binderFactory.GetBinder(paramList);

var ctor = addCtor(binder, paramList);
addProperties(ctor.Parameters);
}

addCopyCtor();
addCloneMethod();
addProperties(ctor.Parameters);

var thisEquals = addThisEquals();
addObjectEquals(thisEquals);
addHashCode();
Expand All @@ -2965,7 +2968,7 @@ private void AddSynthesizedRecordMembersIfNecessary(MembersAndInitializersBuilde

return;

SynthesizedRecordConstructor addCtor(ParameterListSyntax paramList)
SynthesizedRecordConstructor addCtor(Binder binder, ParameterListSyntax paramList)
{
var ctor = new SynthesizedRecordConstructor(this, binder, paramList, diagnostics);
if (!memberSignatures.ContainsKey(ctor))
Expand Down Expand Up @@ -3000,7 +3003,7 @@ void addCloneMethod()

void addProperties(ImmutableArray<ParameterSymbol> recordParameters)
{
foreach (ParameterSymbol param in ctor.Parameters)
foreach (ParameterSymbol param in recordParameters)
{
var property = new SynthesizedRecordPropertySymbol(this, param, diagnostics);
if (!memberSignatures.ContainsKey(property))
Expand Down Expand Up @@ -3063,8 +3066,12 @@ private void AddSynthesizedConstructorsIfNecessary(ArrayBuilder<Symbol> members,
switch (method.MethodKind)
{
case MethodKind.Constructor:
hasInstanceConstructor = true;
hasParameterlessInstanceConstructor = hasParameterlessInstanceConstructor || method.ParameterCount == 0;
// Ignore the record copy constructor
if (!(method is SynthesizedRecordCopyCtor))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we check 'method.IsImplicitlyDeclared' here instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's true for all SynthesizedInstanceMethodSymbols, including the record primary constructor, unfortunately

{
hasInstanceConstructor = true;
hasParameterlessInstanceConstructor = hasParameterlessInstanceConstructor || method.ParameterCount == 0;
}
break;

case MethodKind.StaticConstructor:
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadRecordDeclaration">
<source>Records must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">Records must have both a 'data' modifier and non-empty parameter list</target>
<source>A positional record must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">A positional record must have both a 'data' modifier and non-empty parameter list</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadSwitchValue">
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadRecordDeclaration">
<source>Records must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">Records must have both a 'data' modifier and non-empty parameter list</target>
<source>A positional record must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">A positional record must have both a 'data' modifier and non-empty parameter list</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadSwitchValue">
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadRecordDeclaration">
<source>Records must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">Records must have both a 'data' modifier and non-empty parameter list</target>
<source>A positional record must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">A positional record must have both a 'data' modifier and non-empty parameter list</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadSwitchValue">
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadRecordDeclaration">
<source>Records must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">Records must have both a 'data' modifier and non-empty parameter list</target>
<source>A positional record must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">A positional record must have both a 'data' modifier and non-empty parameter list</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadSwitchValue">
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadRecordDeclaration">
<source>Records must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">Records must have both a 'data' modifier and non-empty parameter list</target>
<source>A positional record must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">A positional record must have both a 'data' modifier and non-empty parameter list</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadSwitchValue">
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadRecordDeclaration">
<source>Records must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">Records must have both a 'data' modifier and non-empty parameter list</target>
<source>A positional record must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">A positional record must have both a 'data' modifier and non-empty parameter list</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadSwitchValue">
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadRecordDeclaration">
<source>Records must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">Records must have both a 'data' modifier and non-empty parameter list</target>
<source>A positional record must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">A positional record must have both a 'data' modifier and non-empty parameter list</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadSwitchValue">
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadRecordDeclaration">
<source>Records must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">Records must have both a 'data' modifier and non-empty parameter list</target>
<source>A positional record must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">A positional record must have both a 'data' modifier and non-empty parameter list</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadSwitchValue">
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadRecordDeclaration">
<source>Records must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">Records must have both a 'data' modifier and non-empty parameter list</target>
<source>A positional record must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">A positional record must have both a 'data' modifier and non-empty parameter list</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadSwitchValue">
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadRecordDeclaration">
<source>Records must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">Records must have both a 'data' modifier and non-empty parameter list</target>
<source>A positional record must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">A positional record must have both a 'data' modifier and non-empty parameter list</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadSwitchValue">
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadRecordDeclaration">
<source>Records must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">Records must have both a 'data' modifier and non-empty parameter list</target>
<source>A positional record must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">A positional record must have both a 'data' modifier and non-empty parameter list</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadSwitchValue">
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadRecordDeclaration">
<source>Records must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">Records must have both a 'data' modifier and non-empty parameter list</target>
<source>A positional record must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">A positional record must have both a 'data' modifier and non-empty parameter list</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadSwitchValue">
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_BadRecordDeclaration">
<source>Records must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">Records must have both a 'data' modifier and non-empty parameter list</target>
<source>A positional record must have both a 'data' modifier and non-empty parameter list</source>
<target state="new">A positional record must have both a 'data' modifier and non-empty parameter list</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadSwitchValue">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,31 @@ public class LookupPositionTests : CompilingTestBase
public void PositionalRecord1()
{
var text = @"
class C(int x, int y);";
data class C(int x, int y)
`{
`}";
var expectedNames = MakeExpectedSymbols(
Add( // Global
"System",
"Microsoft",
"C"));
"C"),
Add( // Members
"C C.Clone()",
"System.Boolean C.Equals(C? )",
"System.Boolean C.Equals(System.Object? )",
"System.Boolean System.Object.Equals(System.Object obj)",
"System.Boolean System.Object.Equals(System.Object objA, System.Object objB)",
"System.Boolean System.Object.ReferenceEquals(System.Object objA, System.Object objB)",
"System.Int32 C.GetHashCode()",
"System.Int32 C.x { get; init; }",
"System.Int32 C.y { get; init; }",
"System.Int32 System.Object.GetHashCode()",
"System.Object System.Object.MemberwiseClone()",
"System.String System.Object.ToString()",
"System.Type System.Object.GetType()",
"void System.Object.Finalize()"),
s_pop
);

TestLookupNames(text, expectedNames);
}
Expand All @@ -42,7 +61,7 @@ class C(int x, int y);";
public void PositionalRecord2()
{
var text = @"
`class C`<T`>(int x, T t = default(T));";
data `class C`<T`>(int x, T t = default(T));";
var expectedNames = MakeExpectedSymbols(
Add( // Global
"System",
Expand Down Expand Up @@ -71,6 +90,47 @@ public void PositionalRecord2()
TestLookupNames(text, expectedNames);
}

[Fact]
public void NominalRecord()
{
var text = @"
data `class C`<T`>
`{
int x { get; }
T t { get; }
`}";
var members = new[] {
"C<T> C<T>.Clone()",
"System.Int32 C<T>.x { get; }",
"T C<T>.t { get; }",
"System.Boolean C<T>.Equals(C<T>? )",
"System.Boolean C<T>.Equals(System.Object? )",
"System.Boolean System.Object.Equals(System.Object obj)",
"System.Boolean System.Object.Equals(System.Object objA, System.Object objB)",
"System.Boolean System.Object.ReferenceEquals(System.Object objA, System.Object objB)",
"System.Int32 C<T>.GetHashCode()",
"System.Int32 System.Object.GetHashCode()",
"System.Object System.Object.MemberwiseClone()",
"void System.Object.Finalize()",
"System.String System.Object.ToString()",
"System.Type System.Object.GetType()",
};
var expectedNames = MakeExpectedSymbols(
Add( // Global
"System",
"Microsoft",
"C<T>"),
Add( // C decl
"T"),
Add(members), // members are visible in type parameter list
s_pop,
Add(members), // body
Combine(s_pop, s_pop) // remove members and type parameters
);

TestLookupNames(text, expectedNames);
}

[Fact]
public void ExpressionBodiedProp()
{
Expand Down
Loading