Require definite assignment of all fields if struct includes any field initializers#57925
Require definite assignment of all fields if struct includes any field initializers#57925cston merged 2 commits intodotnet:mainfrom
Conversation
| { | ||
| // 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.
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.
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). |
| { | ||
| // 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.
(!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.
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) |
…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