dotnet / dotnet/roslyn

Diagnostic is unclear when passing ref readonly to ref readonly parameter using 'ref'

Open
#73,459 4 comments 0 reactions 0 assignees View on GitHub
Area-Compilers Bug Concept-Diagnostic Clarity
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

**Version Used**: fca6e1fcdcded85e69cc32e15acfb6820cd45597

**Steps to Reproduce**:

[SharpLab](https://sharplab.io/#v2:EYLgtghglgdgNAFxFANnAJiA1AHwAIBMABAMICwAUAN6VF1F4AsRAsgAwAUATgKYBmRXhHQB7GCgCeRWAmkBKIlSIBfWvSasAjN36CewsZOkxZUOWro0K9G6068BZgNxEA9K6I8uXEV1IBlAA4AZgIAThBSCBgYEVkAVwBnHiIANwguKAhgFBSAcig8oghE4r0BXyIReNl0lHiU4B4AYwgklKhTUog9A3EpdMzs3Itbdg5YeSdR1QplIA===)

```cs
class C
{
void M0(ref readonly int i) { }
void M1(ref readonly int i)
{
M0(ref i); // error CS8329: Cannot use variable 'i' as a ref or out value because it is a readonly variable
M0(in i); // ok
}
}
```

It appears that this error is [by design](https://github.com/dotnet/csharplang/blob/main/proposals/csharp-12.0/ref-readonly-parameters.md#value-kind-checks). @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 using `ref` for this parameter without issue. It was not obvious to me that using `ref` was actually requiring the argument to be a writable reference, even though the parameter will not assign to the referent.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.