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

Readonly members more test coverage #34602

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Invocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,7 @@ ContainingMemberOrLambda is MethodSymbol containingMethod &&
!method.IsEffectivelyReadOnly &&
!method.IsStatic)
{
Error(diagnostics, ErrorCode.WRN_ImplicitCopyInReadOnlyMember, receiver.Syntax, method.Name, ThisParameterSymbol.SymbolName);
Error(diagnostics, ErrorCode.WRN_ImplicitCopyInReadOnlyMember, receiver.Syntax, method, ThisParameterSymbol.SymbolName);
return false;
}

Expand Down
11 changes: 10 additions & 1 deletion src/Compilers/CSharp/Portable/CSharpResources.Designer.cs

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

5 changes: 4 additions & 1 deletion src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -5431,7 +5431,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<value>Call to non-readonly member from a 'readonly' member results in an implicit copy.</value>
</data>
<data name="ERR_StaticMemberCantBeReadOnly" xml:space="preserve">
<value>Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.</value>
<value>Static member '{0}' cannot be marked 'readonly'.</value>
</data>
<data name="ERR_AutoSetterCantBeReadOnly" xml:space="preserve">
<value>Auto-implemented 'set' accessor '{0}' cannot be marked 'readonly'.</value>
Expand All @@ -5451,6 +5451,9 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_PartialMethodReadOnlyDifference" xml:space="preserve">
<value>Both partial method declarations must be readonly or neither may be readonly</value>
</data>
<data name="ERR_ReadOnlyModMissingAccessor" xml:space="preserve">
<value>'{0}': 'readonly' can only be used on accessors if the property or indexer has both a get and a set accessor</value>
</data>
<data name="WRN_NullabilityMismatchInArgument" xml:space="preserve">
<value>Argument of type '{0}' cannot be used for parameter '{2}' of type '{1}' in '{3}' due to differences in the nullability of reference types.</value>
</data>
Expand Down
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 @@ -1691,6 +1691,7 @@ internal enum ErrorCode
ERR_DuplicatePropertyReadOnlyMods = 8660,
ERR_FieldLikeEventCantBeReadOnly = 8661,
ERR_PartialMethodReadOnlyDifference = 8662,
ERR_ReadOnlyModMissingAccessor = 8663

