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

Implicit field initialization in struct constructors #59788

Merged

Conversation

RikkiGibson
Copy link
Contributor

@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.

var builder = ArrayBuilder<BoundStatement>.GetInstance(implicitlyInitializedFields.Length + 1);
foreach (var field in implicitlyInitializedFields)
{
builder.Add(new BoundExpressionStatement(
Copy link
Contributor 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
Contributor 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
Contributor Author

@cston @jcouv for another review pass

Copy link
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
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
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
Member

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
Member

@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
Contributor 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