dotnet/roslyn

Confusing error when assigning result of ref readonly-returning method to a ref local

オープン

#28,653 opened on 2018/07/18

 (0 件のコメント) (0 件のリアクション) (0 人の担当者)C# (4,257 件のフォーク)batch import
Area-CompilersBugConcept-Diagnostic Clarityhelp wanted

Repository metrics

Stars
 (20,414 個のスター)
PR merge metrics
 (平均マージ 6d 17h) (30d で 256 merged PRs)

説明

Version Used: 2.8.2; master from 14 July 2018 (c4189de08b943f87d9f03d45ef01e615800e359d)

Steps to Reproduce:

Try to assign the result of a ref readonly-returning method to a non-readonly ref local:

class C
{
    void M()
    {
        ref int x = ref Bar();
    }
    
    ref readonly int Bar() => throw null;
}

Actual Behavior:

error CS8329: Cannot use method 'C.Bar()' as a ref or out value because it is a readonly variable

I think this is not a good error message because:

  1. It's not correct: method is not a variable.
  2. It does not explain well what the error is and how to fix it.

コントリビューターガイド