-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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]: Add System.Runtime.CompilerServices.RefSafetyRulesAttribute #76032
Comments
Tagging subscribers to this area: @dotnet/area-system-runtime-compilerservices Issue DetailsBackground and motivationThe C# compiler emits a The API Proposalnamespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
public sealed class RefSafetyRulesAttribute : Attribute
{
public RefSafetyRulesAttribute(int version) { Version = version; }
public int Version;
}
} API UsageThe Alternative DesignsNo response RisksNo response
|
@cston This looks like a .NET 7 need, correct? |
@AaronRobinsonMSFT, this can wait until .NET 8. |
There are other attributes the compiler will emit as internal into an assembly and that we haven't exposed from corelib, e.g. |
The other ones require some level of opt in. For instance you aren't going to see any of the The ref safety rules one though we include on almost all assemblies. There isn't as much of a trigger to opt into ref rules. Originally we conditioned the rule on a signature having a Also if there was a better trigger for when to emit this, be willing to listen to that option 😄 |
Is the /cc @marek-safar |
No, it is not. It can be removed during trimming of a final application. It must be kept on assemblies that will be used during build or else things could be very very very ... bad. |
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
public sealed class RefSafetyRulesAttribute : Attribute
{
public RefSafetyRulesAttribute(int version) { Version = version; }
public int Version { get; }
}
} |
@AaronRobinsonMSFT |
It's just the attribute. |
Right. This issue is just about adding it to the product and updating the appropriate ref assembly. We typically add a test too, for completeness and verifying it works with reflection. |
In this case, we'll also want to validate that the compiler recognizes this attribute and stops emitting into all of our assemblies its own internal copy. |
Sure, but I don't think there is any cost with declaring them in the BCL, because nullable annotations are enabled by default anyway, and System.Runtime/System.Private.CoreLib would almost definitely already have these declared already (just not exposed for external consumption) since they support nullable annotations. There is a reward, which is not having to declare them in every single other BCL library, and every other library that wants to use nullable annotations (which I remind you, is the default). Also, since nullable annotations is the default for any new project, it's definitely not opt-in, but instead opt-out. |
Background and motivation
The C# compiler emits a
[module: RefSafetyRules(11)]
attribute to modules compiled with-langversion:11
or above, or compiled with .NET 7 or above, to indicate that the module was compiled with C#11 ref safety rules.The
RefSafetyRulesAttribute
type definition should be added to the BCL to avoid having the compiler emit the attribute definition to each module.See RefSafetyRulesAttribute.
API Proposal
API Usage
The
RefSafetyRulesAttribute
type is for compiler use only - it is not permitted in source.Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: