Skip to content

Fix auto-default behavior when value-assigning a ref field#69283

Merged
RikkiGibson merged 10 commits intodotnet:mainfrom
RikkiGibson:ref-field-auto-default
Aug 2, 2023
Merged

Fix auto-default behavior when value-assigning a ref field#69283
RikkiGibson merged 10 commits intodotnet:mainfrom
RikkiGibson:ref-field-auto-default

Conversation

@RikkiGibson
Copy link
Copy Markdown
Member

@RikkiGibson RikkiGibson commented Jul 28, 2023

Closes #69148

Fixes an issue where a value-assignment to an unassigned ref-field was not treated as a read of the ref field, but was treated as a write of the ref field. It seems like it should be both. Have added dataflow tests accordingly.

Also adds an enabled-by-default warning when a ref field is used before being ref-assigned. I feel this is likely enough to not be what you wanted to do that it's worth reporting. Note that only a disabled-by-default warning is reported if you exit the constructor without assigning the field.

@ghost ghost added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jul 28, 2023
@RikkiGibson RikkiGibson marked this pull request as ready for review July 31, 2023 22:22
@RikkiGibson RikkiGibson requested a review from a team as a code owner July 31, 2023 22:22
<value>Reference kind modifier of parameter doesn't match the corresponding parameter in target.</value>
</data>
<data name="WRN_UseDefViolationRefField" xml:space="preserve">
<value>ref field '{0}' should be ref-assigned before use.</value>
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.

Should the warning sentence start with an uppercase letter?

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 don't know. I thought it's better this way because it looks more like the ref modifier.

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.

  <data name="ERR_RefReturningPropertiesCannotBeRequired" xml:space="preserve">
    <value>Ref returning properties cannot be required.</value>
  </data>

seems like decent enough precedent to change the casing.


ref struct RS
{
ref int ri;
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.

Will the warning be reported even if the field is ref readonly (and hence the assignment is an error)? Consider testing that.

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.

Flow analysis behaves the same whether the referent is readonly or not. I'll add a test.

Diagnostic(ErrorCode.ERR_RbraceExpected, "").WithLocation(27, 2));
}

[Fact]
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.

[WorkItem("https://github.com/dotnet/roslyn/issues/69148")]?

// public RS()
Diagnostic(ErrorCode.WRN_UnassignedThisSupportedVersion, "RS").WithArguments("RS.ri").WithLocation(15, 12),
// (17,21): warning CS9201: 'ref' field 'ri' should be ref-assigned before use.
// int local = ri; // 1
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.

Suggested change
// int local = ri; // 1
// int local = ri;

@RikkiGibson RikkiGibson requested review from a team and jjonescz August 1, 2023 18:50
{
private readonly ref int _i;
public R(ref int i) { _i = i; }
public R(ref int i) { _i = ref i; }
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.

Nice.

@RikkiGibson RikkiGibson merged commit 637a13b into dotnet:main Aug 2, 2023
@ghost ghost added this to the Next milestone Aug 2, 2023
@dibarbet dibarbet modified the milestones: Next, 17.8 P2 Aug 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ref struct with ref field

4 participants