Correct nullability annotations for CreateInstance implementation details (#675)
authorJan Kotas <jkotas@microsoft.com>
Sun, 8 Dec 2019 21:10:15 +0000 (13:10 -0800)
committerGitHub <noreply@github.com>
Sun, 8 Dec 2019 21:10:15 +0000 (13:10 -0800)
Fixes #674

src/coreclr/src/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs
src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs
src/coreclr/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs
src/libraries/System.Private.CoreLib/src/System/Activator.RuntimeType.cs
src/libraries/System.Private.CoreLib/src/System/Activator.cs

index e090679..c0690fd 100644 (file)
@@ -1203,7 +1203,7 @@ namespace System.Reflection
                 }
                 else
                 {
-                    attribute = attributeType.CreateInstanceDefaultCtor(publicOnly: false, skipCheckThis: true, fillCache: true, wrapExceptions: false);
+                    attribute = attributeType.CreateInstanceDefaultCtor(publicOnly: false, skipCheckThis: true, fillCache: true, wrapExceptions: false)!;
 
                     // It is allowed by the ECMA spec to have an empty signature blob
                     int blobLen = (int)((byte*)blobEnd - (byte*)blobStart);
index 1f7dc73..a2279a5 100644 (file)
@@ -198,7 +198,7 @@ namespace System.Runtime.InteropServices
         private static object PtrToStructureHelper(IntPtr ptr, Type structureType)
         {
             var rt = (RuntimeType)structureType;
-            object structure = rt.CreateInstanceDefaultCtor(publicOnly: false, skipCheckThis: false, fillCache: false, wrapExceptions: true);
+            object structure = rt.CreateInstanceDefaultCtor(publicOnly: false, skipCheckThis: false, fillCache: false, wrapExceptions: true)!;
             PtrToStructureHelper(ptr, structure, allowValueClasses: true);
             return structure;
         }
index 10ca31a..0b7da5b 100644 (file)
@@ -3822,12 +3822,12 @@ namespace System
                 throw new NotSupportedException(SR.Acc_CreateVoid);
         }
 
-        internal object CreateInstanceImpl(
+        internal object? CreateInstanceImpl(
             BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture)
         {
             CreateInstanceCheckThis();
 
-            object server;
+            object? instance;
 
             args ??= Array.Empty<object>();
 
@@ -3841,7 +3841,7 @@ namespace System
             if (args.Length == 0 && (bindingAttr & BindingFlags.Public) != 0 && (bindingAttr & BindingFlags.Instance) != 0
                 && (IsGenericCOMObjectImpl() || IsValueType))
             {
-                server = CreateInstanceDefaultCtor(publicOnly, false, true, wrapExceptions);
+                instance = CreateInstanceDefaultCtor(publicOnly, skipCheckThis: false, fillCache: true, wrapExceptions);
             }
             else
             {
@@ -3895,17 +3895,17 @@ namespace System
                     }
 
                     // fast path??
-                    server = Activator.CreateInstance(this, nonPublic: true, wrapExceptions: wrapExceptions)!;
+                    instance = Activator.CreateInstance(this, nonPublic: true, wrapExceptions: wrapExceptions);
                 }
                 else
                 {
-                    server = ((ConstructorInfo)invokeMethod).Invoke(bindingAttr, binder, args, culture);
+                    instance = ((ConstructorInfo)invokeMethod).Invoke(bindingAttr, binder, args, culture);
                     if (state != null)
                         binder.ReorderArgumentArray(ref args, state);
                 }
             }
 
-            return server;
+            return instance;
         }
 
         // the cache entry
@@ -3951,7 +3951,7 @@ namespace System
         /// <summary>
         /// The slow path of CreateInstanceDefaultCtor
         /// </summary>
-        private object CreateInstanceDefaultCtorSlow(bool publicOnly, bool wrapExceptions, bool fillCache)
+        private object? CreateInstanceDefaultCtorSlow(bool publicOnly, bool wrapExceptions, bool fillCache)
         {
             RuntimeMethodHandleInternal runtimeCtor = default;
             bool canBeCached = false;
@@ -3977,7 +3977,7 @@ namespace System
         /// </summary>
         [DebuggerStepThrough]
         [DebuggerHidden]
-        internal object CreateInstanceDefaultCtor(bool publicOnly, bool skipCheckThis, bool fillCache, bool wrapExceptions)
+        internal object? CreateInstanceDefaultCtor(bool publicOnly, bool skipCheckThis, bool fillCache, bool wrapExceptions)
         {
             // Call the cached
             if (GenericCache is ActivatorCache cacheEntry)
index a679e60..8d89a05 100644 (file)
@@ -12,6 +12,10 @@ namespace System
 {
     public static partial class Activator
     {
+        //
+        // Note: CreateInstance returns null for Nullable<T>, e.g. CreateInstance(typeof(int?)) returns null.
+        //
+
         public static object? CreateInstance(Type type, BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture, object?[]? activationAttributes)
         {
             if (type is null)
@@ -139,7 +143,7 @@ namespace System
         [System.Runtime.CompilerServices.Intrinsic]
         public static T CreateInstance<T>()
         {
-            return (T)((RuntimeType)typeof(T)).CreateInstanceDefaultCtor(publicOnly: true, skipCheckThis: true, fillCache: true, wrapExceptions: true);
+            return (T)((RuntimeType)typeof(T)).CreateInstanceDefaultCtor(publicOnly: true, skipCheckThis: true, fillCache: true, wrapExceptions: true)!;
         }
 
         private static T CreateDefaultInstance<T>() where T : struct => default;
index 0426211..3c6c24b 100644 (file)
@@ -16,6 +16,10 @@ namespace System
     {
         private const BindingFlags ConstructorDefault = BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance;
 
+        //
+        // Note: CreateInstance returns null for Nullable<T>, e.g. CreateInstance(typeof(int?)) returns null.
+        //
+
         [DebuggerHidden]
         [DebuggerStepThrough]
         public static object? CreateInstance(Type type, BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture) =>