Fix auto-default behavior when value-assigning a ref field#69283
Fix auto-default behavior when value-assigning a ref field#69283RikkiGibson merged 10 commits intodotnet:mainfrom
Conversation
| <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> |
There was a problem hiding this comment.
Should the warning sentence start with an uppercase letter?
There was a problem hiding this comment.
I don't know. I thought it's better this way because it looks more like the ref modifier.
There was a problem hiding this comment.
<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; |
There was a problem hiding this comment.
Will the warning be reported even if the field is ref readonly (and hence the assignment is an error)? Consider testing that.
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
[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 |
There was a problem hiding this comment.
| // int local = ri; // 1 | |
| // int local = ri; |
| { | ||
| private readonly ref int _i; | ||
| public R(ref int i) { _i = i; } | ||
| public R(ref int i) { _i = ref i; } |
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.