Skip to content

[API Proposal]: Unsafe evolution attributes #125134

@jjonescz

Description

@jjonescz

Background and motivation

Attributes needed for the unsafe evolution C# language feature (dotnet/roslyn#81207).

API Proposal

namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
    public sealed class RequiresUnsafeAttribute : Attribute { }
}

namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Module, Inherited = false, AllowMultiple = false)]
    public sealed class MemorySafetyRulesAttribute : Attribute
    {
        public MemorySafetyRulesAttribute(int version) { Version = version; }
        public int Version;
    }
}

API Usage

The MemorySafetyRulesAttribute is used by the compiler (it is disallowed to use it in source). The compiler applies it to a module when updated memory safety rules are enabled.

The RequiresUnsafeAttribute is used by users but can be also applied by the compiler implicitly for extern members.

class C
{
    [RequiresUnsafe] public void M() { }
}

Alternative Designs

Since the RequiresUnsafe attribute is to be used by users, perhaps it should live in System.Diagnostics.CodeAnalysis instead - in fact, it's already defined there internally:

Risks

No response

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions