Skip to content

Commit

Permalink
Improve error reporting for 'this()' initializer from 'record struct'…
Browse files Browse the repository at this point in the history
… constructor
  • Loading branch information
cston committed Jan 25, 2022
1 parent 5fd73c5 commit 588f867
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 41 deletions.
33 changes: 23 additions & 10 deletions src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3420,29 +3420,31 @@ private BoundNode BindConstructorBody(ConstructorDeclarationSyntax constructor,

bool thisInitializer = initializer?.IsKind(SyntaxKind.ThisConstructorInitializer) == true;
if (!thisInitializer &&
hasAnyRecordConstructors())
hasRecordPrimaryConstructor())
{
var constructorSymbol = (MethodSymbol)this.ContainingMember();
if (!constructorSymbol.IsStatic &&
if (isInstanceConstructor(out MethodSymbol constructorSymbol) &&
!SynthesizedRecordCopyCtor.IsCopyConstructor(constructorSymbol))
{
// Note: we check the constructor initializer of copy constructors elsewhere
Error(diagnostics, ErrorCode.ERR_UnexpectedOrMissingConstructorInitializerInRecord, initializer?.ThisOrBaseKeyword ?? constructor.Identifier);
}
}

// The `: this()` initializer is ignored when it is a default value type constructor
// and we need to include field initializers into the constructor.
bool skipInitializer = includesFieldInitializers
&& thisInitializer
bool isDefaultValueTypeInitializer = thisInitializer
&& ContainingType.IsDefaultValueTypeConstructor(initializer);

if (skipInitializer &&
hasAnyRecordConstructors())
if (isDefaultValueTypeInitializer &&
isInstanceConstructor(out _) &&
hasRecordPrimaryConstructor())
{
Error(diagnostics, ErrorCode.ERR_RecordStructConstructorCallsDefaultConstructor, initializer.ThisOrBaseKeyword);
}

// The `: this()` initializer is ignored when it is a default value type constructor
// and we need to include field initializers into the constructor.
bool skipInitializer = includesFieldInitializers
&& isDefaultValueTypeInitializer;

// Using BindStatement to bind block to make sure we are reusing results of partial binding in SemanticModel
return new BoundConstructorMethodBody(constructor,
bodyBinder.GetDeclaredLocalsForScope(constructor),
Expand All @@ -3454,8 +3456,19 @@ private BoundNode BindConstructorBody(ConstructorDeclarationSyntax constructor,
bodyBinder.BindExpressionBodyAsBlock(constructor.ExpressionBody,
constructor.Body == null ? diagnostics : BindingDiagnosticBag.Discarded));

bool hasAnyRecordConstructors() =>
bool hasRecordPrimaryConstructor() =>
ContainingType.GetMembersUnordered().OfType<SynthesizedRecordConstructor>().Any();

bool isInstanceConstructor(out MethodSymbol constructorSymbol)
{
if (this.ContainingMember() is MethodSymbol { IsStatic: false } method)
{
constructorSymbol = method;
return true;
}
constructorSymbol = null;
return false;
}
}

internal virtual BoundExpressionStatement BindConstructorInitializer(ConstructorInitializerSyntax initializer, BindingDiagnosticBag diagnostics)
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -6867,7 +6867,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<value>A lambda expression with attributes cannot be converted to an expression tree</value>
</data>
<data name="ERR_RecordStructConstructorCallsDefaultConstructor" xml:space="preserve">
<value>A 'this' initializer for a 'record struct' constructor must call the primary constructor or an explicitly declared constructor.</value>
<value>A constructor declared in a 'record struct' with parameter list must have a 'this' initializer that calls the primary constructor or an explicitly declared constructor.</value>
</data>
<data name="WRN_CompileTimeCheckedOverflow" xml:space="preserve">
<value>The operation may overflow '{0}' at runtime (use 'unchecked' syntax to override)</value>
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 588f867

Please sign in to comment.