Skip to content

Escape scope analysis won't work with primary constructors #68610

@controlflow

Description

@controlflow

Version Used:

main branch

Steps to Reproduce:

The following struct compiles fine:

public ref struct WithRefField
{
  public readonly ref readonly int X;

  public WithRefField(ref int x)
  {
    X = ref x;
  }
}

But it won't compiles in a primary constructor form:

public ref struct WithRefField(ref int x)
{
  public readonly ref readonly int X = ref x;
}

Error CS9077 : Cannot return a parameter by reference 'x' through a ref parameter; it can only be returned in a return statement

Expected Behavior:

Both forms compile fine

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions