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 prototype cleanup #34468

Merged
1 change: 1 addition & 0 deletions docs/contributing/Compiler Test Plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ This document provides guidance for thinking about language interactions and tes
- NoPIA
- Dynamic
- Ref structs, Readonly structs
- Readonly members on structs (methods, property/indexer accessors, custom event accessors)

# Code
- Operators (see Eric's list below)
Expand Down
5 changes: 5 additions & 0 deletions docs/features/readonly-instance-members.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ The compiler would emit the instance member, as usual, and would additionally em

This would allow the user to safely call said instance method without the compiler needing to make a copy.

Some more "edge" cases that are explicitly permitted:

* Explicit interface implementations are allowed to be `readonly`.
* Partial methods are allowed to be `readonly`. Both signatures or neither must have the `readonly` keyword.

The restrictions would include:

* The `readonly` modifier cannot be applied to static methods, constructors or destructors.
Expand Down
9 changes: 9 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.Designer.cs

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

3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -5448,6 +5448,9 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_FieldLikeEventCantBeReadOnly" xml:space="preserve">
<value>Field-like event '{0}' cannot be 'readonly'.</value>
</data>
<data name="ERR_PartialMethodReadOnlyDifference" xml:space="preserve">
<value>Both partial method declarations must be readonly or neither may be readonly</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
3 changes: 2 additions & 1 deletion src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1689,7 +1689,8 @@ internal enum ErrorCode
ERR_AutoPropertyWithSetterCantBeReadOnly = 8658,
ERR_InvalidPropertyReadOnlyMods = 8659,
ERR_DuplicatePropertyReadOnlyMods = 8660,
ERR_FieldLikeEventCantBeReadOnly = 8661
ERR_FieldLikeEventCantBeReadOnly = 8661,
ERR_PartialMethodReadOnlyDifference = 8662,

#endregion diagnostics introduced for C# 8.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,11 @@ private static void PartialMethodChecks(SourceOrdinaryMethodSymbol definition, S
diagnostics.Add(ErrorCode.ERR_PartialMethodStaticDifference, implementation.Locations[0]);
}

if (definition.IsDeclaredReadOnly != implementation.IsDeclaredReadOnly)
{
diagnostics.Add(ErrorCode.ERR_PartialMethodReadOnlyDifference, implementation.Locations[0]);
}

