Skip to content

Commit

Permalink
Fix auto-default behavior when value-assigning a ref field (dotnet#69283
Browse files Browse the repository at this point in the history
)
  • Loading branch information
RikkiGibson authored Aug 2, 2023
1 parent a7ec1a4 commit 637a13b
Show file tree
Hide file tree
Showing 22 changed files with 457 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -7784,4 +7784,10 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="WRN_TargetDifferentRefness_Title" xml:space="preserve">
<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>
</data>
<data name="WRN_UseDefViolationRefField_Title" xml:space="preserve">
<value>Ref field should be ref-assigned before use.</value>
</data>
</root>
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2265,6 +2265,7 @@ internal enum ErrorCode
WRN_TargetDifferentRefness = 9198,
ERR_OutAttrOnRefReadonlyParam = 9199,
WRN_RefReadonlyParameterDefaultValue = 9200,
WRN_UseDefViolationRefField = 9201,

#endregion

Expand Down
2 changes: 2 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ internal static int GetWarningLevel(ErrorCode code)
case ErrorCode.WRN_HidingDifferentRefness:
case ErrorCode.WRN_TargetDifferentRefness:
case ErrorCode.WRN_RefReadonlyParameterDefaultValue:
case ErrorCode.WRN_UseDefViolationRefField:
return 1;
default:
return 0;
Expand Down Expand Up @@ -2396,6 +2397,7 @@ internal static bool IsBuildOnlyDiagnostic(ErrorCode code)
case ErrorCode.ERR_OutAttrOnRefReadonlyParam:
case ErrorCode.WRN_RefReadonlyParameterDefaultValue:
case ErrorCode.WRN_ByValArraySizeConstRequired:
case ErrorCode.WRN_UseDefViolationRefField:
return false;
default:
// NOTE: All error codes must be explicitly handled in this switch statement
Expand Down
17 changes: 16 additions & 1 deletion src/Compilers/CSharp/Portable/FlowAnalysis/DefiniteAssignment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,18 @@ void addDiagnosticForStructField(int fieldSlot, FieldSymbol fieldSymbol)
// should we handle nested fields here? https://github.com/dotnet/roslyn/issues/59890
AddImplicitlyInitializedField((FieldSymbol)fieldIdentifier.Symbol);

if (compilation.IsFeatureEnabled(MessageID.IDS_FeatureAutoDefaultStructs))
if (fieldSymbol.RefKind != RefKind.None)
{
// 'hasAssociatedProperty' is only true here in error scenarios where we don't need to report this as a cascading diagnostic
if (!hasAssociatedProperty)
{
Diagnostics.Add(
ErrorCode.WRN_UseDefViolationRefField,
node.Location,
symbolName);
}
}
else if (compilation.IsFeatureEnabled(MessageID.IDS_FeatureAutoDefaultStructs))
{
Diagnostics.Add(
hasAssociatedProperty ? ErrorCode.WRN_UseDefViolationPropertySupportedVersion : ErrorCode.WRN_UseDefViolationFieldSupportedVersion,
Expand Down Expand Up @@ -1497,6 +1508,10 @@ private Symbol UseNonFieldSymbolUnsafely(BoundExpression expression)

protected void Assign(BoundNode node, BoundExpression value, bool isRef = false, bool read = true)
{
if (!isRef && node is BoundFieldAccess { FieldSymbol.RefKind: not RefKind.None } fieldAccess)
{
CheckAssigned(fieldAccess, node.Syntax);
}
AssignImpl(node, value, written: true, isRef: isRef, read: read);
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 637a13b

Please sign in to comment.