-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Determine Libraries strategy for using the readonly members annotation #1718
Comments
CC. @stephentoub as I believe you made a similar pass for |
I plan on looking at |
(The ability to do this work is also pending a compiler update to the repo). |
I'd written a little Roslyn-based tool that just made structs readonly if all of their fields were readonly, and then I reviewed the changes it made to ensure they were appropriate (I didn't do the next step of having it look for fields that weren't readonly but could have been). A simple tool could similarly be written here that would look at all methods/properties on non-readonly structs and see whether they write to any of the struct's fields, pass this by ref, etc... there'd be some false positives and some false negatives, but I expect it would automate most of the work. However, we'll want to be careful in what we annotate as readonly, as once we do, that method won't be able to modify the struct (at least not without hackery), and so we'll want to only do so when we're confident it'll never want to mutate. |
I've got a PR up for the System.Numerics.Vectors project: dotnet/corefx#36663. In this case, they are all structs that expose their fields publicly (so we can't mark them as |
I think other methods will likely be a case by case basis. |
|
Would like to throw in a vote to make members of |
Edit: Also because constructs like |
Opened a PR to mark Guid as readonly. Do we need an API review session for this? |
So far we have done API review sessions for most of these changes (but they have been pretty quick in each case). It is generally beneficial just to have the input from @dotnet/fxdc that the right changes are being made. |
I've updated the issue title and description to reflect what we learned in #46675 (comment). |
We sort of have have guidance for new API:
And, I put the breakingness into the book:
The thing that we don't have a stance on is how to re-evaluate existing methods, or what level of mutation we're willing to lie about (e.g. I believe you can still do things like take a pointer to a readonly struct, at which point you have a pointer, so "readonly "is out the door). And since we never made the conscious choice with existing API we now have the "hmm, do we keep the door open here, or let it close?" problem, which is (largely) unique to existing API. |
For mutable structs,
For types like When looking at whether or not something should be marked
|
I see struct types as falling into two general categories. The first category is where the struct is an exchange type and represents some standalone datum. Tanner gave some good examples earlier: The second category is where the struct does not represent some standalone datum, but where it instead acts as a wrapper or mutator around something else. Some examples of this are enumerators ( For this second category, I don't think there's significant benefit to marking the APIs readonly. Assuming that it's not typical for application code to maintain readonly references to such types, nobody's really going to see the benefit anyway. And it ties our hands such that we'd need to guarantee that these methods remain non-mutating for all time. The tradeoff does not seem worth it. |
C# 8 has added a new feature called "readonly members". This feature allows you to indicate that an individual method on a non-readonly struct is itself "readonly" (i.e. that the method does not mutate the state of the instance).
Edit
We need to define a strategy for if/how/when/where this feature should be used across the .NET Libraries. Originally, the intent of this issue was to take a pass over the libraries and annotate methods on non-readonly structs which do not and will never mutate the state of the instance. It was noted that there are a number of these methods in the
System.Numerics
namespace which users also may try to pass around asin
. However, after such a pass was taken by @hrrrrustic in #46675, we realized that we need to be more strategic about this effort.Some of the aspects that need to be considered are:
readonly
later. For example, adding caching to areadonly
method later would be a breaking change.readonly
provides across different areas relative to that possibilityThe text was updated successfully, but these errors were encountered: