Merge pull request #22642 from DougGregor/gsb-infer-nested-type-name-match-5.0

[5.0] [ABI] [GSB] Consistently use nested type name match constraints.
diff --git a/lib/Migrator/APIDiffMigratorPass.cpp b/lib/Migrator/APIDiffMigratorPass.cpp
index ef8f0bf..815be1d 100644
--- a/lib/Migrator/APIDiffMigratorPass.cpp
+++ b/lib/Migrator/APIDiffMigratorPass.cpp
@@ -1102,6 +1102,25 @@
     }
   }
 
+  // If a property has changed from nonnull to nullable, we should add ! to the
+  // reference of the property.
+  bool handlePropertyTypeChange(Expr *E) {
+    if (auto MRE = dyn_cast<MemberRefExpr>(E)) {
+      if (auto *VD = MRE->getReferencedDecl().getDecl()) {
+        for (auto *I: getRelatedDiffItems(VD)) {
+          if (auto *Item = dyn_cast<CommonDiffItem>(I)) {
+            if (Item->DiffKind == NodeAnnotation::WrapOptional &&
+                Item->NodeKind == SDKNodeKind::DeclVar) {
+              Editor.insertAfterToken(E->getEndLoc(), "!");
+              return true;
+            }
+          }
+        }
+      }
+    }
+    return false;
+  }
+
   bool walkToExprPre(Expr *E) override {
     if (E->getSourceRange().isInvalid())
       return false;
@@ -1115,6 +1134,8 @@
       return false;
     if (handleAttributeReference(E))
       return false;
+    if (handlePropertyTypeChange(E))
+      return false;
     if (auto *CE = dyn_cast<CallExpr>(E)) {
       auto Fn = CE->getFn();
       auto Args = CE->getArg();
diff --git a/test/Migrator/Inputs/CallExpr.json b/test/Migrator/Inputs/CallExpr.json
index ec11dd5..431f4cd 100644
--- a/test/Migrator/Inputs/CallExpr.json
+++ b/test/Migrator/Inputs/CallExpr.json
@@ -20,5 +20,16 @@
     "RightUsr": "",
     "RightComment": "",
     "ModuleName": "Cities"
+  },
+  {
+    "DiffItemKind": "CommonDiffItem",
+    "NodeKind": "Var",
+    "NodeAnnotation": "WrapOptional",
+    "ChildIndex": "0",
+    "LeftUsr": "s:6CitiesAAC4nameSSvp",
+    "LeftComment": "",
+    "RightUsr": "",
+    "RightComment": "",
+    "ModuleName": "Cities"
   }
 ]
diff --git a/test/Migrator/Inputs/Cities.swift b/test/Migrator/Inputs/Cities.swift
index 5cb7320..dc04453 100644
--- a/test/Migrator/Inputs/Cities.swift
+++ b/test/Migrator/Inputs/Cities.swift
@@ -1,5 +1,6 @@
 open class Cities {
   var x: Int
+  public var name: String = ""
   public init(x: Int) { self.x = x }
   public init!(y: Int) { self.x = y }
   open func mooloolaba(x: Cities, y: Cities?) {}
diff --git a/test/Migrator/call_expr_result.swift b/test/Migrator/call_expr_result.swift
index 01f1c8b..55dfcc5 100644
--- a/test/Migrator/call_expr_result.swift
+++ b/test/Migrator/call_expr_result.swift
@@ -10,4 +10,8 @@
   let c1 = Cities(x: 3)
   _ = Cities.init(x: 3)
   _ = c1.noosa()
-}
\ No newline at end of file
+  _ = c1.name
+  bar(c1.name)
+}
+
+func bar(_ n: String) {}
diff --git a/test/Migrator/call_expr_result.swift.expected b/test/Migrator/call_expr_result.swift.expected
index 4210d21..1a211b0 100644
--- a/test/Migrator/call_expr_result.swift.expected
+++ b/test/Migrator/call_expr_result.swift.expected
@@ -10,4 +10,8 @@
   let c1 = Cities(x: 3)!
   _ = Cities.init(x: 3)!
   _ = c1.noosa()!
-}
\ No newline at end of file
+  _ = c1.name!
+  bar(c1.name!)
+}
+
+func bar(_ n: String) {}