Fix incorrect optimization of Activator.CreateInstance (#50228)
authorMichal Strehovský <MichalStrehovsky@users.noreply.github.com>
Thu, 25 Mar 2021 14:39:41 +0000 (15:39 +0100)
committerGitHub <noreply@github.com>
Thu, 25 Mar 2021 14:39:41 +0000 (07:39 -0700)
We should not return "default" if there's a private parameterless constructor. GetDefaultConstructor only calls the default constructor in the C# sense (public parameterless ctor on a non-abstract class).

src/coreclr/tools/aot/ILCompiler.ReadyToRun/IL/ReadyToRunILProvider.cs

index 46ec240..ec9ced7 100644 (file)
@@ -19,7 +19,7 @@ namespace Internal.IL
                 && method.Name == "CreateInstance")
             {
                 TypeDesc type = method.Instantiation[0];
-                if (type.IsValueType && type.GetDefaultConstructor() == null)
+                if (type.IsValueType && type.GetParameterlessConstructor() == null)
                 {
                     // Replace the body with implementation that just returns "default"
                     MethodDesc createDefaultInstance = method.OwningType.GetKnownMethod("CreateDefaultInstance", method.GetTypicalMethodDefinition().Signature);