-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand on the future ref rules a bit #9415
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
base: main
Are you sure you want to change the base?
Conversation
@agocke, @AaronRobinsonMSFT PTAL |
|
||
Conforming runtimes will ensure that `TypedReference`, `RuntimeArgumentHandle` and `ArgIterator` are defined as `ref struct`. Further `TypedReference` must be viewed as having a `ref` field to a `ref struct` for any possible type (it can store any value). That combined with the above rules will ensure references to the stack do not escape beyond their lifetime. | ||
|
||
```csharp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AaronRobinsonMSFT here you go.
// Error: safe-to-escape is current method which is not returnable | ||
return tr; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to confirm the following example:
struct OS
{
public IS inner;
}
struct IS
{ }
TypedReference M4(TypedReference os)
{
// os contains a 'ref OS'
// Return a 'ref IS' via the TypedReference
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you trying to get the following to compile?
TypedReference M4(TypedReference os)
{
ref IS local = __refvalue(os, IS);
return __makeref(local);
}
That will compile. It's logically equivalent to the following:
TR M(TR tr)
{
ref int local = ref tr.field;
return new TR { field = ref local };
}
ref struct TR
{
public ref int field;
}
@jaredpar I wonder if there is some document describing your view of the C# lifetime system with goals, imaginary syntax and so on? I'm really curious how you envision such a big feature/change. (Even if it might never come). |
Co-authored-by: Aaron Robinson <[email protected]>
Which change are you referring to here: a world where we have explicit lifetimes? |
@jaredpar Yes, explicit lifetime annotations in C#. How they would work with IDisposable, would they work with reference types, pointers? What would be design goals: is it to enhance ref usage only or to enable developers with something like automatic disposal on scope exit? I think it's fun to experiment with such features and I'm curious what do you have in mind. |
@jaredpar Yes, explicit lifetime annotations in C#. How they would work with IDisposable, would they work with reference types, pointers? What would be design goals: is it to enhance ref usage only or to enable developers with something like automatic disposal on scope exit? I think it's fun to olay with such features and I'm curious what do you have in mind. |
I've never sat down and wrote a doc about this because I wanted to focus on how far we could take the existing system. Once we know the limitations of how far you can take Once I get pas this though, I will likely sit down and write a doc for where we'd go beyond |
No description provided.