-
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
Conversation
|
||
[Fact] | ||
[WorkItem(58328, "https://github.com/dotnet/roslyn/issues/58328")] | ||
public void ExplicitConstructors_05() |
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.
@@ -1993,7 +1993,7 @@ internal enum ErrorCode | |||
WRN_CallerArgumentExpressionParamForUnconsumedLocation = 8966, | |||
ERR_NewlinesAreNotAllowedInsideANonVerbatimInterpolatedString = 8967, | |||
ERR_AttrTypeArgCannotBeTypeVar = 8968, | |||
// WRN_AttrDependentTypeNotAllowed = 8969, // Backed out of of warning wave 6, may be reintroduced later | |||
ERR_RecordStructConstructorCallsDefaultConstructor = 8969, // Added in 17.1 for an invalid C#10 scenario. |
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.
Is reusing the warning number okay here? #Resolved
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.
Maybe, but reusing seems unnecessary. Updated.
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.
LGTM (commit 1)
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
A 'this' initializer for a 'record struct' constructor must call the primary constructor or an explicitly declared constructor.
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 UnexpectedOrMissingConstructorInitializerInRecord
:
<data name="ERR_UnexpectedOrMissingConstructorInitializerInRecord" xml:space="preserve">
<value>A constructor declared in a record with parameter list must have 'this' constructor initializer.</value>
</data>
``` #Closed
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
} | ||
record struct S4(char A, char B) | ||
{ | ||
public S4(object o) : this() { } |
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.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
The fact that skipInitializer
depends on includesFieldInitializers
looks suspicious. I think it is technically possible to have a primary constructor and no initializers. It would be good trying to construct a scenario like that. Consider instead using condition thisInitializer && ContainingType.IsDefaultValueTypeConstructor(initializer)
, I think that would exactly reflect the condition that design requires us to check.
#Closed
Done with review pass (commit 2) |
Responded to additional feedback in #58430. |
…implicit parameterless constructor (#59055) * Report error if 'record struct' constructor calls default parameterless constructor (#58339) * Improve error reporting for 'this()' initializer from 'record struct' constructor * Require definite assignment of all fields if struct includes any field initializers (#57925)
Confirmed with C# LDT:
In a
record struct
with a primary constructor, explicit constructors require athis()
initializer that invokes the primary constructor or another explicitly declared constructor.Fixes #58328