Use Platform-Guard Assertion Annotations for some known cases (#52689)
authorBuyaa Namnan <bunamnan@microsoft.com>
Mon, 17 May 2021 16:51:22 +0000 (09:51 -0700)
committerGitHub <noreply@github.com>
Mon, 17 May 2021 16:51:22 +0000 (09:51 -0700)
* Use platform guard attributes

eng/Versions.props
src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs
src/libraries/System.Net.Http/ref/System.Net.Http.cs
src/libraries/System.Net.Http/src/System/Net/Http/BrowserHttpHandler/SocketsHttpHandler.cs
src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs
src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionSettings.cs
src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/SocketsHttpHandler.cs
src/libraries/System.Net.Http/tests/TrimmingTests/HttpClientTest.cs
src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/ThreadPoolTaskScheduler.cs
src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs
src/mono/System.Private.CoreLib/src/System/Threading/Thread.Browser.Mono.cs

index 95eb353..d3437f7 100644 (file)
@@ -47,7 +47,7 @@
     <MicrosoftCodeAnalysisVersion>3.8.0</MicrosoftCodeAnalysisVersion>
   </PropertyGroup>
   <PropertyGroup>
-    <MicrosoftCodeAnalysisNetAnalyzersVersion>6.0.0-preview5.21219.2</MicrosoftCodeAnalysisNetAnalyzersVersion>
+    <MicrosoftCodeAnalysisNetAnalyzersVersion>6.0.0-preview5.21262.4</MicrosoftCodeAnalysisNetAnalyzersVersion>
     <MicrosoftCodeAnalysisCSharpCodeStyleVersion>3.10.0-2.final</MicrosoftCodeAnalysisCSharpCodeStyleVersion>
     <MicrosoftCodeAnalysisCSharpVersion>3.10.0-2.final</MicrosoftCodeAnalysisCSharpVersion>
     <!-- Arcade dependencies -->
index a7ac0f7..9f4c45a 100644 (file)
@@ -47,8 +47,8 @@ namespace Microsoft.Extensions.Logging.Console
             _optionsReloadToken = _options.OnChange(ReloadLoggerOptions);
 
             _messageQueue = new ConsoleLoggerProcessor();
-            // TODO update when https://github.com/dotnet/runtime/issues/44922 implemented
-            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || DoesWindowsConsoleSupportAnsi())
+
+            if (DoesConsoleSupportAnsi())
             {
                 _messageQueue.Console = new AnsiLogConsole();
                 _messageQueue.ErrorConsole = new AnsiLogConsole(stdErr: true);
@@ -60,8 +60,14 @@ namespace Microsoft.Extensions.Logging.Console
             }
         }
 
