-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
S.R.CS.Unsafe and read-only references #23504
Comments
@VSadov Is changing |
Proposal 1 looks fine if it is not breaking change. Not sure what the rules are. |
Changing ref --> readonly ref is a source-breaker to callers. They no longer need/can use "ref" at the call site. Generally "ref readonly" works well when we intend to read the value. Volatile.Read would be a good example. When dealing with references, especially when reference types are allowed, it may be confusing. The 3rd thing will allow most what you want. Another concern is that "ref readonly" allows implicit conversions at the call site. Including those that do not preserve identity. |
Ok, so this takes 1st thing out. @ektrah Could you please update the proposal to have the 3rd thing at the top to prep it for the API review? |
@jkotas - done. |
@ektrah Thanks! |
Shouldn't there also be a method to directly convert a |
You can say that about other Unsafe APIs that take |
Looks good as proposed. |
FYI: The API review discussion was recorded - see https://youtu.be/m4BUM3nJZRw?t=5923 (5 min duration) |
I would grab this, but I have a few questions first. I guess the implementation is the same as for the existing ... except for the method signature:
|
For netcoreapp builds, it should look like this:
For non-netcoreapp builds, it should look like this:
And there needs to be a local definition of IsReadOnlyAttribute included in the file for non-netcoreapp builds. The best way to figure this out is to compile a small test program that has
|
@jkotas, I thought they additionally had a CC @VSadov |
@tannergooding Yes, the modreq should be there and the compiler will require that it be there when loading [IsReadOnly]. We have a bug on that for 15.5 (the current implementation accepts such metadata, but it won't after the bug is fixed). |
…espectively (master) (#23916) * Update BuildTools, CoreClr to preview4-03913-01, preview5-27612-73, respectively * Fix nullable build errors Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
…espectively (master) (#23916) * Update BuildTools, CoreClr to preview4-03913-01, preview5-27612-73, respectively * Fix nullable build errors Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
Most (if not all) APIs today are defined to accept references with
ref
rather thanref readonly
. This includes all methods of theUnsafe
class. This proposal is to provide a method to turn aref readonly T
into aref T
, so that read-only references can be passed to methods that useref
but are known to be only reading them.public static partial class Unsafe { ... + public static ref T AsRef<T>(ref readonly T source) }
Original proposals (click to expand)
A few proposals for APIs related to the System.Runtime.CompilerServices.Unsafe class and read-only references.
Proposal 1: Change
ref T
toref readonly T
where possible:Proposal 2:
Unsafe
but everyref T
is aref readonly T
:Proposal 3: A method to turn a
ref readonly T
into aref T
:public static partial class Unsafe { ... + public static ref T AsRef<T>(ref readonly T source) }
Potential problems:
Passing a
ref readonly T
argument does not look different from passing aT
argument, so the following code is legal but very surprising:The text was updated successfully, but these errors were encountered: