From 3d4806e5bfc512128ed710e287ec7df141ce702b Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Sun, 28 Jul 2019 20:14:40 -0400 Subject: [PATCH] Fix nullability annotations for static (#25914) The compiler is now analyzing statics. Get compliant. --- .../shared/System/Diagnostics/Tracing/EventSource.cs | 2 +- src/System.Private.CoreLib/shared/System/Environment.Win32.cs | 2 +- .../shared/System/Globalization/CultureInfo.Windows.cs | 2 +- .../shared/System/Globalization/DateTimeFormatInfo.cs | 10 +++++----- .../shared/System/Globalization/DateTimeFormatInfoScanner.cs | 4 ++-- .../shared/System/Globalization/GregorianCalendar.cs | 2 +- .../shared/System/Globalization/JapaneseCalendar.cs | 2 +- .../shared/System/Globalization/NumberFormatInfo.cs | 2 +- .../shared/System/Globalization/TaiwanCalendar.cs | 2 +- .../shared/System/IO/FileStreamCompletionSource.Win32.cs | 2 +- src/System.Private.CoreLib/shared/System/IO/Stream.cs | 2 +- src/System.Private.CoreLib/shared/System/Random.cs | 4 ++-- .../shared/System/Runtime/Serialization/SerializationInfo.cs | 2 +- .../shared/System/Threading/ThreadPool.cs | 6 +++--- .../src/System/Diagnostics/Eventing/RuntimeEventSource.cs | 2 +- .../src/System/Reflection/Emit/DynamicMethod.cs | 2 +- .../InteropServices/WindowsRuntime/CLRIPropertyValueImpl.cs | 2 +- .../InteropServices/WindowsRuntime/WindowsRuntimeMetadata.cs | 2 +- src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs | 2 +- src/System.Private.CoreLib/src/System/String.CoreCLR.cs | 2 ++ .../src/System/Threading/Tasks/AsyncCausalityTracer.cs | 3 +-- 21 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/EventSource.cs b/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/EventSource.cs index 93687a6..b03011c 100644 --- a/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/EventSource.cs +++ b/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/EventSource.cs @@ -3956,7 +3956,7 @@ namespace System.Diagnostics.Tracing // Rather than depending on initialized statics, use lazy initialization to ensure that the statics are initialized exactly when they are needed. // used for generating GUID from eventsource name - private static byte[] namespaceBytes; + private static byte[]? namespaceBytes; #endregion } diff --git a/src/System.Private.CoreLib/shared/System/Environment.Win32.cs b/src/System.Private.CoreLib/shared/System/Environment.Win32.cs index 78d49b1..37213f0 100644 --- a/src/System.Private.CoreLib/shared/System/Environment.Win32.cs +++ b/src/System.Private.CoreLib/shared/System/Environment.Win32.cs @@ -404,7 +404,7 @@ namespace System #if FEATURE_APPX private static class WinRTFolderPaths { - private static Func s_winRTFolderPathsGetFolderPath; + private static Func? s_winRTFolderPathsGetFolderPath; public static string GetFolderPath(SpecialFolder folder, SpecialFolderOption option) { diff --git a/src/System.Private.CoreLib/shared/System/Globalization/CultureInfo.Windows.cs b/src/System.Private.CoreLib/shared/System/Globalization/CultureInfo.Windows.cs index d746659..775ee4e 100644 --- a/src/System.Private.CoreLib/shared/System/Globalization/CultureInfo.Windows.cs +++ b/src/System.Private.CoreLib/shared/System/Globalization/CultureInfo.Windows.cs @@ -13,7 +13,7 @@ namespace System.Globalization { #if FEATURE_APPX // When running under AppX, we use this to get some information about the language list - private static volatile WindowsRuntimeResourceManagerBase s_WindowsRuntimeResourceManager; + private static volatile WindowsRuntimeResourceManagerBase? s_WindowsRuntimeResourceManager; [ThreadStatic] private static bool ts_IsDoingAppXCultureInfoLookup; diff --git a/src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormatInfo.cs b/src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormatInfo.cs index b8167d9..db64442 100644 --- a/src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormatInfo.cs +++ b/src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormatInfo.cs @@ -51,7 +51,7 @@ namespace System.Globalization { // cache for the invariant culture. // invariantInfo is constant irrespective of your current culture. - private static volatile DateTimeFormatInfo s_invariantInfo; + private static volatile DateTimeFormatInfo? s_invariantInfo; // an index which points to a record in Culture Data Table. private CultureData _cultureData; @@ -2135,8 +2135,8 @@ namespace System.Globalization internal const string JapaneseLangName = "ja"; internal const string EnglishLangName = "en"; - private static volatile DateTimeFormatInfo s_jajpDTFI; - private static volatile DateTimeFormatInfo s_zhtwDTFI; + private static volatile DateTimeFormatInfo? s_jajpDTFI; + private static volatile DateTimeFormatInfo? s_zhtwDTFI; /// /// Create a Japanese DTFI which uses JapaneseCalendar. This is used to parse @@ -2146,7 +2146,7 @@ namespace System.Globalization /// internal static DateTimeFormatInfo GetJapaneseCalendarDTFI() { - DateTimeFormatInfo temp = s_jajpDTFI; + DateTimeFormatInfo? temp = s_jajpDTFI; if (temp == null) { temp = new CultureInfo("ja-JP", false).DateTimeFormat; @@ -2164,7 +2164,7 @@ namespace System.Globalization /// internal static DateTimeFormatInfo GetTaiwanCalendarDTFI() { - DateTimeFormatInfo temp = s_zhtwDTFI; + DateTimeFormatInfo? temp = s_zhtwDTFI; if (temp == null) { temp = new CultureInfo("zh-TW", false).DateTimeFormat; diff --git a/src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormatInfoScanner.cs b/src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormatInfoScanner.cs index 7b5ae00..accc5b9 100644 --- a/src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormatInfoScanner.cs +++ b/src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormatInfoScanner.cs @@ -115,7 +115,7 @@ namespace System.Globalization // The collection fo date words & postfix. internal List m_dateWords = new List(); // Hashtable for the known words. - private static volatile Dictionary s_knownWords; + private static volatile Dictionary? s_knownWords; static Dictionary KnownWords { @@ -147,7 +147,7 @@ namespace System.Globalization s_knownWords = temp; } - return (s_knownWords); + return s_knownWords; } } diff --git a/src/System.Private.CoreLib/shared/System/Globalization/GregorianCalendar.cs b/src/System.Private.CoreLib/shared/System/Globalization/GregorianCalendar.cs index ebc5a54..2be5a22 100644 --- a/src/System.Private.CoreLib/shared/System/Globalization/GregorianCalendar.cs +++ b/src/System.Private.CoreLib/shared/System/Globalization/GregorianCalendar.cs @@ -27,7 +27,7 @@ namespace System.Globalization private static readonly int[] DaysToMonth366 = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }; - private static volatile Calendar s_defaultInstance; + private static volatile Calendar? s_defaultInstance; public override DateTime MinSupportedDateTime => DateTime.MinValue; diff --git a/src/System.Private.CoreLib/shared/System/Globalization/JapaneseCalendar.cs b/src/System.Private.CoreLib/shared/System/Globalization/JapaneseCalendar.cs index b0428db..fcc2302 100644 --- a/src/System.Private.CoreLib/shared/System/Globalization/JapaneseCalendar.cs +++ b/src/System.Private.CoreLib/shared/System/Globalization/JapaneseCalendar.cs @@ -81,7 +81,7 @@ namespace System.Globalization }); } - internal static volatile Calendar s_defaultInstance; + internal static volatile Calendar? s_defaultInstance; internal GregorianCalendarHelper _helper; internal static Calendar GetDefaultInstance() diff --git a/src/System.Private.CoreLib/shared/System/Globalization/NumberFormatInfo.cs b/src/System.Private.CoreLib/shared/System/Globalization/NumberFormatInfo.cs index 3078c8e..7af0381 100644 --- a/src/System.Private.CoreLib/shared/System/Globalization/NumberFormatInfo.cs +++ b/src/System.Private.CoreLib/shared/System/Globalization/NumberFormatInfo.cs @@ -35,7 +35,7 @@ namespace System.Globalization /// public sealed class NumberFormatInfo : IFormatProvider, ICloneable { - private static volatile NumberFormatInfo s_invariantInfo; + private static volatile NumberFormatInfo? s_invariantInfo; internal int[] _numberGroupSizes = new int[] { 3 }; internal int[] _currencyGroupSizes = new int[] { 3 }; diff --git a/src/System.Private.CoreLib/shared/System/Globalization/TaiwanCalendar.cs b/src/System.Private.CoreLib/shared/System/Globalization/TaiwanCalendar.cs index e190f66..a0f64c9 100644 --- a/src/System.Private.CoreLib/shared/System/Globalization/TaiwanCalendar.cs +++ b/src/System.Private.CoreLib/shared/System/Globalization/TaiwanCalendar.cs @@ -30,7 +30,7 @@ namespace System.Globalization new EraInfo( 1, 1912, 1, 1, 1911, 1, GregorianCalendar.MaxYear - 1911) // era #, start year/month/day, yearOffset, minEraYear }; - private static volatile Calendar s_defaultInstance; + private static volatile Calendar? s_defaultInstance; private readonly GregorianCalendarHelper _helper; diff --git a/src/System.Private.CoreLib/shared/System/IO/FileStreamCompletionSource.Win32.cs b/src/System.Private.CoreLib/shared/System/IO/FileStreamCompletionSource.Win32.cs index 837946e..246538b 100644 --- a/src/System.Private.CoreLib/shared/System/IO/FileStreamCompletionSource.Win32.cs +++ b/src/System.Private.CoreLib/shared/System/IO/FileStreamCompletionSource.Win32.cs @@ -24,7 +24,7 @@ namespace System.IO private const long CompletedCallback = (long)8 << 32; private const ulong ResultMask = ((ulong)uint.MaxValue) << 32; - private static Action s_cancelCallback; + private static Action? s_cancelCallback; private readonly FileStream _stream; private readonly int _numBufferedBytes; diff --git a/src/System.Private.CoreLib/shared/System/IO/Stream.cs b/src/System.Private.CoreLib/shared/System/IO/Stream.cs index 4b3b0d3..85899ef 100644 --- a/src/System.Private.CoreLib/shared/System/IO/Stream.cs +++ b/src/System.Private.CoreLib/shared/System/IO/Stream.cs @@ -644,7 +644,7 @@ namespace System.IO callback(rwc); } - private static ContextCallback s_invokeAsyncCallback; + private static ContextCallback? s_invokeAsyncCallback; void ITaskCompletionAction.Invoke(Task completingTask) { diff --git a/src/System.Private.CoreLib/shared/System/Random.cs b/src/System.Private.CoreLib/shared/System/Random.cs index ed79732..709fc0c 100644 --- a/src/System.Private.CoreLib/shared/System/Random.cs +++ b/src/System.Private.CoreLib/shared/System/Random.cs @@ -116,7 +116,7 @@ namespace System } [ThreadStatic] - private static Random t_threadRandom; + private static Random? t_threadRandom; private static readonly Random s_globalRandom = new Random(GenerateGlobalSeed()); /*=====================================GenerateSeed===================================== @@ -125,7 +125,7 @@ namespace System ========================================================================================*/ private static int GenerateSeed() { - Random rnd = t_threadRandom; + Random? rnd = t_threadRandom; if (rnd == null) { int seed; diff --git a/src/System.Private.CoreLib/shared/System/Runtime/Serialization/SerializationInfo.cs b/src/System.Private.CoreLib/shared/System/Runtime/Serialization/SerializationInfo.cs index a11fa94..57f1cf0 100644 --- a/src/System.Private.CoreLib/shared/System/Runtime/Serialization/SerializationInfo.cs +++ b/src/System.Private.CoreLib/shared/System/Runtime/Serialization/SerializationInfo.cs @@ -32,7 +32,7 @@ namespace System.Runtime.Serialization // On AoT, assume private members are reflection blocked, so there's no further protection required // for the thread's DeserializationTracker [ThreadStatic] - private static DeserializationTracker t_deserializationTracker; + private static DeserializationTracker? t_deserializationTracker; private static DeserializationTracker GetThreadDeserializationTracker() { diff --git a/src/System.Private.CoreLib/shared/System/Threading/ThreadPool.cs b/src/System.Private.CoreLib/shared/System/Threading/ThreadPool.cs index dca22ab..21d8e8f 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/ThreadPool.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/ThreadPool.cs @@ -494,7 +494,7 @@ namespace System.Threading internal bool LocalFindAndPop(object callback) { - ThreadPoolWorkQueueThreadLocals tl = ThreadPoolWorkQueueThreadLocals.threadLocals; + ThreadPoolWorkQueueThreadLocals? tl = ThreadPoolWorkQueueThreadLocals.threadLocals; return tl != null && tl.workStealingQueue.LocalFindAndPop(callback); } @@ -728,7 +728,7 @@ namespace System.Threading internal sealed class ThreadPoolWorkQueueThreadLocals { [ThreadStatic] - public static ThreadPoolWorkQueueThreadLocals threadLocals; + public static ThreadPoolWorkQueueThreadLocals? threadLocals; public readonly ThreadPoolWorkQueue workQueue; public readonly ThreadPoolWorkQueue.WorkStealingQueue workStealingQueue; @@ -1231,7 +1231,7 @@ namespace System.Threading internal static IEnumerable GetLocallyQueuedWorkItems() { - ThreadPoolWorkQueue.WorkStealingQueue wsq = ThreadPoolWorkQueueThreadLocals.threadLocals.workStealingQueue; + ThreadPoolWorkQueue.WorkStealingQueue? wsq = ThreadPoolWorkQueueThreadLocals.threadLocals?.workStealingQueue; if (wsq != null && wsq.m_array != null) { object?[] items = wsq.m_array; diff --git a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs index f8498a6..5e6f57d 100644 --- a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs +++ b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/RuntimeEventSource.cs @@ -12,7 +12,7 @@ namespace System.Diagnostics.Tracing [EventSource(Guid="49592C0F-5A05-516D-AA4B-A64E02026C89", Name = "System.Runtime")] internal sealed class RuntimeEventSource : EventSource { - private static RuntimeEventSource s_RuntimeEventSource; + private static RuntimeEventSource? s_RuntimeEventSource; private PollingCounter? _gcHeapSizeCounter; private IncrementingPollingCounter? _gen0GCCounter; private IncrementingPollingCounter? _gen1GCCounter; diff --git a/src/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.cs b/src/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.cs index 0991b09..11db43e 100644 --- a/src/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.cs +++ b/src/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.cs @@ -41,7 +41,7 @@ namespace System.Reflection.Emit // We capture the creation context so that we can do the checks against the same context, // irrespective of when the method gets compiled. Note that the DynamicMethod does not know when // it is ready for use since there is not API which indictates that IL generation has completed. - private static volatile InternalModuleBuilder s_anonymouslyHostedDynamicMethodsModule; + private static volatile InternalModuleBuilder? s_anonymouslyHostedDynamicMethodsModule; private static readonly object s_anonymouslyHostedDynamicMethodsModuleLock = new object(); // diff --git a/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/CLRIPropertyValueImpl.cs b/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/CLRIPropertyValueImpl.cs index 752f7bc..b4b1b59 100644 --- a/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/CLRIPropertyValueImpl.cs +++ b/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/CLRIPropertyValueImpl.cs @@ -15,7 +15,7 @@ namespace System.Runtime.InteropServices.WindowsRuntime private object _data; // Numeric scalar types which participate in coersion - private static volatile Tuple[] s_numericScalarTypes; + private static volatile Tuple[]? s_numericScalarTypes; internal CLRIPropertyValueImpl(PropertyType type, object data) { diff --git a/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/WindowsRuntimeMetadata.cs b/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/WindowsRuntimeMetadata.cs index ce1d0ef..273b270 100644 --- a/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/WindowsRuntimeMetadata.cs +++ b/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/WindowsRuntimeMetadata.cs @@ -8,7 +8,7 @@ namespace System.Runtime.InteropServices.WindowsRuntime { internal static class WindowsRuntimeMetadata { - private static EventHandler DesignerNamespaceResolve; + private static EventHandler? DesignerNamespaceResolve; internal static string[]? OnDesignerNamespaceResolve(string namespaceName) { diff --git a/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs b/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs index 6e7527c..02518db 100644 --- a/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs +++ b/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs @@ -4342,7 +4342,7 @@ namespace System } } - private static OleAutBinder s_ForwardCallBinder; + private static OleAutBinder? s_ForwardCallBinder; private OleAutBinder ForwardCallBinder { get => s_ForwardCallBinder ?? (s_ForwardCallBinder = new OleAutBinder()); diff --git a/src/System.Private.CoreLib/src/System/String.CoreCLR.cs b/src/System.Private.CoreLib/src/System/String.CoreCLR.cs index b942ea5..79eeca6 100644 --- a/src/System.Private.CoreLib/src/System/String.CoreCLR.cs +++ b/src/System.Private.CoreLib/src/System/String.CoreCLR.cs @@ -31,8 +31,10 @@ namespace System // We need to call the String constructor so that the compiler doesn't mark this as a literal. // Marking this as a literal would mean that it doesn't show up as a field which we can access // from native. +#pragma warning disable CS8618 // compiler sees this non-nullable static string as uninitialized [Intrinsic] public static readonly string Empty; +#pragma warning restore CS8618 // Gets the character at a specified position. // diff --git a/src/System.Private.CoreLib/src/System/Threading/Tasks/AsyncCausalityTracer.cs b/src/System.Private.CoreLib/src/System/Threading/Tasks/AsyncCausalityTracer.cs index 3332f44..614b61e 100644 --- a/src/System.Private.CoreLib/src/System/Threading/Tasks/AsyncCausalityTracer.cs +++ b/src/System.Private.CoreLib/src/System/Threading/Tasks/AsyncCausalityTracer.cs @@ -33,8 +33,7 @@ namespace System.Threading.Tasks //Indicates this information comes from the BCL Library private const WFD.CausalitySource s_CausalitySource = WFD.CausalitySource.Library; - //Lazy initialize the actual factory - private static WFD.IAsyncCausalityTracerStatics s_TracerFactory; + private static WFD.IAsyncCausalityTracerStatics s_TracerFactory = null!; // The loggers that this Tracer knows about. [Flags] -- 2.7.4