-        private static bool DoesWindowsConsoleSupportAnsi()
+        [UnsupportedOSPlatformGuard("windows")]
+        private static bool DoesConsoleSupportAnsi()
         {
+            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+            {
+                return true;
+            }
+
             // for Windows, check the console mode
             var stdOutHandle = Interop.Kernel32.GetStdHandle(Interop.Kernel32.STD_OUTPUT_HANDLE);
             if (!Interop.Kernel32.GetConsoleMode(stdOutHandle, out int consoleMode))
index c05f0e0..9fa9bdc 100644 (file)
@@ -352,6 +352,7 @@ namespace System.Net.Http
     public sealed partial class SocketsHttpHandler : System.Net.Http.HttpMessageHandler
     {
         public SocketsHttpHandler() { }
+        [System.Runtime.Versioning.UnsupportedOSPlatformGuardAttribute("browser")]
         public static bool IsSupported { get { throw null; } }
         public bool AllowAutoRedirect { get { throw null; } set { } }
         public System.Net.DecompressionMethods AutomaticDecompression { get { throw null; } set { } }
index 0fb0ca5..f85f0a3 100644 (file)
@@ -16,6 +16,7 @@ namespace System.Net.Http
     [UnsupportedOSPlatform("browser")]
     public sealed class SocketsHttpHandler : HttpMessageHandler
     {
+        [UnsupportedOSPlatformGuard("browser")]
         public static bool IsSupported => false;
 
         public bool UseCookies
index 16272cb..1a3b68c 100644 (file)
@@ -76,6 +76,9 @@ namespace System.Net.Http
         private byte[]? _http2AltSvcOriginUri;
         internal readonly byte[]? _http2EncodedAuthorityHostHeader;
 
+        [SupportedOSPlatformGuard("linux")]
+        [SupportedOSPlatformGuard("macOS")]
+        [SupportedOSPlatformGuard("Windows")]
         private readonly bool _http3Enabled;
         private Http3Connection? _http3Connection;
         private SemaphoreSlim? _http3ConnectionCreateLock;
@@ -122,8 +125,8 @@ namespace System.Net.Http
             }
 
             _http2Enabled = _poolManager.Settings._maxHttpVersion >= HttpVersion.Version20;
-            // TODO: Replace with Platform-Guard Assertion Annotations once https://github.com/dotnet/runtime/issues/44922 is finished
-            if ((OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS())
+
+            if (IsHttp3Supported())
             {
                 _http3Enabled = _poolManager.Settings._maxHttpVersion >= HttpVersion.Version30 && (_poolManager.Settings._quicImplementationProvider ?? QuicImplementationProviders.Default).IsSupported;
             }
@@ -261,8 +264,7 @@ namespace System.Net.Http
                     _http3EncodedAuthorityHostHeader = QPackEncoder.EncodeLiteralHeaderFieldWithStaticNameReferenceToArray(H3StaticTable.Authority, hostHeader);
                 }
 
-                // TODO: Replace with Platform-Guard Assertion Annotations once https://github.com/dotnet/runtime/issues/44922 is finished
-                if ((OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS())
+                if (IsHttp3Supported())
                 {
                     if (_http3Enabled)
                     {
@@ -281,14 +283,17 @@ namespace System.Net.Http
             if (NetEventSource.Log.IsEnabled()) Trace($"{this}");
         }
 
+        [SupportedOSPlatformGuard("linux")]
+        [SupportedOSPlatformGuard("macOS")]
+        [SupportedOSPlatformGuard("Windows")]
+        internal static bool IsHttp3Supported() => (OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS();
         private static readonly List<SslApplicationProtocol> s_http3ApplicationProtocols = CreateHttp3ApplicationProtocols();
         private static readonly List<SslApplicationProtocol> s_http2ApplicationProtocols = new List<SslApplicationProtocol>() { SslApplicationProtocol.Http2, SslApplicationProtocol.Http11 };
         private static readonly List<SslApplicationProtocol> s_http2OnlyApplicationProtocols = new List<SslApplicationProtocol>() { SslApplicationProtocol.Http2 };
 
         private static List<SslApplicationProtocol> CreateHttp3ApplicationProtocols()
         {
-            // TODO: Replace with Platform-Guard Assertion Annotations once https://github.com/dotnet/runtime/issues/44922 is finished
-            if ((OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS())
+            if (IsHttp3Supported())
             {
                 // TODO: Once the HTTP/3 versions are part of SslApplicationProtocol, see https://github.com/dotnet/runtime/issues/1293, move this back to field initialization.
                 return new List<SslApplicationProtocol>() { Http3Connection.Http3ApplicationProtocol31, Http3Connection.Http3ApplicationProtocol30, Http3Connection.Http3ApplicationProtocol29 };
@@ -866,8 +871,7 @@ namespace System.Net.Http
         {
             HttpResponseMessage? response;
 
-            // TODO: Replace with Platform-Guard Assertion Annotations once https://github.com/dotnet/runtime/issues/44922 is finished
-            if ((OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS())
+            if (IsHttp3Supported())
             {
                 response = await TrySendUsingHttp3Async(request, async, doRequestAuth, cancellationToken).ConfigureAwait(false);
                 if (response is not null)
index cf81a5d..1ccbc3d 100644 (file)
@@ -122,9 +122,8 @@ namespace System.Net.Http
                 _plaintextStreamFilter = _plaintextStreamFilter
             };
 
-            // TODO: Replace with Platform-Guard Assertion Annotations once https://github.com/dotnet/runtime/issues/44922 is finished
             // TODO: Remove if/when QuicImplementationProvider is removed from System.Net.Quic.
-            if ((OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS())
+            if (HttpConnectionPool.IsHttp3Supported())
             {
                 settings._quicImplementationProvider = _quicImplementationProvider;
             }
index 8e0b508..31a9e6a 100644 (file)
@@ -42,6 +42,7 @@ namespace System.Net.Http
         /// <summary>
         /// Gets a value that indicates whether the handler is supported on the current platform.
         /// </summary>
+        [UnsupportedOSPlatformGuard("browser")]
         public static bool IsSupported => true;
 
         public bool UseCookies
index c05cac7..7ded3b5 100644 (file)
@@ -4,10 +4,16 @@
 using System;
 using System.IO;
 using System.Net.Http;
+using System.Runtime.Versioning;
 using System.Threading.Tasks;
 
 class Program
 {
+    [SupportedOSPlatformGuard("linux")]
+    [SupportedOSPlatformGuard("macOS")]
+    [SupportedOSPlatformGuard("Windows")]
+    private static bool IsHttp3Supported => (OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS();
+
     static async Task<int> Main(string[] args)
     {
         using var client = new HttpClient();
@@ -18,8 +24,7 @@ class Program
         const string quicDll = "System.Net.Quic.dll";
         var quicDllExists = File.Exists(Path.Combine(AppContext.BaseDirectory, quicDll));
 
-        // TODO: Replace with Platform-Guard Assertion Annotations once https://github.com/dotnet/runtime/issues/44922 is finished
-        if ((OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS())
+        if (IsHttp3Supported)
         {
             Console.WriteLine($"Expected {quicDll} is {(quicDllExists ? "present - OK" : "missing - BAD")}.");
             return quicDllExists ? 100 : -1;
@@ -30,4 +35,4 @@ class Program
             return quicDllExists ? -1 : 100;
         }
     }
-}
\ No newline at end of file
+}
index 692e39b..e1469c5 100644 (file)
@@ -45,13 +45,11 @@ namespace System.Threading.Tasks
             if (Thread.IsThreadStartSupported && (options & TaskCreationOptions.LongRunning) != 0)
             {
                 // Run LongRunning tasks on their own dedicated thread.
-#pragma warning disable CA1416 // TODO: https://github.com/dotnet/runtime/issues/44922
                 new Thread(s_longRunningThreadWork)
                 {
                     IsBackground = true,
                     Name = ".NET Long Running Task"
                 }.UnsafeStart(task);
-#pragma warning restore CA1416
             }
             else
             {
index aa6e8c6..d6b20c4 100644 (file)
@@ -164,7 +164,8 @@ namespace System.Threading
         }
 
 #if !TARGET_BROWSER
-        internal const bool IsThreadStartSupported = true;
+        [UnsupportedOSPlatformGuard("browser")]
+        internal static bool IsThreadStartSupported => true;
 
         /// <summary>Causes the operating system to change the state of the current instance to <see cref="ThreadState.Running"/>, and optionally supplies an object containing data to be used by the method the thread executes.</summary>
         /// <param name="parameter">An object that contains data to be used by the method the thread executes.</param>
index a456c5d..435a155 100644 (file)
@@ -7,7 +7,8 @@ namespace System.Threading
 {
     public partial class Thread
     {
-        internal const bool IsThreadStartSupported = false;
+        [UnsupportedOSPlatformGuard("browser")]
+        internal static bool IsThreadStartSupported => false;
 
         [UnsupportedOSPlatform("browser")]
         public void Start() => throw new PlatformNotSupportedException();