Merge pull request #17685 from jckarter/nested-collection-upcast-peephole-4.2

[4.2] Sema: Clear the types of exprs changed by collection upcast peepholes.
diff --git a/lib/Migrator/APIDiffMigratorPass.cpp b/lib/Migrator/APIDiffMigratorPass.cpp
index de37aed..825fe29 100644
--- a/lib/Migrator/APIDiffMigratorPass.cpp
+++ b/lib/Migrator/APIDiffMigratorPass.cpp
@@ -1052,8 +1052,10 @@
   bool walkToExprPre(Expr *E) override {
     if (E->getSourceRange().isInvalid())
       return false;
-    if (handleRevertRawRepresentable(E))
-      return false;
+    if (handleRevertRawRepresentable(E)) {
+      // The name may also change, so we should keep visiting.
+      return true;
+    }
     if (handleQualifiedReplacement(E))
       return false;
     if (handleAssignDestMigration(E))
diff --git a/stdlib/public/core/NormalizedCodeUnitIterator.swift b/stdlib/public/core/NormalizedCodeUnitIterator.swift
index 40b9903..c76a487 100644
--- a/stdlib/public/core/NormalizedCodeUnitIterator.swift
+++ b/stdlib/public/core/NormalizedCodeUnitIterator.swift
@@ -63,7 +63,11 @@
     where Source.Element == UInt16, Source.SubSequence == Source
   {
     var remaining: Int {
-      return collection.distance(from: index, to: collection.endIndex)
+      @_specialize(where Source == _UnmanagedString<UInt16>)
+      @_specialize(where Source == _UnmanagedOpaqueString)
+      get {
+        return collection.distance(from: index, to: collection.endIndex)
+      }
     }
     var collection: Source
     var index: Source.Index
diff --git a/test/Migrator/Inputs/Cities.swift b/test/Migrator/Inputs/Cities.swift
index 4704e49..edc531a 100644
--- a/test/Migrator/Inputs/Cities.swift
+++ b/test/Migrator/Inputs/Cities.swift
@@ -84,7 +84,8 @@
 
 public class Wrapper {
   public struct Attribute: RawRepresentable {
-    public init?(rawValue: String) { self.rawValue = rawValue }
+    public static let KnownAttr = Wrapper.Attribute(rawValue: "")
+    public init(rawValue: String) { self.rawValue = rawValue }
     public var rawValue: String
     public typealias RawValue = String
   }
diff --git a/test/Migrator/Inputs/string-representable.json b/test/Migrator/Inputs/string-representable.json
index d991bc3..309a115 100644
--- a/test/Migrator/Inputs/string-representable.json
+++ b/test/Migrator/Inputs/string-representable.json
@@ -260,4 +260,12 @@
     "RightComment": "String",
     "ModuleName": "Cities"
   },
+  {
+    "DiffItemKind": "TypeMemberDiffItem",
+    "Usr": "s:6Cities7WrapperC9AttributeV9KnownAttrAEvpZ",
+    "OldPrintedName": "KnownAttr",
+    "OldTypeName": "Wrapper.Attribute",
+    "NewPrintedName": "NewKnownAttr",
+    "NewTypeName": "NewAttributeWrapper"
+  },
 ]
diff --git a/test/Migrator/string-representable.swift b/test/Migrator/string-representable.swift
index d16b42e..2178340 100644
--- a/test/Migrator/string-representable.swift
+++ b/test/Migrator/string-representable.swift
@@ -53,6 +53,7 @@
   _ = Wrapper.Attribute(rawValue: "somevalue")
   _ = Wrapper.Attribute.init(rawValue: "somevalue")
   _ = b.rawValue
+  _ = Wrapper.Attribute.KnownAttr.rawValue
 }
 
 
diff --git a/test/Migrator/string-representable.swift.expected b/test/Migrator/string-representable.swift.expected
index 93bc6c7..9108b47 100644
--- a/test/Migrator/string-representable.swift.expected
+++ b/test/Migrator/string-representable.swift.expected
@@ -53,6 +53,7 @@
   _ = "somevalue"
   _ = "somevalue"
   _ = b
+  _ = NewAttributeWrapper.NewKnownAttr
 }