Merge pull request #16727 from jckarter/decodeMangledType-node-checks-4.2

[4.2]  TypeDecoder: Be more forgiving of extra child nodes in demangled nominal types.
diff --git a/include/swift/Demangling/TypeDecoder.h b/include/swift/Demangling/TypeDecoder.h
index 9c7b3d9..e20cf2f 100644
--- a/include/swift/Demangling/TypeDecoder.h
+++ b/include/swift/Demangling/TypeDecoder.h
@@ -127,7 +127,7 @@
     case NodeKind::BoundGenericEnum:
     case NodeKind::BoundGenericStructure:
     case NodeKind::BoundGenericOtherNominalType: {
-      if (Node->getNumChildren() != 2)
+      if (Node->getNumChildren() < 2)
         return BuiltType();
 
       BuiltNominalTypeDecl typeDecl = BuiltNominalTypeDecl();
@@ -161,7 +161,7 @@
       // Handle lowered metatypes in a hackish way. If the representation
       // was not thin, force the resulting typeref to have a non-empty
       // representation.
-      if (Node->getNumChildren() == 2) {
+      if (Node->getNumChildren() >= 2) {
         auto repr = Node->getChild(i++);
         if (repr->getKind() != NodeKind::MetatypeRepresentation ||
             !repr->hasText())
diff --git a/test/stdlib/Inputs/RuntimeRetroactiveConformance/A.swift b/test/stdlib/Inputs/RuntimeRetroactiveConformance/A.swift
new file mode 100644
index 0000000..d1e95f5
--- /dev/null
+++ b/test/stdlib/Inputs/RuntimeRetroactiveConformance/A.swift
@@ -0,0 +1 @@
+public struct AType {}
diff --git a/test/stdlib/Inputs/RuntimeRetroactiveConformance/B.swift b/test/stdlib/Inputs/RuntimeRetroactiveConformance/B.swift
new file mode 100644
index 0000000..182996b
--- /dev/null
+++ b/test/stdlib/Inputs/RuntimeRetroactiveConformance/B.swift
@@ -0,0 +1 @@
+public protocol BProto {}
diff --git a/test/stdlib/RuntimeRetroactiveConformance.swift b/test/stdlib/RuntimeRetroactiveConformance.swift
new file mode 100644
index 0000000..aa91c9f
--- /dev/null
+++ b/test/stdlib/RuntimeRetroactiveConformance.swift
@@ -0,0 +1,24 @@
+// RUN: %empty-directory(%t)
+// RUN: %target-build-swift -c -force-single-frontend-invocation -parse-as-library -emit-module -emit-module-path %t/A.swiftmodule -o %t/A.o -module-name A %S/Inputs/RuntimeRetroactiveConformance/A.swift 
+// RUN: %target-build-swift -c -force-single-frontend-invocation -parse-as-library -emit-module -emit-module-path %t/B.swiftmodule -o %t/B.o -module-name B %S/Inputs/RuntimeRetroactiveConformance/B.swift 
+// RUN: %target-build-swift %s %t/A.o %t/B.o -I %t -o %t/a.out
+// RUN: %target-run %t/a.out
+
+// REQUIRES: executable_test
+
+import A
+import B
+
+extension AType: BProto {}
+
+struct Foo<T: BProto> {}
+
+struct Bar {
+  var foo: Foo<AType> = Foo()
+}
+
+let mirror = Mirror(reflecting: Bar())
+
+_ = mirror.children.first!
+
+print("I survived") // CHECK: I survived