From 7d8eb8f104a2d24402f114712e27ef91d1935936 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Thu, 7 Jul 2022 18:44:11 +0200 Subject: [PATCH 1/2] Add/implement ObsoletedInOSPlatformAttribute. As described here: https://github.com/dotnet/runtime/issues/68557#issuecomment-1177934266 @buyaa-n will implement the changes required in the analyzer (target is RC1). Fixes https://github.com/dotnet/runtime/issues/68557. --- .../ILLink/ILLink.LinkAttributes.Shared.xml | 3 ++ .../Runtime/Versioning/PlatformAttributes.cs | 41 +++++++++++++++++++ .../System.Runtime/ref/System.Runtime.cs | 10 +++++ .../Versioning/OSPlatformAttributeTests.cs | 36 ++++++++++++++++ 4 files changed, 90 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.LinkAttributes.Shared.xml b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.LinkAttributes.Shared.xml index 5d965dfd023a82..cb9cabdc3ea646 100644 --- a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.LinkAttributes.Shared.xml +++ b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.LinkAttributes.Shared.xml @@ -210,6 +210,9 @@ + + + diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/PlatformAttributes.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/PlatformAttributes.cs index 4a0a3753c00b61..ef235aae22261e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/PlatformAttributes.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/Versioning/PlatformAttributes.cs @@ -102,6 +102,47 @@ sealed class UnsupportedOSPlatformAttribute : OSPlatformAttribute public UnsupportedOSPlatformAttribute(string platformName) : base(platformName) { } + public UnsupportedOSPlatformAttribute(string platformName, string? message) : base(platformName) + { + Message = message; + } + public string? Message { get; } + } + + /// + /// Marks APIs that were obsoleted in a given operating system version. + /// + /// + /// Primarily used by OS bindings to indicate APIs that should not be used anymore. + /// + [AttributeUsage(AttributeTargets.Assembly | + AttributeTargets.Class | + AttributeTargets.Constructor | + AttributeTargets.Enum | + AttributeTargets.Event | + AttributeTargets.Field | + AttributeTargets.Interface | + AttributeTargets.Method | + AttributeTargets.Module | + AttributeTargets.Property | + AttributeTargets.Struct, + AllowMultiple = true, Inherited = false)] +#if SYSTEM_PRIVATE_CORELIB + public +#else + internal +#endif + sealed class ObsoletedInOSPlatformAttribute : OSPlatformAttribute + { + public ObsoletedInOSPlatformAttribute(string platformName) : base(platformName) + { + } + public ObsoletedInOSPlatformAttribute(string platformName, string? message) : base(platformName) + { + Message = message; + } + public string? Message { get; } + 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 07abb8a23199f1..b30665c2d7f981 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -13437,6 +13437,14 @@ public FrameworkName(string identifier, System.Version version, string? profile) public static bool operator !=(System.Runtime.Versioning.FrameworkName? left, System.Runtime.Versioning.FrameworkName? right) { throw null; } public override string ToString() { throw null; } } + [System.AttributeUsageAttribute(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Enum | System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Module | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple=true, Inherited=false)] + public sealed partial class ObsoletedInOSPlatformAttribute : System.Runtime.Versioning.OSPlatformAttribute + { + public ObsoletedInOSPlatformAttribute(string platformName) : base(platformName) { } + public ObsoletedInOSPlatformAttribute(string platformName, string? message) : base(platformName) { } + public string? Message { get { throw null; } } + public string? Url { get { throw null; } set {} } + } public abstract partial class OSPlatformAttribute : System.Attribute { private protected OSPlatformAttribute(string platformName) { } @@ -13503,6 +13511,8 @@ public TargetPlatformAttribute(string platformName) : base(platformName) { } public sealed partial class UnsupportedOSPlatformAttribute : System.Runtime.Versioning.OSPlatformAttribute { public UnsupportedOSPlatformAttribute(string platformName) : base(platformName) { } + public UnsupportedOSPlatformAttribute(string platformName, string? message) : base(platformName) { } + public string? Message { get { throw null; } } } [System.AttributeUsageAttribute(System.AttributeTargets.Field | System.AttributeTargets.Method | System.AttributeTargets.Property, AllowMultiple=true, Inherited=false)] public sealed partial class UnsupportedOSPlatformGuardAttribute : System.Runtime.Versioning.OSPlatformAttribute diff --git a/src/libraries/System.Runtime/tests/System/Runtime/Versioning/OSPlatformAttributeTests.cs b/src/libraries/System.Runtime/tests/System/Runtime/Versioning/OSPlatformAttributeTests.cs index 332a98e40dadf2..8e682ec9e640c8 100644 --- a/src/libraries/System.Runtime/tests/System/Runtime/Versioning/OSPlatformAttributeTests.cs +++ b/src/libraries/System.Runtime/tests/System/Runtime/Versioning/OSPlatformAttributeTests.cs @@ -28,6 +28,19 @@ public void TestUnsupportedOSPlatformAttribute(string platformName) var tpa = new UnsupportedOSPlatformAttribute(platformName); Assert.Equal(platformName, tpa.PlatformName); + Assert.Null(tpa.Message); + } + + [Theory] + [InlineData("Windows8.0", "Message in a bottle")] + [InlineData("Android4.1", "Message on a pigeon")] + [InlineData("", null)] + public void TestUnsupportedOSPlatformAttributeWithMessage(string platformName, string? message) + { + var tpa = new UnsupportedOSPlatformAttribute(platformName, message); + + Assert.Equal(platformName, tpa.PlatformName); + Assert.Equal(message, tpa.Message); } [Theory] @@ -41,6 +54,29 @@ public void TestSupportedOSPlatformAttribute(string platformName) Assert.Equal(platformName, tpa.PlatformName); } + [Theory] + [InlineData("Windows8.0")] + [InlineData("Android4.1")] + [InlineData("")] + public void TestObsoletedOSPlatformAttribute(string platformName) + { + var tpa = new ObsoletedOSPlatformAttribute(platformName); + + Assert.Equal(platformName, tpa.PlatformName); + } + + [Theory] + [InlineData("Windows8.0", "Message in a bottle")] + [InlineData("Android4.1", "Message on a pigeon")] + [InlineData("", null)] + public void TestObsoletedOSPlatformAttributeWithMessage(string platformName, string? message) + { + var tpa = new ObsoletedOSPlatformAttribute(platformName, message); + + Assert.Equal(platformName, tpa.PlatformName); + Assert.Equal(message, tpa.Message); + } + [Theory] [InlineData("Windows8.0")] [InlineData("Android4.1")] From 054a8562028aba8e5f35591e07a6a9ecb1ba4966 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Fri, 8 Jul 2022 20:12:29 +0200 Subject: [PATCH 2/2] Fix typename --- .../System/Runtime/Versioning/OSPlatformAttributeTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Runtime/tests/System/Runtime/Versioning/OSPlatformAttributeTests.cs b/src/libraries/System.Runtime/tests/System/Runtime/Versioning/OSPlatformAttributeTests.cs index 8e682ec9e640c8..619bdc7c8f1134 100644 --- a/src/libraries/System.Runtime/tests/System/Runtime/Versioning/OSPlatformAttributeTests.cs +++ b/src/libraries/System.Runtime/tests/System/Runtime/Versioning/OSPlatformAttributeTests.cs @@ -58,9 +58,9 @@ public void TestSupportedOSPlatformAttribute(string platformName) [InlineData("Windows8.0")] [InlineData("Android4.1")] [InlineData("")] - public void TestObsoletedOSPlatformAttribute(string platformName) + public void TestObsoletedInOSPlatformAttribute(string platformName) { - var tpa = new ObsoletedOSPlatformAttribute(platformName); + var tpa = new ObsoletedInOSPlatformAttribute(platformName); Assert.Equal(platformName, tpa.PlatformName); } @@ -69,9 +69,9 @@ public void TestObsoletedOSPlatformAttribute(string platformName) [InlineData("Windows8.0", "Message in a bottle")] [InlineData("Android4.1", "Message on a pigeon")] [InlineData("", null)] - public void TestObsoletedOSPlatformAttributeWithMessage(string platformName, string? message) + public void TestObsoletedInOSPlatformAttributeWithMessage(string platformName, string? message) { - var tpa = new ObsoletedOSPlatformAttribute(platformName, message); + var tpa = new ObsoletedInOSPlatformAttribute(platformName, message); Assert.Equal(platformName, tpa.PlatformName); Assert.Equal(message, tpa.Message);