diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index cf031056c51f8f..1475134b521b96 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -896,6 +896,7 @@ + diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/RequiresUnsafeAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/RequiresUnsafeAttribute.cs index 6ecf050781400a..bfa9bc8a0f0128 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/RequiresUnsafeAttribute.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/RequiresUnsafeAttribute.cs @@ -4,16 +4,17 @@ namespace System.Diagnostics.CodeAnalysis { /// - /// Indicates that the specified method requires unsafe code that may not be available - /// in all execution environments. + /// Indicates that the specified member requires the caller to be in an unsafe context. /// - /// - /// This allows tools to understand which methods are unsafe to call when targeting - /// environments that do not support unsafe code. - /// - [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Property, Inherited = false)] - [Conditional("DEBUG")] - internal sealed class RequiresUnsafeAttribute : Attribute + [AttributeUsage( + AttributeTargets.Constructor | AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Property, + Inherited = false, + AllowMultiple = false)] + public sealed class RequiresUnsafeAttribute : Attribute { + /// + /// Initializes a new instance of the class. + /// + public RequiresUnsafeAttribute() { } } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/MemorySafetyRulesAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/MemorySafetyRulesAttribute.cs new file mode 100644 index 00000000000000..54597d38373cd9 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/MemorySafetyRulesAttribute.cs @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.ComponentModel; + +namespace System.Runtime.CompilerServices +{ + /// Indicates the version of the memory safety rules used when the module was compiled. + [EditorBrowsable(EditorBrowsableState.Never)] + [AttributeUsage(AttributeTargets.Module, Inherited = false, AllowMultiple = false)] + public sealed class MemorySafetyRulesAttribute : Attribute + { + /// Initializes a new instance of the class. + /// The version of the memory safety rules used when the module was compiled. + public MemorySafetyRulesAttribute(int version) => Version = version; + + /// Gets the version of the memory safety rules used when the module was compiled. + public int Version { get; } + } +} diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index ccc217672ebad4..20eb121fddf1f3 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -9110,6 +9110,11 @@ public RequiresUnreferencedCodeAttribute(string message) { } public string Message { get { throw null; } } public string? Url { get { throw null; } set { } } } + [System.AttributeUsageAttribute(System.AttributeTargets.Constructor | System.AttributeTargets.Event | System.AttributeTargets.Method | System.AttributeTargets.Property, Inherited=false, AllowMultiple=false)] + public sealed partial class RequiresUnsafeAttribute : System.Attribute + { + public RequiresUnsafeAttribute() { } + } [System.AttributeUsageAttribute(System.AttributeTargets.Constructor, AllowMultiple=false, Inherited=false)] public sealed partial class SetsRequiredMembersAttribute : System.Attribute { @@ -14028,6 +14033,13 @@ public enum LoadHint Always = 1, Sometimes = 2, } + [System.AttributeUsageAttribute(System.AttributeTargets.Module, Inherited=false, AllowMultiple=false)] + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] + public sealed partial class MemorySafetyRulesAttribute : System.Attribute + { + public MemorySafetyRulesAttribute(int version) { } + public int Version { get { throw null; } } + } public enum MethodCodeType { IL = 0, diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Diagnostics/CodeAnalysis/RequiresUnsafeAttributeTests.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Diagnostics/CodeAnalysis/RequiresUnsafeAttributeTests.cs new file mode 100644 index 00000000000000..bf07e6e23a3d59 --- /dev/null +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Diagnostics/CodeAnalysis/RequiresUnsafeAttributeTests.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Xunit; + +namespace System.Diagnostics.CodeAnalysis.Tests; + +public class RequiresUnsafeAttributeTests +{ + [Fact] + public static void TestConstructor() + { + new RequiresUnsafeAttribute(); + } +} diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/AttributesTests.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/AttributesTests.cs index e80943edf58551..431c72b61c7ce6 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/AttributesTests.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/AttributesTests.cs @@ -235,6 +235,17 @@ public static void RefSafetyRulesAttributeTests(int version) Assert.Equal(version, attr.Version); } + [Theory] + [InlineData(-1)] + [InlineData(0)] + [InlineData(2)] + [InlineData(42)] + public static void MemorySafetyRulesAttributeTests(int version) + { + var attr = new MemorySafetyRulesAttribute(version); + Assert.Equal(version, attr.Version); + } + [Theory] [InlineData("1")] [InlineData("2")]