Skip to content

Implicit field initialization in struct constructors#59788

Merged
RikkiGibson merged 36 commits intodotnet:release/dev17.3from
RikkiGibson:struct-definite-assignment
Mar 28, 2022
Merged

Implicit field initialization in struct constructors#59788
RikkiGibson merged 36 commits intodotnet:release/dev17.3from
RikkiGibson:struct-definite-assignment

Conversation

@RikkiGibson
Copy link
Copy Markdown
Member

@RikkiGibson RikkiGibson commented Feb 26, 2022

Related to #60167

@cston @jcouv for review.

This design introduces a set of "disabled by default" diagnostics for definite assignment analysis of 'this' in struct constructors. Currently, in order to enable the diagnostics, the user would need to do something like the following:

# .editorconfig
dotnet_diagnostic.CS9018.severity = warning
dotnet_diagnostic.CS9019.severity = warning
dotnet_diagnostic.CS9020.severity = warning
dotnet_diagnostic.CS9021.severity = warning
dotnet_diagnostic.CS9022.severity = warning

I considered introducing some kind of shorthand which would allow specifying this whole set of diagnostics at once, but it feels like it might be worth waiting for user feedback before we add something like that.

@ghost ghost added the Area-Compilers label Feb 26, 2022
var builder = ArrayBuilder<BoundStatement>.GetInstance(implicitlyInitializedFields.Length + 1);
foreach (var field in implicitlyInitializedFields)
{
builder.Add(new BoundExpressionStatement(
Copy link
Copy Markdown
Member Author

@RikkiGibson RikkiGibson Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a use site diagnostic here? see CodeGenTupleTests.CustomValueTuple_StructWithConstructor in this branch.

It might make more sense to give a "Missing compiler required member" on the tuple declaration which is missing the well-known fields.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm leaning toward allowing the current behavior here. Declaring your own ValueTuple is a pretty rare thing to do and I wasn't able to find a decent way to introduce the use site diagnostics here without adding redundant use site diagnostics in other scenarios.

@RikkiGibson RikkiGibson marked this pull request as ready for review March 3, 2022 20:23
@RikkiGibson RikkiGibson requested review from a team as code owners March 3, 2022 20:23
@RikkiGibson
Copy link
Copy Markdown
Member Author

@cston @jcouv for another review pass

Copy link
Copy Markdown
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with review pass (iteration 32). Overall, looks very nice

Copy link
Copy Markdown
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thanks (iteration 33)

references: moduleReference,
options: TestOptions.DebugDll.WithWarningLevel(5),
parseOptions: TestOptions.Regular10,
verify: Verification.Skipped);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider modifying those tests to also verify execution (expectedOutput: should show reads of auto-defaulted values)

{
var associatedSymbol = fieldSymbol.AssociatedSymbol;
var hasAssociatedProperty = associatedSymbol?.Kind == SymbolKind.Property;
var symbolName = associatedSymbol?.Kind == SymbolKind.Property ? associatedSymbol.Name : fieldSymbol.Name;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

associatedSymbol?.Kind == SymbolKind.Property

Minor: hasAssociatedProperty

while (true)
{
var containingSlot = variableBySlot[fieldSlot].ContainingSlot;
if (containingSlot == fieldSlot)
Copy link
Copy Markdown
Contributor

@cston cston Mar 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

containingSlot == fieldSlot

Minor: This seems surprising to check that fieldSlot is the same as its containingSlot. Could we simply check if (fieldSlot == 0) instead, and before using variableBySlot[fieldSlot].ContainingSlot?

@RikkiGibson
Copy link
Copy Markdown
Member Author

This is should be ready to go now @jaredpar. There are some test plan items that can be addressed in the coming weeks.

@RikkiGibson RikkiGibson enabled auto-merge (squash) March 28, 2022 21:59
@RikkiGibson RikkiGibson merged commit 65d5234 into dotnet:release/dev17.3 Mar 28, 2022
@RikkiGibson RikkiGibson deleted the struct-definite-assignment branch March 28, 2022 23:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants