Skip to content

Ref-assigning a ref parameter to a ref field with primary constructor #67371

@ufcpp

Description

@ufcpp

Version Used:

Visual Studio 17.6 Preview 2
C# Tools 4.6.0-2.23151.17+1314d090671dc1a1500c5303c4b5ae9150f40d98

Steps to Reproduce:

ref struct Ok
{
    public ref int Ref;

    public Ok(ref int x)
    {
        Ref = ref x; // OK
    }
}

ref struct Error(ref int x)
{
    public ref int Ref = ref x; // CS9077. Isn't this a bug?
}

ref struct ForReference([UnscopedRef] ref int x)
{
    public ref int Ref = ref x; // OK

    public void M1(ref int x)
    {
        Ref = ref x; // CS9077. This is correct.
    }

    public void M2([UnscopedRef] ref int x)
    {
        Ref = ref x; // OK
    }
}

Expected Behavior:

No error on public ref int Ref = ref x;, for consistency with Ok struct.

Actual Behavior:

CS9077 error.

See also #68610

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions