-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Report error if 'record struct' constructor calls default parameterless constructor #58339
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3497,7 +3497,7 @@ private BoundNode BindConstructorBody(ConstructorDeclarationSyntax constructor, | |
|
||
bool thisInitializer = initializer?.IsKind(SyntaxKind.ThisConstructorInitializer) == true; | ||
if (!thisInitializer && | ||
ContainingType.GetMembersUnordered().OfType<SynthesizedRecordConstructor>().Any()) | ||
hasAnyRecordConstructors()) | ||
{ | ||
var constructorSymbol = (MethodSymbol)this.ContainingMember(); | ||
if (!constructorSymbol.IsStatic && | ||
|
@@ -3514,6 +3514,12 @@ private BoundNode BindConstructorBody(ConstructorDeclarationSyntax constructor, | |
&& thisInitializer | ||
&& ContainingType.IsDefaultValueTypeConstructor(initializer); | ||
|
||
if (skipInitializer && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fact that |
||
hasAnyRecordConstructors()) | ||
{ | ||
Error(diagnostics, ErrorCode.ERR_RecordStructConstructorCallsDefaultConstructor, initializer.ThisOrBaseKeyword); | ||
} | ||
|
||
// Using BindStatement to bind block to make sure we are reusing results of partial binding in SemanticModel | ||
return new BoundConstructorMethodBody(constructor, | ||
bodyBinder.GetDeclaredLocalsForScope(constructor), | ||
|
@@ -3524,6 +3530,9 @@ private BoundNode BindConstructorBody(ConstructorDeclarationSyntax constructor, | |
null : | ||
bodyBinder.BindExpressionBodyAsBlock(constructor.ExpressionBody, | ||
constructor.Body == null ? diagnostics : BindingDiagnosticBag.Discarded)); | ||
|
||
bool hasAnyRecordConstructors() => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
ContainingType.GetMembersUnordered().OfType<SynthesizedRecordConstructor>().Any(); | ||
} | ||
|
||
internal virtual BoundExpressionStatement BindConstructorInitializer(ConstructorInitializerSyntax initializer, BindingDiagnosticBag diagnostics) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6911,6 +6911,9 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ | |
<data name="ERR_LambdaWithAttributesToExpressionTree" xml:space="preserve"> | ||
<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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The wording looks too general. The requirement is only for records with primary constructors, I think the message should reflect that. See, for example, how we do that for
|
||
</data> | ||
<data name="WRN_CompileTimeCheckedOverflow" xml:space="preserve"> | ||
<value>The operation may overflow '{0}' at runtime (use 'unchecked' syntax to override)</value> | ||
</data> | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also check if
!constructorSymbol.IsStatic
? #Closed