Skip to content
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

Merged
merged 2 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static BoundBlock Rewrite(
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))) ||
Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Contributor

@AlekseyTs AlekseyTs Nov 29, 2021

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

Copy link
Member Author

@cston cston Nov 29, 2021

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,
}

Analyze(compilation, method, block, diagnostics))
{
block = AppendImplicitReturn(block, method, originalBodyNested);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4932,19 +4932,19 @@ public void Struct_ImplementSynthesizedConstructor()
struct S
{
int a = 1;
int b;
int b = 2;
}
";
var source1 =
@"
struct S
{
int a = 1;
int b;
int b = 2;

public S()
{
b = 2;
b = 3;
}
}
";
Expand Down
Loading