#endregion diagnostics introduced for C# 8.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ protected void CheckModifiersAndType(DiagnosticBag diagnostics)
}
else if (IsReadOnly && IsStatic)
{
// Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.
// Static member '{0}' cannot be marked 'readonly'.
diagnostics.Add(ErrorCode.ERR_StaticMemberCantBeReadOnly, location, this);
}
else if (IsReadOnly && HasAssociatedField)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ private void CheckModifiers(bool hasBody, Location location, DiagnosticBag diagn
}
else if (IsStatic && IsDeclaredReadOnly)
{
// Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.
// Static member '{0}' cannot be marked 'readonly'.
diagnostics.Add(ErrorCode.ERR_StaticMemberCantBeReadOnly, location, this);
}
else if (IsAbstract && !ContainingType.IsAbstract && (ContainingType.TypeKind == TypeKind.Class || ContainingType.TypeKind == TypeKind.Submission))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ private void CheckModifiers(Location location, bool hasBody, bool isAutoProperty
}
else if (LocalDeclaredReadOnly && IsStatic)
{
// Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.
// Static member '{0}' cannot be marked 'readonly'.
diagnostics.Add(ErrorCode.ERR_StaticMemberCantBeReadOnly, location, this);
}
else if (LocalDeclaredReadOnly && _isAutoPropertyAccessor && MethodKind == MethodKind.PropertySet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,12 @@ private SourcePropertySymbol(
{
diagnostics.Add(ErrorCode.ERR_AccessModMissingAccessor, location, this);
}

// Check that 'readonly' is not set on the one accessor.
if (accessor.LocalDeclaredReadOnly)
{
diagnostics.Add(ErrorCode.ERR_ReadOnlyModMissingAccessor, location, this);
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
Expand Down Expand Up @@ -910,7 +916,7 @@ private void CheckModifiers(Location location, bool isIndexer, DiagnosticBag dia
}
else if (IsStatic && HasReadOnlyModifier)
{
// Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.
// Static member '{0}' cannot be marked 'readonly'.
diagnostics.Add(ErrorCode.ERR_StaticMemberCantBeReadOnly, location, this);
}
else if (IsOverride && (IsNew || IsVirtual))
Expand Down
9 changes: 7 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@
<target state="translated">Dílčí vzor vlastnosti vyžaduje odkaz na vlastnost nebo pole k přiřazení, např. „{{ Name: {0} }}“.</target>
<note />
</trans-unit>
<trans-unit id="ERR_ReadOnlyModMissingAccessor">
<source>'{0}': 'readonly' can only be used on accessors if the property or indexer has both a get and a set accessor</source>
<target state="new">'{0}': 'readonly' can only be used on accessors if the property or indexer has both a get and a set accessor</target>
<note />
</trans-unit>
<trans-unit id="ERR_RefAssignNarrower">
<source>Cannot ref-assign '{1}' to '{0}' because '{1}' has a narrower escape scope than '{0}'.</source>
<target state="translated">Přiřazení odkazu {1} k {0} nelze provést, protože {1} má užší řídicí obor než {0}.</target>
Expand All @@ -318,8 +323,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_StaticMemberCantBeReadOnly">
<source>Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.</source>
<target state="new">Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.</target>
<source>Static member '{0}' cannot be marked 'readonly'.</source>
<target state="new">Static member '{0}' cannot be marked 'readonly'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_SwitchArmSubsumed">
Expand Down
9 changes: 7 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@
<target state="translated">Ein Eigenschaftsteilmuster erfordert einen Verweis auf die abzugleichende Eigenschaft oder das abzugleichende Feld. Beispiel: "{{ Name: {0} }}"</target>
<note />
</trans-unit>
<trans-unit id="ERR_ReadOnlyModMissingAccessor">
<source>'{0}': 'readonly' can only be used on accessors if the property or indexer has both a get and a set accessor</source>
<target state="new">'{0}': 'readonly' can only be used on accessors if the property or indexer has both a get and a set accessor</target>
<note />
</trans-unit>
<trans-unit id="ERR_RefAssignNarrower">
<source>Cannot ref-assign '{1}' to '{0}' because '{1}' has a narrower escape scope than '{0}'.</source>
<target state="translated">ref-assign von "{1}" zu "{0}" ist nicht möglich, weil "{1}" einen geringeren Escapebereich als "{0}" aufweist.</target>
Expand All @@ -318,8 +323,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_StaticMemberCantBeReadOnly">
<source>Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.</source>
<target state="new">Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.</target>
<source>Static member '{0}' cannot be marked 'readonly'.</source>
<target state="new">Static member '{0}' cannot be marked 'readonly'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_SwitchArmSubsumed">
Expand Down
9 changes: 7 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@
<target state="translated">El subpatrón de una propiedad requiere una referencia a la propiedad o al campo que debe coincidir; por ejemplo, "{{ Name: {0} }}"</target>
<note />
</trans-unit>
<trans-unit id="ERR_ReadOnlyModMissingAccessor">
<source>'{0}': 'readonly' can only be used on accessors if the property or indexer has both a get and a set accessor</source>
<target state="new">'{0}': 'readonly' can only be used on accessors if the property or indexer has both a get and a set accessor</target>
<note />
</trans-unit>
<trans-unit id="ERR_RefAssignNarrower">
<source>Cannot ref-assign '{1}' to '{0}' because '{1}' has a narrower escape scope than '{0}'.</source>
<target state="translated">No se puede asignar referencia "{1}" a "{0}" porque "{1}" tiene un ámbito de escape más limitado que "{0}".</target>
Expand All @@ -318,8 +323,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_StaticMemberCantBeReadOnly">
<source>Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.</source>
<target state="new">Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.</target>
<source>Static member '{0}' cannot be marked 'readonly'.</source>
<target state="new">Static member '{0}' cannot be marked 'readonly'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_SwitchArmSubsumed">
Expand Down
9 changes: 7 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@
<target state="translated">Un sous-modèle de propriété nécessite une correspondance de la référence à la propriété ou au champ. Exemple : '{{ Nom: {0} }}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ReadOnlyModMissingAccessor">
<source>'{0}': 'readonly' can only be used on accessors if the property or indexer has both a get and a set accessor</source>
<target state="new">'{0}': 'readonly' can only be used on accessors if the property or indexer has both a get and a set accessor</target>
<note />
</trans-unit>
<trans-unit id="ERR_RefAssignNarrower">
<source>Cannot ref-assign '{1}' to '{0}' because '{1}' has a narrower escape scope than '{0}'.</source>
<target state="translated">Impossible d'effectuer une assignation par référence de '{1}' vers '{0}', car '{1}' a une portée de sortie plus limitée que '{0}'.</target>
Expand All @@ -318,8 +323,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_StaticMemberCantBeReadOnly">
<source>Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.</source>
<target state="new">Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.</target>
<source>Static member '{0}' cannot be marked 'readonly'.</source>
<target state="new">Static member '{0}' cannot be marked 'readonly'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_SwitchArmSubsumed">
Expand Down
9 changes: 7 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@
<target state="translated">Con un criterio secondario di proprietà è richiesto un riferimento alla proprietà o al campo da abbinare, ad esempio '{{ Name: {0} }}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ReadOnlyModMissingAccessor">
<source>'{0}': 'readonly' can only be used on accessors if the property or indexer has both a get and a set accessor</source>
<target state="new">'{0}': 'readonly' can only be used on accessors if the property or indexer has both a get and a set accessor</target>
<note />
</trans-unit>
<trans-unit id="ERR_RefAssignNarrower">
<source>Cannot ref-assign '{1}' to '{0}' because '{1}' has a narrower escape scope than '{0}'.</source>
<target state="translated">Non è possibile assegnare '{1}' a '{0}' come ref perché l'ambito di escape di '{1}' è ridotto rispetto a quello di '{0}'.</target>
Expand All @@ -318,8 +323,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_StaticMemberCantBeReadOnly">
<source>Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.</source>
<target state="new">Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.</target>
<source>Static member '{0}' cannot be marked 'readonly'.</source>
<target state="new">Static member '{0}' cannot be marked 'readonly'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_SwitchArmSubsumed">
Expand Down
9 changes: 7 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@
<target state="translated">プロパティ サブパターンには、一致させるプロパティまたはフィールドへの参照が必要です。例: '{{ Name: {0} }}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_ReadOnlyModMissingAccessor">
<source>'{0}': 'readonly' can only be used on accessors if the property or indexer has both a get and a set accessor</source>
<target state="new">'{0}': 'readonly' can only be used on accessors if the property or indexer has both a get and a set accessor</target>
<note />
</trans-unit>
<trans-unit id="ERR_RefAssignNarrower">
<source>Cannot ref-assign '{1}' to '{0}' because '{1}' has a narrower escape scope than '{0}'.</source>
<target state="translated">'{1}' を '{0}' に ref 割り当てすることはできません。'{1}' のエスケープ スコープが '{0}' より狭いためです。</target>
Expand All @@ -318,8 +323,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_StaticMemberCantBeReadOnly">
<source>Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.</source>
<target state="new">Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.</target>
<source>Static member '{0}' cannot be marked 'readonly'.</source>
<target state="new">Static member '{0}' cannot be marked 'readonly'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_SwitchArmSubsumed">
Expand Down
9 changes: 7 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@
<target state="translated">속성 하위 패턴은 일치시킬 속성 또는 필드에 대한 참조가 필요합니다(예: '{{ Name: {0} }}')</target>
<note />
</trans-unit>
<trans-unit id="ERR_ReadOnlyModMissingAccessor">
<source>'{0}': 'readonly' can only be used on accessors if the property or indexer has both a get and a set accessor</source>
<target state="new">'{0}': 'readonly' can only be used on accessors if the property or indexer has both a get and a set accessor</target>
<note />
</trans-unit>
<trans-unit id="ERR_RefAssignNarrower">
<source>Cannot ref-assign '{1}' to '{0}' because '{1}' has a narrower escape scope than '{0}'.</source>
<target state="translated">'{1}'을(를) '{0}'에 참조 할당할 수 없습니다. '{1}'이(가) '{0}'보다 이스케이프 범위가 좁기 때문입니다.</target>
Expand All @@ -318,8 +323,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_StaticMemberCantBeReadOnly">
<source>Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.</source>
<target state="new">Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.</target>
<source>Static member '{0}' cannot be marked 'readonly'.</source>
<target state="new">Static member '{0}' cannot be marked 'readonly'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_SwitchArmSubsumed">
Expand Down
9 changes: 7 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@
<target state="translated">Wzorzec podrzędny właściwości wymaga odwołania do właściwości lub pola, które należy dopasować, na przykład „{{ Name: {0} }}”</target>
<note />
</trans-unit>
<trans-unit id="ERR_ReadOnlyModMissingAccessor">
<source>'{0}': 'readonly' can only be used on accessors if the property or indexer has both a get and a set accessor</source>
<target state="new">'{0}': 'readonly' can only be used on accessors if the property or indexer has both a get and a set accessor</target>
<note />
</trans-unit>
<trans-unit id="ERR_RefAssignNarrower">
<source>Cannot ref-assign '{1}' to '{0}' because '{1}' has a narrower escape scope than '{0}'.</source>
<target state="translated">Nie można przypisać odwołania elementu „{1}” do elementu „{0}”, ponieważ element „{1}” ma węższy zakres wyjścia niż element „{0}”.</target>
Expand All @@ -318,8 +323,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_StaticMemberCantBeReadOnly">
<source>Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.</source>
<target state="new">Static member '{0}' cannot be marked 'readonly' because readonly members cannot modify 'this' and static members do not have a 'this' parameter.</target>
<source>Static member '{0}' cannot be marked 'readonly'.</source>
<target state="new">Static member '{0}' cannot be marked 'readonly'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_SwitchArmSubsumed">
Expand Down
Loading