-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Diagnostic is unclear when passing ref readonly to ref readonly parameter using 'ref' #73459
Comments
Note that you would get the same error with class C
{
void M0(in int i) { }
void M1(in int i)
{
M0(ref i); // error CS8329: Cannot use variable 'i' as a ref or out value because it is a readonly variable
}
}
|
I am still interested to know why the design requires writability for a ref readonly argument passed using The reason I found this design so confusing is that it is inconsistent with all the existing places that I can create int i = 1;
ref readonly int ri = ref i; // ok
ri = ref i; // reassignment also ok
M(ref i); // ok?
M(ref ri); // error!
ref readonly int M(ref readonly int i) => ref i; // ok So, when you get started using this feature, and If we really have a strong reason for requiring writability here and not in the other places, then OK. Let's resolve by simply issuing a diagnostic which is consistent with all the other places that a wrong argument ref kind is being used: |
Thanks Jan! |
Version Used: fca6e1f
Steps to Reproduce:
SharpLab
It appears that this error is by design. @jjonescz are there notes handy for why the design is this way?
Given the above I think the diagnostic message should simply be adjusted here to indicate that the user should use
in
at the call side instead.I personally found it surprising that
ref
was not permitted here, because other kinds of arguments can be passed usingref
for this parameter without issue. It was not obvious to me that usingref
was actually requiring the argument to be a writable reference, even though the parameter will not assign to the referent.The text was updated successfully, but these errors were encountered: