Skip to content
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

Inaccurate error message when assigning to ref readonly variable #25676

Open
svick opened this issue Mar 23, 2018 · 2 comments
Open

Inaccurate error message when assigning to ref readonly variable #25676

svick opened this issue Mar 23, 2018 · 2 comments
Labels
Area-Compilers Bug Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings.
Milestone

Comments

@svick
Copy link
Contributor

svick commented Mar 23, 2018

Version Used: SharpLab using Roslyn master from 14 March: 644e1e0

Steps to Reproduce:

Compile the following code:

class C
{
    void M(int[] a)
    {
        ref readonly int i = ref a[0];
        i = 42;
    }
}

Expected Behavior:

Accurate error message, possibly:

error CS8331: Cannot assign to variable 'in int' because it is a readonly variable

Actual Behavior:

error CS0131: The left-hand side of an assignment must be a variable, property or indexer

This error message is not accurate, because i is a variable.

@jcouv jcouv added Area-Compilers Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. labels Mar 23, 2018
@jcouv
Copy link
Member

jcouv commented Mar 23, 2018

Tagging @VSadov

@jaredpar jaredpar added this to the Unknown milestone Aug 31, 2018
@verelpode
Copy link

verelpode commented May 1, 2019

Likewise the problem also occurs when accessing struct members via a ref readonly local variable:

struct STestStruct
{
    public int A;
    public int B;
}

class MyTestRefReturns
{
    STestStruct fTestStructField;

    ref readonly STestStruct GetFieldRef()
    {
        return ref fTestStructField;
    }

    void Test()
    {
        GetFieldRef().A = 123;  // Error CS8332 (makes sense).
        ref readonly STestStruct r = ref GetFieldRef();
        r.A = 123;  // Error CS0131 (does not make sense).
    }
}
Error CS8332 Cannot assign to a member of method 'MyTestRefReturns.GetFieldRef()' because it is a readonly variable
Error CS0131 The left-hand side of an assignment must be a variable, property or indexer

The incorrect error CS0131 occurs when compiling with VS 2019 16.0.3.

@gafter gafter added the Bug label Sep 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Bug Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings.
Projects
None yet
Development

No branches or pull requests

5 participants