From 0905a419419f8eb44bfe82be9907bd458f8235cc Mon Sep 17 00:00:00 2001 From: tlakollo Date: Mon, 22 Nov 2021 22:57:13 -0800 Subject: [PATCH 1/4] Add RequiresDynamicCodeAttribute to runtime --- .../System.Private.CoreLib.Shared.projitems | 1 + .../RequiresDynamicCodeAttribute.cs | 41 +++++++++++++++++++ .../System.Runtime/ref/System.Runtime.cs | 7 ++++ .../RequiresDynamicCodeAttributeTests.cs | 34 +++++++++++++++ 4 files changed, 83 insertions(+) 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 01fd0776225fa..dee56dc10ae53 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 @@ -252,6 +252,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..3d8bd8eee098c --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/RequiresDynamicCodeAttribute.cs @@ -0,0 +1,41 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#nullable enable + +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 7157e359ccd54..e65a12def323d 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -9084,6 +9084,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/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); + } + } +} From fb53b5c535866f16f310ea05096a2b9f102c2756 Mon Sep 17 00:00:00 2001 From: tlakollo Date: Mon, 22 Nov 2021 23:12:40 -0800 Subject: [PATCH 2/4] Delete nullable enable --- .../Diagnostics/CodeAnalysis/RequiresDynamicCodeAttribute.cs | 2 -- 1 file changed, 2 deletions(-) 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 index 3d8bd8eee098c..a067002e9f9fc 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/RequiresDynamicCodeAttribute.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/RequiresDynamicCodeAttribute.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable enable - namespace System.Diagnostics.CodeAnalysis { /// From 1e2bf390926ff4e14f073bc3ffb7c9c923886a43 Mon Sep 17 00:00:00 2001 From: tlakollo Date: Tue, 23 Nov 2021 16:23:31 -0800 Subject: [PATCH 3/4] Add test file to compilation for RequiresDynamicCode and RequiresAssemblyFiles --- .../System.Runtime/tests/System.Runtime.Tests.csproj | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 @@ + + + From 06d0fee720d31cfca4987e71411c9583bd2696a4 Mon Sep 17 00:00:00 2001 From: tlakollo Date: Tue, 23 Nov 2021 17:05:28 -0800 Subject: [PATCH 4/4] Fix RequiresAssemblyFiles tests --- .../RequiresAssemblyFilesAttributeTests.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) 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); } } }