[vm/kernel] Fix reading of type parameters in factory constructors

VM will crash when factory constructor was passed typedef with type arguments. procedure_type_parameter_count should not include type argument for factory function, as factory function share the same type argument with class.
Bug:  https://github.com/dart-lang/sdk/issues/37264
Change-Id: I358c6d35cf217162d2b89f7d8fa9d303f981b713
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/106563
Reviewed-by: Siva Annamalai <asiva@google.com>
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Commit-Queue: Zichang Guo <zichangguo@google.com>
diff --git a/runtime/tests/vm/dart/type_argument_factory_constructor_test.dart b/runtime/tests/vm/dart/type_argument_factory_constructor_test.dart
new file mode 100644
index 0000000..58bac74
--- /dev/null
+++ b/runtime/tests/vm/dart/type_argument_factory_constructor_test.dart
@@ -0,0 +1,21 @@
+// Copyright (c) 2019, the Dart project authors.  Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+// Regression test for Issue https://github.com/dart-lang/sdk/issues/37264.
+typedef SomeCb = T Function<T>(T val);
+
+class A<T> {
+  final SomeCb cb;
+
+  A(this.cb);
+
+  factory A.b(SomeCb cb) {
+    return A(cb);
+  }
+}
+
+main() {
+  // VM should not crash on this case
+  A<int>.b(<String>(v) => v);
+}
diff --git a/runtime/vm/compiler/frontend/kernel_translation_helper.cc b/runtime/vm/compiler/frontend/kernel_translation_helper.cc
index 35c0178..d70628f 100644
--- a/runtime/vm/compiler/frontend/kernel_translation_helper.cc
+++ b/runtime/vm/compiler/frontend/kernel_translation_helper.cc
@@ -2900,9 +2900,10 @@
       }
       parameter_index -= class_types.Length();
     }
-
+    // Factory function should not be considered as procedure.
     intptr_t procedure_type_parameter_count =
-        active_class_->MemberIsProcedure()
+        (active_class_->MemberIsProcedure() &&
+         !active_class_->MemberIsFactoryProcedure())
             ? active_class_->MemberTypeParameterCount(Z)
             : 0;
     if (procedure_type_parameter_count > 0) {