You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Calling non-readonly struct methods from readonly methods
Calling a non-readonly method on a reference type field from a readonly method
Variants of these which use different combinations of properties and methods
Assignment to this
Reference to instance fields in readonly member
Assignment is an error
Invocation w/ instance field as receiver causes copy when type is non-readonly struct
Invocation of non-readonly method or getter on this inside a readonly member causes copy and a warning. Non-readonly setter access on this or a value typed field is an error.
Unary increment/decrement operators on fields are errors
Compound assignment on fields is an error
Round-tripping through metadata
Error to take ref to this in readonly member
Property assignment:
Disallow on non-readonly instance properties on this or value-typed fields
Allow on static or readonly properties, or properties on reference-typed fields
- [ ] Extracting a method from a readonly method should produce a readonly method#34647 - [ ] Suggestion to make a member readonly when it is called on a ref readonly receiver#34648 - [ ] WRN_ImplicitCopyInReadOnlyMember should have a suggestion to make the callee readonly if possible#34649 - [ ] WRN_ImplicitCopyInReadOnlyMember should have a suggestion to make an explicit copy of the receiver
Major sections of work (ideally PRs will not cover more than one of these)
Parsing of readonly method and property syntax (mostly just testing)
IsReadOnly API on MethodSymbol
In the case of properties, is prop.GetMethod.IsReadOnly acceptable design, or is it confusing to have prop.IsReadOnly potentially return a different value than prop.GetMethod.IsReadOnly?
Flow analysis to prevent mutation of this in readonly member implementation
maybe the analysis used for in params or readonly struct can just be reused?
Error on invalid usage of readonly
Any method or property without a this parameter (i.e. statics)
Any member inside a non-struct declaration (i.e. class, interface)
Emit tests to demonstrate that expected metadata (IsReadOnly, modopt, modreq) and optimizations (reduced value copying) are present in IL
- [ ] Should some synthesized methods be marked as readonly? (discussion)#36587
Should we disallow readonly members on readonly structs, since redundant? Or maybe we create an IDE fixer?
Resolution: Should be allowed, based on the principle that changing struct to readonly struct on a type where all members are marked readonly shouldn't result in a compile error.
The text was updated successfully, but these errors were encountered:
@RikkiGibson
It looks like there is only one unchecked bullet at this point (Should some synthesized methods be marked as readonly?). Can we get a resolution on that and close this issue? Alternatively, move this remaining question to a dedicated issue and close OP.
Proposal: dotnet/csharplang#1710
This issue is a place to collect test ideas and track their validation. Use the compiler test plan as a tool for brainstorming.
Methods
readonly
onnew ToString()
Properties
int P readonly => 42;
(should error)int P { readonly get; set; }
allowedint P { readonly get; }
not allowed ->readonly int P { get; }
Misc
- [ ] SymbolDisplay on methods and property/indexer/event accessorsReadonly members SymbolDisplay/MetadataAsSource #34650- [ ] IsDeclaredReadonly on ISymbol or IPropertySymbol/IMethodSymbolPublic API design for readonly properties and events in C# #34213Semantic
this
this
inside areadonly
member causes copy and a warning. Non-readonly setter access onthis
or a value typed field is an error.ref
tothis
inreadonly
memberthis
or value-typed fieldshttps://sharplab.io/#v2:EYLgZgpghgLgrgJwgZwLRKgEwPYDsA2AnqgLYQnAQLIA0MIAlvjZiANQA+AAgAwAEXAIwBuALAAoDDgKE+yGAjgBjGHwDKEgN4S+uvg1yqACn018A5hFVmuAdj49hAXzlXTfF0517vurgBY+AFkACgN1OQBKX1MYvT4YAAsGZAA6EwBePn8AJjFxePi0zOy8mK8Cn3EKiXlFFXUcrRiDY3dLawF7RxdkNzNPGJipPCIBQNDwtRy5HOjK3W0FwoB6FaSU9L4s3PzCvTXkHK2dsuX4gH1thOTivf2bzZM2LLP4iviJJyA=
IDE
- [ ] Extracting a method from a#34647readonly
method should produce areadonly
method- [ ] Suggestion to make a member#34648readonly
when it is called on aref readonly
receiver- [ ] WRN_ImplicitCopyInReadOnlyMember should have a suggestion to make the callee#34649readonly
if possible- [ ] WRN_ImplicitCopyInReadOnlyMember should have a suggestion to make an explicit copy of the receiver- [ ] Metadata as sourceReadonly members SymbolDisplay/MetadataAsSource #34650Major sections of work (ideally PRs will not cover more than one of these)
readonly
method and property syntax (mostly just testing)prop.GetMethod.IsReadOnly
acceptable design, or is it confusing to haveprop.IsReadOnly
potentially return a different value thanprop.GetMethod.IsReadOnly
?this
inreadonly
member implementationin
params orreadonly struct
can just be reused?readonly
this
parameter (i.e. statics)- [ ] Should some synthesized methods be marked as readonly? (discussion)#36587struct
toreadonly struct
on a type where all members are marked readonly shouldn't result in a compile error.The text was updated successfully, but these errors were encountered: