-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: System.Diagnostics.CodeAnalysis.UnscopedRefAttribute #72074
Comments
Tagging subscribers to this area: @dotnet/area-system-runtime-compilerservices Issue DetailsBackground and motivationThere are several cases where the C# compiler treats a
For specific instances where the See low-level-struct-improvements.md. API Proposalnamespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(
AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Parameter,
AllowMultiple = false,
Inherited = false)]
public sealed class RefEscapesAttribute : Attribute
{
}
} API UsageWhen applied to struct S1
{
private int _field;
[RefEscapes] public ref int Property => ref _field; // ok
[RefEscapes] public ref int GetField() => ref _field; // ok
} When applied to a [RefEscapes]
struct S2
{
private int _field;
public ref int Property => ref _field; // ok
public ref int GetField() => ref _field; // ok
}
[RefEscapes]
struct S3
{
private S2 _child;
public ref int Value => ref _child.Property; // ok
} When applied to a ref struct R { }
ref R ReturnRefStructRef(bool b, ref R x, [RefEscapes] ref R y)
{
if (b)
return ref x; // error: Cannot return parameter by reference
else
return ref y; // ok
} When applied to an ref int ReturnOut(bool b, out int x, [RefEscapes] out int y)
{
x = 1;
y = 2;
if (b)
return ref x; // error: Cannot return parameter by reference
else
return ref y; // ok
} Alternative DesignsWithout a well-known attribute recognized by the compiler, callers will need to use An alternative attribute name: RisksNo response
|
@cston Is this expected for .NET 8 or do we need it for .NET 7? |
Why is the attribute needed here: ref struct R { }
ref R ReturnRefStructRef(bool b, ref R x, [RefEscapes] ref R y)
{
if (b)
return ref x; // error: Cannot return parameter by reference
else
return ref y; // ok
} Wouldn't those refs all be escaping, and in order to get only the second one to be so, the first should be marked as |
@cston, is this instead of the proposed "unscoped" keyword? |
@Sergio0694, it's likely that |
@stephentoub, yes, this attribute is instead of the proposed |
@AaronRobinsonMSFT, we should add this in .NET 7 if possible. The attribute will be especially important after the compiler has been updated to treat |
Q: wouldn't that make it harder to read and understand code, if the scope of refs will now depend on how the type itself is defined rather than just what's visible at the parameter declaration? As in, I can no longer just see whether I'm taking a
Just so I understand, this is only so that we can enable this in C# 11 already by manually using the attribute (and allow people to escape fields by ref), while language support isn't there yet, but the plan is still to eventually get |
namespace System.Diagnostics.CodeAnalysis;
[AttributeUsage(AttributeTargets.Method |
AttributeTargets.Property |
AttributeTargets.Parameter,
AllowMultiple = false,
Inherited = false)]
public sealed class UnscopedRefAttribute : Attribute
{
public UnscopedRefAttribute();
} |
Background and motivation
There are several cases where the C# compiler treats a
ref
as implicitlyscoped
, where the compiler does not allow theref
to escape the method.this
forstruct
instance methodsref
parameters that refer toref struct
typesout
parametersFor specific instances where the
ref
should be allowed to escape, we should provide a well-known attribute that the compiler will recognize that indicates theref
is not scoped.See low-level-struct-improvements.md.
API Proposal
API Usage
When applied to
struct
instance methods and properties,this
ref is unscoped.When applied to a
struct
type,this
ref is unscoped for all instance methods and properties.When applied to a
ref
parameter, the ref is unscoped.When applied to an
out
parameter, the ref is unscoped.Alternative Designs
Without a well-known attribute recognized by the compiler, callers will need to use
Unsafe.AsRef()
and similar methods to return ascoped ref
.An alternative attribute name:
UnscopedAttribute
.Risks
No response
The text was updated successfully, but these errors were encountered: