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

Support ref field assignment in object initializers #62120

Closed
cston opened this issue Jun 24, 2022 · 2 comments · Fixed by #62584
Closed

Support ref field assignment in object initializers #62120

cston opened this issue Jun 24, 2022 · 2 comments · Fixed by #62584

Comments

@cston
Copy link
Member

cston commented Jun 24, 2022

ref struct R<T>
{
    public ref T F;
}

int i = 0;
var r = new R<int> { F = ref i };

See LDM notes.

Relates to test plan #59194

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Language Design untriaged Issues and PRs which have not yet been triaged by a lead labels Jun 24, 2022
@jcouv jcouv removed the untriaged Issues and PRs which have not yet been triaged by a lead label Jun 24, 2022
@jcouv jcouv added this to the C# 11.0 milestone Jun 24, 2022
@jcouv jcouv added the Bug label Jun 24, 2022
@jaredpar jaredpar modified the milestones: C# 11.0, 17.4 Jun 24, 2022
@cston cston assigned jcouv and unassigned cston Jul 8, 2022
@ufcpp
Copy link
Contributor

ufcpp commented Aug 11, 2022

int x = 1;

var a = new A();
a.X = ref x; // error. (a is not scoped.)

var b = new A { X = ref x }; // no error?

ref struct A { public ref int X; }

@cston
Copy link
Member Author

cston commented Aug 11, 2022

@ufcpp, the escape scope of the local is set by the initializer unless the local is explicitly declared scoped.

For a, the initializer value new A() can be returned from the current method, so a is unscoped. For b, the initializer cannot escape the current method, so b is implicitly scoped.

The error for a.X = ref x; can be avoided by declaring a as scoped var a = new A();.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants