-
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
Require definite assignment of all fields if struct includes any field initializers #57925
Conversation
@@ -43,7 +43,7 @@ internal class FlowAnalysisPass | |||
if (method.ReturnsVoid || method.IsIterator || method.IsAsyncEffectivelyReturningTask(compilation)) | |||
{ | |||
// we don't analyze synthesized void methods. | |||
if ((method.IsImplicitlyDeclared && !method.IsScriptInitializer) || | |||
if ((method.IsImplicitlyDeclared && !method.IsScriptInitializer && (!method.IsParameterlessConstructor() || method.IsDefaultValueTypeConstructor(requireZeroInit: true))) || |
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.
I find it a little brittle that we do this specifically for synthesized void methods. Basically, whenever a new synthesized void method is added to the compiler/language, we could forget to change this. Would it be possible to remove the condition, identify the problematic void methods that we don't want to analyze, and then introduce a new condition which excludes those methods more specifically? That way when a new kind of synthesized void method comes about it will just be analyzed by default and the author will have to make a special effort to opt out of it.
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.
I'd like to keep this change simple if possible in case we want to port this change to other branches. I've logged #58012 to update the condition separately.
@dotnet/roslyn-compiler please review, thanks. |
/azp run |
Azure Pipelines successfully started running 4 pipeline(s). |
@@ -43,7 +43,7 @@ internal class FlowAnalysisPass | |||
if (method.ReturnsVoid || method.IsIterator || method.IsAsyncEffectivelyReturningTask(compilation)) | |||
{ | |||
// we don't analyze synthesized void methods. | |||
if ((method.IsImplicitlyDeclared && !method.IsScriptInitializer) || | |||
if ((method.IsImplicitlyDeclared && !method.IsScriptInitializer && (!method.IsParameterlessConstructor() || method.IsDefaultValueTypeConstructor(requireZeroInit: true))) || |
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.
(!method.IsParameterlessConstructor() || method.IsDefaultValueTypeConstructor(requireZeroInit: true))
It looks like this condition will be false for a synthesized parameter-less constructor of a class. Therefore, we are going to Analyze
it, which we didn't do before. I assume that was not the intent of the change. #Closed
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.
Good catch, thanks. I've updated the if
condition.
I'm open to suggestions on how to improve the readability of the if
though. It feels like the call to IsDefaultValueTypeConstructor(bool requireZeroInit)
is the complicated part. One possibility is to change that method to return a value that describes the constructor (such as the enum
below), but that might mean performing extra work for callers that don't care about requireZeroInit
.
enum ValueTypeConstructorKind
{
None,
Explicit,
ImplicitZeroInit,
ImplicitWithFieldInitializers,
}
Done with review pass (commit 1) |
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 2)
…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)
See proposals/csharp-10.0/parameterless-struct-constructors.md:
Fixes #57870