if (definition.IsExtensionMethod != implementation.IsExtensionMethod)
{
diagnostics.Add(ErrorCode.ERR_PartialMethodExtensionDifference, implementation.Locations[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ public override RefKind RefKind
return RefKind.Out;
}

// PROTOTYPE: examine remaining locations where TypeSymbol.IsReadOnly is tested on 'this'.
// Determine whether it needs to be replaced with e.g. ContainingMethod.IsEffectivelyReadOnly.
if (ContainingType.IsReadOnly)
if (_containingMethod?.IsEffectivelyReadOnly == true)
{
return RefKind.In;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@
<target state="translated">Výstupní proměnná nemůže být deklarovaná jako lokální proměnná podle odkazu.</target>
<note />
</trans-unit>
<trans-unit id="ERR_PartialMethodReadOnlyDifference">
<source>Both partial method declarations must be readonly or neither may be readonly</source>
<target state="new">Both partial method declarations must be readonly or neither may be readonly</target>
<note />
</trans-unit>
<trans-unit id="ERR_PointerTypeInPatternMatching">
<source>Pattern-matching is not permitted for pointer types.</source>
<target state="translated">Porovnávání vzorů není povolené pro typy ukazatelů.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@
<target state="translated">Eine out-Variable kann nicht als lokales ref-Element deklariert werden.</target>
<note />
</trans-unit>
<trans-unit id="ERR_PartialMethodReadOnlyDifference">
<source>Both partial method declarations must be readonly or neither may be readonly</source>
<target state="new">Both partial method declarations must be readonly or neither may be readonly</target>
<note />
</trans-unit>
<trans-unit id="ERR_PointerTypeInPatternMatching">
<source>Pattern-matching is not permitted for pointer types.</source>
<target state="translated">Der Musterabgleich ist für Zeigertypen unzulässig.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@
<target state="translated">Una variable out no se puede declarar como ref local</target>
<note />
</trans-unit>
<trans-unit id="ERR_PartialMethodReadOnlyDifference">
<source>Both partial method declarations must be readonly or neither may be readonly</source>
<target state="new">Both partial method declarations must be readonly or neither may be readonly</target>
<note />
</trans-unit>
<trans-unit id="ERR_PointerTypeInPatternMatching">
<source>Pattern-matching is not permitted for pointer types.</source>
<target state="translated">No se permite la coincidencia de patrones para tipos de puntero.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@
<target state="translated">Impossible de déclarer une variable out en tant que variable locale ref</target>
<note />
</trans-unit>
<trans-unit id="ERR_PartialMethodReadOnlyDifference">
<source>Both partial method declarations must be readonly or neither may be readonly</source>
<target state="new">Both partial method declarations must be readonly or neither may be readonly</target>
<note />
</trans-unit>
<trans-unit id="ERR_PointerTypeInPatternMatching">
<source>Pattern-matching is not permitted for pointer types.</source>
<target state="translated">Les critères spéciaux ne sont pas autorisés pour les types de pointeur.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@
<target state="translated">Non è possibile dichiarare una variabile out come variabile locale ref</target>
<note />
</trans-unit>
<trans-unit id="ERR_PartialMethodReadOnlyDifference">
<source>Both partial method declarations must be readonly or neither may be readonly</source>
<target state="new">Both partial method declarations must be readonly or neither may be readonly</target>
<note />
</trans-unit>
<trans-unit id="ERR_PointerTypeInPatternMatching">
<source>Pattern-matching is not permitted for pointer types.</source>
<target state="translated">I criteri di ricerca non sono consentiti per i tipi di puntatore.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@
<target state="translated">out 変数を ref ローカルと宣言することはできません</target>
<note />
</trans-unit>
<trans-unit id="ERR_PartialMethodReadOnlyDifference">
<source>Both partial method declarations must be readonly or neither may be readonly</source>
<target state="new">Both partial method declarations must be readonly or neither may be readonly</target>
<note />
</trans-unit>
<trans-unit id="ERR_PointerTypeInPatternMatching">
<source>Pattern-matching is not permitted for pointer types.</source>
<target state="translated">ポインター型でパターン マッチングを使用することはできません。</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@
<target state="translated">출력 변수는 참조 로컬로 선언할 수 없습니다.</target>
<note />
</trans-unit>
<trans-unit id="ERR_PartialMethodReadOnlyDifference">
<source>Both partial method declarations must be readonly or neither may be readonly</source>
<target state="new">Both partial method declarations must be readonly or neither may be readonly</target>
<note />
</trans-unit>
<trans-unit id="ERR_PointerTypeInPatternMatching">
<source>Pattern-matching is not permitted for pointer types.</source>
<target state="translated">포인터 형식에 대해 패턴 일치가 허용되지 않습니다.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@
<target state="translated">Zmiennej out nie można zadeklarować jako lokalnej zmiennej ref</target>
<note />
</trans-unit>
<trans-unit id="ERR_PartialMethodReadOnlyDifference">
<source>Both partial method declarations must be readonly or neither may be readonly</source>
<target state="new">Both partial method declarations must be readonly or neither may be readonly</target>
<note />
</trans-unit>
<trans-unit id="ERR_PointerTypeInPatternMatching">
<source>Pattern-matching is not permitted for pointer types.</source>
<target state="translated">Dopasowanie wzorca jest niedozwolone dla typów wskaźnika.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@
<target state="translated">Uma variável out não pode ser declarada como uma referência local</target>
<note />
</trans-unit>
<trans-unit id="ERR_PartialMethodReadOnlyDifference">
<source>Both partial method declarations must be readonly or neither may be readonly</source>
<target state="new">Both partial method declarations must be readonly or neither may be readonly</target>
<note />
</trans-unit>
<trans-unit id="ERR_PointerTypeInPatternMatching">
<source>Pattern-matching is not permitted for pointer types.</source>
<target state="translated">A correspondência de padrões não é permitida para tipos de ponteiro.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@
<target state="translated">Выходная переменная не может быть объявлена как локальная переменная ref</target>
<note />
</trans-unit>
<trans-unit id="ERR_PartialMethodReadOnlyDifference">
<source>Both partial method declarations must be readonly or neither may be readonly</source>
<target state="new">Both partial method declarations must be readonly or neither may be readonly</target>
<note />
</trans-unit>
<trans-unit id="ERR_PointerTypeInPatternMatching">
<source>Pattern-matching is not permitted for pointer types.</source>
<target state="translated">Сопоставление шаблонов запрещено для типов указателей.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@
<target state="translated">Bir out değişkeni ref yerel değeri olarak bildirilemez</target>
<note />
</trans-unit>
<trans-unit id="ERR_PartialMethodReadOnlyDifference">
<source>Both partial method declarations must be readonly or neither may be readonly</source>
<target state="new">Both partial method declarations must be readonly or neither may be readonly</target>
<note />
</trans-unit>
<trans-unit id="ERR_PointerTypeInPatternMatching">
<source>Pattern-matching is not permitted for pointer types.</source>
<target state="translated">İşaretçi türleri için desen eşleştirmeye izin verilmez.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@
<target state="translated">out 变量无法声明为 ref 局部变量</target>
<note />
</trans-unit>
<trans-unit id="ERR_PartialMethodReadOnlyDifference">
<source>Both partial method declarations must be readonly or neither may be readonly</source>
<target state="new">Both partial method declarations must be readonly or neither may be readonly</target>
<note />
</trans-unit>
<trans-unit id="ERR_PointerTypeInPatternMatching">
<source>Pattern-matching is not permitted for pointer types.</source>
<target state="translated">指针类型不允许进行模式匹配。</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@
<target state="translated">out 變數不可宣告為 ref local</target>
<note />
</trans-unit>
<trans-unit id="ERR_PartialMethodReadOnlyDifference">
<source>Both partial method declarations must be readonly or neither may be readonly</source>
<target state="new">Both partial method declarations must be readonly or neither may be readonly</target>
<note />
</trans-unit>
<trans-unit id="ERR_PointerTypeInPatternMatching">
<source>Pattern-matching is not permitted for pointer types.</source>
<target state="translated">指標類型不允許進行模式比對。</target>
Expand Down
Loading