Nullable: Contract.cs and more interop (#24193)
authorStephen Toub <stoub@microsoft.com>
Thu, 25 Apr 2019 14:06:02 +0000 (10:06 -0400)
committerGitHub <noreply@github.com>
Thu, 25 Apr 2019 14:06:02 +0000 (10:06 -0400)
* Nullable: Contract.cs and more interop

* Address PR feedback

30 files changed:
src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.ActivityControl.cs
src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.EVENT_INFO_CLASS.cs
src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.EtwEnableCallback.cs
src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.EventActivityIdControl.cs
src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.EventRegister.cs
src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.EventSetInformation.cs
src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.EventTraceGuidsEx.cs
src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.EventUnregister.cs
src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.EventWriteString.cs
src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.EventWriteTransfer.cs
src/System.Private.CoreLib/shared/System/DateTime.Win32.cs
src/System.Private.CoreLib/shared/System/DateTime.WinRT.cs
src/System.Private.CoreLib/shared/System/Diagnostics/Contracts/Contracts.cs
src/System.Private.CoreLib/shared/System/GCMemoryInfo.cs
src/System.Private.CoreLib/shared/System/Runtime/InteropServices/ArrayWithOffset.cs
src/System.Private.CoreLib/shared/System/Runtime/InteropServices/BStrWrapper.cs
src/System.Private.CoreLib/shared/System/Runtime/InteropServices/ComEventsHelpers.NoCom.cs
src/System.Private.CoreLib/shared/System/Runtime/InteropServices/CriticalHandle.cs
src/System.Private.CoreLib/shared/System/Runtime/InteropServices/CurrencyWrapper.cs
src/System.Private.CoreLib/shared/System/Runtime/InteropServices/DispatchWrapper.cs
src/System.Private.CoreLib/shared/System/Runtime/InteropServices/ErrorWrapper.cs
src/System.Private.CoreLib/shared/System/Runtime/InteropServices/HandleRef.cs
src/System.Private.CoreLib/shared/System/Runtime/InteropServices/ICustomAdapter.cs
src/System.Private.CoreLib/shared/System/Runtime/InteropServices/ICustomFactory.cs
src/System.Private.CoreLib/shared/System/Runtime/InteropServices/ICustomQueryInterface.cs
src/System.Private.CoreLib/shared/System/Runtime/InteropServices/SafeBuffer.cs
src/System.Private.CoreLib/shared/System/Runtime/InteropServices/UnknownWrapper.cs
src/System.Private.CoreLib/shared/System/Runtime/InteropServices/VariantWrapper.cs
src/System.Private.CoreLib/shared/System/WinRTFolderPaths.cs
src/System.Private.CoreLib/src/System/DateTime.Windows.CoreCLR.cs

index 34df748..670ac61 100644 (file)
@@ -2,9 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
-using System;
-using System.Runtime.InteropServices;
-
+#nullable enable
 internal partial class Interop
 {
     internal partial class Advapi32
index a122002..0b007d1 100644 (file)
@@ -2,9 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
-using System;
-using System.Runtime.InteropServices;
-
+#nullable enable
 internal partial class Interop
 {
     internal partial class Advapi32
index 6bb1575..560b3ba 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+#nullable enable
 using System;
 using System.Runtime.InteropServices;
 
index 886ff37..f041822 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+#nullable enable
 using System;
 using System.Runtime.InteropServices;
 
index f5d245e..436d8b7 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+#nullable enable
 using System;
 using System.Runtime.InteropServices;
 
index 381cb66..919a868 100644 (file)
@@ -2,7 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
-using System;
+#nullable enable
 using System.Runtime.InteropServices;
 
 internal partial class Interop
index c5f6f3b..6576110 100644 (file)
@@ -2,7 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
-using System;
+#nullable enable
 using System.Runtime.InteropServices;
 
 internal partial class Interop
index f387b3a..79f9e3a 100644 (file)
@@ -2,7 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
-using System;
+#nullable enable
 using System.Runtime.InteropServices;
 
 internal partial class Interop
index a00a2f3..6e4fdae 100644 (file)
@@ -2,7 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
-using System;
+#nullable enable
 using System.Runtime.InteropServices;
 
 internal partial class Interop
index 2d3f45e..2821fff 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+#nullable enable
 using System;
 using System.Runtime.InteropServices;
 #if ES_BUILD_STANDALONE
index d742c89..940d092 100644 (file)
@@ -2,9 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
-using System.Runtime.CompilerServices;
-using System.Runtime.InteropServices;
-
+#nullable enable
 namespace System
 {
     public readonly partial struct DateTime
index 30a9a61..c084435 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+#nullable enable
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 
index b9e4958..26cdc7f 100644 (file)
@@ -17,6 +17,7 @@
 ===========================================================*/
 #define DEBUG // The behavior of this contract library should be consistent regardless of build type.
 
+#nullable enable
 using System.Collections.Generic;
 using System.Reflection;
 
@@ -180,7 +181,7 @@ namespace System.Diagnostics.Contracts
         private string _category;
         private string _setting;
         private bool _enabled;
-        private string _value;
+        private string? _value;
 
         public ContractOptionAttribute(string category, string setting, bool enabled)
         {
@@ -211,7 +212,7 @@ namespace System.Diagnostics.Contracts
             get { return _enabled; }
         }
 
-        public string Value
+        public string? Value
         {
             get { return _value; }
         }
@@ -264,7 +265,7 @@ namespace System.Diagnostics.Contracts
         [Pure]
         [Conditional("DEBUG")]
         [Conditional("CONTRACTS_FULL")]
-        public static void Assume(bool condition, string userMessage)
+        public static void Assume(bool condition, string? userMessage)
         {
             if (!condition)
             {
@@ -297,7 +298,7 @@ namespace System.Diagnostics.Contracts
         [Pure]
         [Conditional("DEBUG")]
         [Conditional("CONTRACTS_FULL")]
-        public static void Assert(bool condition, string userMessage)
+        public static void Assert(bool condition, string? userMessage)
         {
             if (!condition)
                 ReportFailure(ContractFailureKind.Assert, userMessage, null, null);
@@ -335,7 +336,7 @@ namespace System.Diagnostics.Contracts
         /// </remarks>
         [Pure]
         [Conditional("CONTRACTS_FULL")]
-        public static void Requires(bool condition, string userMessage)
+        public static void Requires(bool condition, string? userMessage)
         {
             AssertMustUseRewriter(ContractFailureKind.Precondition, "Requires");
         }
@@ -366,7 +367,7 @@ namespace System.Diagnostics.Contracts
         /// Use this form when you want to throw a particular exception.
         /// </remarks>
         [Pure]
-        public static void Requires<TException>(bool condition, string userMessage) where TException : Exception
+        public static void Requires<TException>(bool condition, string? userMessage) where TException : Exception
         {
             AssertMustUseRewriter(ContractFailureKind.Precondition, "Requires<TException>");
         }
@@ -403,7 +404,7 @@ namespace System.Diagnostics.Contracts
         /// </remarks>
         [Pure]
         [Conditional("CONTRACTS_FULL")]
-        public static void Ensures(bool condition, string userMessage)
+        public static void Ensures(bool condition, string? userMessage)
         {
             AssertMustUseRewriter(ContractFailureKind.Postcondition, "Ensures");
         }
@@ -438,7 +439,7 @@ namespace System.Diagnostics.Contracts
         /// </remarks>
         [Pure]
         [Conditional("CONTRACTS_FULL")]
-        public static void EnsuresOnThrow<TException>(bool condition, string userMessage) where TException : Exception
+        public static void EnsuresOnThrow<TException>(bool condition, string? userMessage) where TException : Exception
         {
             AssertMustUseRewriter(ContractFailureKind.PostconditionOnException, "EnsuresOnThrow");
         }
@@ -454,7 +455,7 @@ namespace System.Diagnostics.Contracts
         /// This method can only be used within the argument to the <seealso cref="Ensures(bool)"/> contract.
         /// </remarks>
         [Pure]
-        public static T Result<T>() { return default; }
+        public static T Result<T>() { return default!; }
 
         /// <summary>
         /// Represents the final (output) value of an out parameter when returning from a method.
@@ -466,7 +467,7 @@ namespace System.Diagnostics.Contracts
         /// This method can only be used within the argument to the <seealso cref="Ensures(bool)"/> contract.
         /// </remarks>
         [Pure]
-        public static T ValueAtReturn<T>(out T value) { value = default; return value; }
+        public static T ValueAtReturn<T>(out T value) { value = default!; return value; }
 
         /// <summary>
         /// Represents the value of <paramref name="value"/> as it was at the start of the method or property.
@@ -478,7 +479,7 @@ namespace System.Diagnostics.Contracts
         /// This method can only be used within the argument to the <seealso cref="Ensures(bool)"/> contract.
         /// </remarks>
         [Pure]
-        public static T OldValue<T>(T value) { return default; }
+        public static T OldValue<T>(T value) { return default!; }
 
         #endregion Old, Result, and Out Parameters
 
@@ -514,7 +515,7 @@ namespace System.Diagnostics.Contracts
         /// </remarks>
         [Pure]
         [Conditional("CONTRACTS_FULL")]
-        public static void Invariant(bool condition, string userMessage)
+        public static void Invariant(bool condition, string? userMessage)
         {
             AssertMustUseRewriter(ContractFailureKind.Invariant, "Invariant");
         }
@@ -651,10 +652,10 @@ namespace System.Diagnostics.Contracts
             // find the first non-mscorlib assembly.
             Assembly thisAssembly = typeof(Contract).Assembly;  // In case we refactor mscorlib, use Contract class instead of Object.
             StackTrace stack = new StackTrace();
-            Assembly probablyNotRewritten = null;
+            Assembly? probablyNotRewritten = null;
             for (int i = 0; i < stack.FrameCount; i++)
             {
-                Assembly caller = stack.GetFrame(i).GetMethod()?.DeclaringType.Assembly;
+                Assembly? caller = stack.GetFrame(i)!.GetMethod()?.DeclaringType.Assembly;
                 if (caller != null && caller != thisAssembly)
                 {
                     probablyNotRewritten = caller;
@@ -664,7 +665,7 @@ namespace System.Diagnostics.Contracts
 
             if (probablyNotRewritten == null)
                 probablyNotRewritten = thisAssembly;
-            string simpleName = probablyNotRewritten.GetName().Name;
+            string? simpleName = probablyNotRewritten.GetName().Name;
             System.Runtime.CompilerServices.ContractHelper.TriggerFailure(kind, SR.Format(SR.MustUseCCRewrite, contractKind, simpleName), null, null, null);
         }
 
@@ -679,7 +680,7 @@ namespace System.Diagnostics.Contracts
         /// System.Runtime.CompilerServices.ContractHelper.TriggerFailure.
         /// </summary>
         [System.Diagnostics.DebuggerNonUserCode]
-        private static void ReportFailure(ContractFailureKind failureKind, string userMessage, string conditionText, Exception innerException)
+        private static void ReportFailure(ContractFailureKind failureKind, string? userMessage, string? conditionText, Exception? innerException)
         {
             if (failureKind < ContractFailureKind.Precondition || failureKind > ContractFailureKind.Assume)
                 throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, failureKind), nameof(failureKind));
index 72c2aca..ea1cd3c 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+#nullable enable
 namespace System
 {
     public readonly struct GCMemoryInfo
index 5341143..7e3e9a9 100644 (file)
@@ -2,8 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
-using System.Runtime.CompilerServices;
-
+#nullable enable
 #if BIT64
 using nuint = System.UInt64;
 #else
@@ -14,14 +13,14 @@ namespace System.Runtime.InteropServices
 {
     public struct ArrayWithOffset
     {
-        private object m_array;
+        private object? m_array;
         private int m_offset;
         private int m_count;
 
         // From MAX_SIZE_FOR_INTEROP in mlinfo.h
         private const int MaxSizeForInterop = 0x7ffffff0;
 
-        public ArrayWithOffset(object array, int offset)
+        public ArrayWithOffset(object? array, int offset)
         {
             int totalSize = 0;
             if (array != null)
@@ -50,13 +49,13 @@ namespace System.Runtime.InteropServices
             m_count = totalSize - offset;
         }
 
-        public object GetArray() => m_array;
+        public object? GetArray() => m_array;
 
         public int GetOffset() => m_offset;
 
         public override int GetHashCode() => m_count + m_offset;
 
-        public override bool Equals(object obj)
+        public override bool Equals(object? obj)
         {
             return obj is ArrayWithOffset && Equals((ArrayWithOffset)obj);
         }
index f6eee34..02e6710 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+#nullable enable
 namespace System.Runtime.InteropServices
 {
     // Wrapper that is converted to a variant with VT_BSTR.
index 7b29d6f..27ed5da 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+#nullable enable
 using System.Runtime.InteropServices.ComTypes;
 
 namespace System.Runtime.InteropServices
index 95d8260..7ea8a23 100644 (file)
@@ -2,24 +2,25 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+#nullable enable
 namespace System.Runtime.InteropServices
 {
     // Wrapper that is converted to a variant with VT_CURRENCY.
     public sealed class CurrencyWrapper
     {
-        public CurrencyWrapper(Decimal obj)
+        public CurrencyWrapper(decimal obj)
         {
             WrappedObject = obj;
         }
 
-        public CurrencyWrapper(Object obj)
+        public CurrencyWrapper(object obj)
         {
-            if (!(obj is Decimal))
+            if (!(obj is decimal))
                 throw new ArgumentException(SR.Arg_MustBeDecimal, nameof(obj));
 
-            WrappedObject = (Decimal)obj;
+            WrappedObject = (decimal)obj;
         }
 
-        public Decimal WrappedObject { get; }
+        public decimal WrappedObject { get; }
     }
 }
index afdcb7b..1378347 100644 (file)
@@ -2,12 +2,13 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+#nullable enable
 namespace System.Runtime.InteropServices
 {
     // Wrapper that is converted to a variant with VT_DISPATCH
     public sealed class DispatchWrapper
     {
-        public DispatchWrapper(Object obj)
+        public DispatchWrapper(object? obj)
         {
             if (obj != null)
             {
@@ -21,6 +22,6 @@ namespace System.Runtime.InteropServices
             }
         }
 
-        public object WrappedObject { get; }
+        public object? WrappedObject { get; }
     }
 }
index d25f023..a68d6a3 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+#nullable enable
 namespace System.Runtime.InteropServices
 {
     // Wrapper that is converted to a variant with VT_ERROR.
index c81a701..8ba7c41 100644 (file)
@@ -2,23 +2,24 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+#nullable enable
 namespace System.Runtime.InteropServices
 {
     public readonly struct HandleRef
     {
         // ! Do not add or rearrange fields as the EE depends on this layout.
         //------------------------------------------------------------------
-        private readonly object _wrapper;
+        private readonly object? _wrapper;
         private readonly IntPtr _handle;
         //------------------------------------------------------------------
 
-        public HandleRef(object wrapper, IntPtr handle)
+        public HandleRef(object? wrapper, IntPtr handle)
         {
             _wrapper = wrapper;
             _handle = handle;
         }
 
-        public object Wrapper
+        public object? Wrapper
         {
             get
             {
index 6dd90e2..7a7a235 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+#nullable enable
 namespace System.Runtime.InteropServices
 {
     // This the base interface that custom adapters can chose to implement when they want to expose the underlying object.
index 799db6a..41d4f68 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+#nullable enable
 namespace System.Runtime.InteropServices
 {
     public interface ICustomFactory
index a91fd7f..439fea4 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+#nullable enable
 namespace System.Runtime.InteropServices
 {
     // This the interface that be implemented by class that want to customize the behavior of QueryInterface.
index 562bd71..41b1bad 100644 (file)
@@ -65,8 +65,7 @@
 // static variable (perhaps using Interlocked.CompareExchange).  Of course,
 // assignments in a static class constructor are under a lock implicitly.
 
-using System;
-using System.Diagnostics;
+#nullable enable
 using System.Runtime.CompilerServices;
 using Internal.Runtime.CompilerServices;
 using Microsoft.Win32.SafeHandles;
index 9d1599a..22dca1c 100644 (file)
@@ -2,16 +2,17 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+#nullable enable
 namespace System.Runtime.InteropServices
 {
     // Wrapper that is converted to a variant with VT_UNKNOWN.
     public sealed class UnknownWrapper
     {
-        public UnknownWrapper(Object obj)
+        public UnknownWrapper(object? obj)
         {
             WrappedObject = obj;
         }
 
-        public Object WrappedObject { get; }
+        public object? WrappedObject { get; }
     }
 }
index 3d75da1..f93a163 100644 (file)
@@ -2,16 +2,17 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+#nullable enable
 namespace System.Runtime.InteropServices
 {
     // Wrapper that is converted to a variant with VT_BYREF | VT_VARIANT.
     public sealed class VariantWrapper
     {
-        public VariantWrapper(Object obj)
+        public VariantWrapper(object? obj)
         {
             WrappedObject = obj;
         }
 
-        public Object WrappedObject { get; }
+        public object? WrappedObject { get; }
     }
 }
index 3ba177a..c76f8af 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+#nullable enable
 using Windows.Foundation.Metadata;
 using Windows.Storage;
 using System.IO;
@@ -124,13 +125,13 @@ namespace System
             switch (folder)
             {
                 case SpecialFolder.ApplicationData:
-                    return ApplicationData.Current.RoamingFolder?.Path;
+                    return ApplicationData.Current.RoamingFolder?.Path ?? string.Empty;
                 case SpecialFolder.LocalApplicationData:
-                    return ApplicationData.Current.LocalFolder?.Path;
+                    return ApplicationData.Current.LocalFolder?.Path ?? string.Empty;
                 case SpecialFolder.System:
                     return SystemDirectory;
                 case SpecialFolder.Windows:
-                    return Path.GetDirectoryName(SystemDirectory);
+                    return Path.GetDirectoryName(SystemDirectory)!; // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
                 default:
                     return string.Empty;
             }
index 5597c13..e2a85e8 100644 (file)
@@ -2,8 +2,8 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+#nullable enable
 using System.Runtime.CompilerServices;
-using System.Runtime.InteropServices;
 
 namespace System
 {