From 90773ac32a6ac44daa00f10eda80d51fc04290fb Mon Sep 17 00:00:00 2001 From: Tlakaelel Axayakatl Ceja Date: Wed, 24 Nov 2021 03:53:01 -0800 Subject: [PATCH] Add RequiresDynamicCodeAttribute to runtime (#61956) * Add RequiresDynamicCodeAttribute to runtime * Delete nullable enable * Add test file to compilation for RequiresDynamicCode and RequiresAssemblyFiles * Fix RequiresAssemblyFiles tests --- .../System.Private.CoreLib.Shared.projitems | 1 + .../RequiresDynamicCodeAttribute.cs | 39 +++++++++++++++++++ .../System.Runtime/ref/System.Runtime.cs | 7 ++++ .../tests/System.Runtime.Tests.csproj | 4 +- .../RequiresAssemblyFilesAttributeTests.cs | 12 ++++-- .../RequiresDynamicCodeAttributeTests.cs | 34 ++++++++++++++++ 6 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/RequiresDynamicCodeAttribute.cs create mode 100644 src/libraries/System.Runtime/tests/System/Diagnostics/CodeAnalysis/RequiresDynamicCodeAttributeTests.cs 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 9723fc8954385..8a5bf95a09d59 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 @@ -254,6 +254,7 @@ + diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/RequiresDynamicCodeAttribute.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/RequiresDynamicCodeAttribute.cs new file mode 100644 index 0000000000000..a067002e9f9fc --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/RequiresDynamicCodeAttribute.cs @@ -0,0 +1,39 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Diagnostics.CodeAnalysis +{ + /// + /// Indicates that the specified method requires the ability to generate new code at runtime, + /// for example through . + /// + /// + /// This allows tools to understand which methods are unsafe to call when compiling ahead of time. + /// + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, Inherited = false)] + public sealed class RequiresDynamicCodeAttribute : Attribute + { + /// + /// Initializes a new instance of the class + /// with the specified message. + /// + /// + /// A message that contains information about the usage of dynamic code. + /// + public RequiresDynamicCodeAttribute(string message) + { + Message = message; + } + + /// + /// Gets a message that contains information about the usage of dynamic code. + /// + public string Message { get; } + + /// + /// Gets or sets an optional URL that contains more information about the method, + /// why it requires dynamic code, and what options a consumer has to deal with it. + /// + public string? Url { get; set; } + } +} diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 04b63cbf2a807..a03a42d21a6f9 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -9155,6 +9155,13 @@ public RequiresAssemblyFilesAttribute(string message) { } public string? Message { get { throw null; } } public string? Url { get { throw null; } set { } } } + [System.AttributeUsageAttribute(System.AttributeTargets.Constructor | System.AttributeTargets.Method, Inherited = false)] + public sealed partial class RequiresDynamicCodeAttribute : System.Attribute + { + public RequiresDynamicCodeAttribute(string message) { } + public string Message { get { throw null; } } + public string? Url { get { throw null; } set { } } + } [System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Method, Inherited=false)] public sealed partial class RequiresUnreferencedCodeAttribute : System.Attribute { diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj b/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj index 913da87c5b284..734e649022814 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj @@ -58,7 +58,6 @@ - @@ -155,6 +154,9 @@ + + + diff --git a/src/libraries/System.Runtime/tests/System/Diagnostics/CodeAnalysis/RequiresAssemblyFilesAttributeTests.cs b/src/libraries/System.Runtime/tests/System/Diagnostics/CodeAnalysis/RequiresAssemblyFilesAttributeTests.cs index 974de11e89bb9..6dd40bf5b3de4 100644 --- a/src/libraries/System.Runtime/tests/System/Diagnostics/CodeAnalysis/RequiresAssemblyFilesAttributeTests.cs +++ b/src/libraries/System.Runtime/tests/System/Diagnostics/CodeAnalysis/RequiresAssemblyFilesAttributeTests.cs @@ -34,7 +34,10 @@ public void TestSetMessage(string message) [InlineData(null)] public void TestSetUrl(string url) { - var attr = new RequiresAssemblyFilesAttribute(Url = url); + var attr = new RequiresAssemblyFilesAttribute() + { + Url = url + }; Assert.Null(attr.Message); Assert.Equal(url, attr.Url); @@ -52,10 +55,13 @@ public void TestSetUrl(string url) [InlineData(null, null)] public void TestSetMessageAndUrl(string message, string url) { - var attr = new RequiresAssemblyFilesAttribute(message, Url = url); + var attr = new RequiresAssemblyFilesAttribute(message) + { + Url = url + }; Assert.Equal(message, attr.Message); - Assert.Equal(ur, attr.Url); + Assert.Equal(url, attr.Url); } } } diff --git a/src/libraries/System.Runtime/tests/System/Diagnostics/CodeAnalysis/RequiresDynamicCodeAttributeTests.cs b/src/libraries/System.Runtime/tests/System/Diagnostics/CodeAnalysis/RequiresDynamicCodeAttributeTests.cs new file mode 100644 index 0000000000000..a9cbc6f6942ec --- /dev/null +++ b/src/libraries/System.Runtime/tests/System/Diagnostics/CodeAnalysis/RequiresDynamicCodeAttributeTests.cs @@ -0,0 +1,34 @@ +// 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 RequiresDynamicCodeAttributeTests + { + [Fact] + public void TestConstructor() + { + var attr = new RequiresDynamicCodeAttribute("User Message"); + + Assert.Equal("User Message", attr.Message); + Assert.Null(attr.Url); + } + + [Theory] + [InlineData("https://dot.net")] + [InlineData("")] + [InlineData(null)] + public void TestSetUrl(string url) + { + var attr = new RequiresDynamicCodeAttribute("User Message") + { + Url = url + }; + + Assert.Equal("User Message", attr.Message); + Assert.Equal(url, attr.Url); + } + } +}